Views: 10804
Last Modified: 05.04.2022
Attention! Provided settings are listed outside the scope of the Virtual Appliance menu. This means that the details below are provided for informational purposes only and should be used with clear understanding of your actions and under your own liability. Bitrix24 technical support reviews the questions related to Virtual Appliance menu items only.

Moving agent to cron

In large or medium-sized projects, sometimes there is an issue with the transfer of especially heavy agents to Cron.

  • First, fully disable execution of agents on a hit. To do this, the command /bitrix/admin/php_command_line.php?lang=en should be executed in PHP-console of Bitrxi24 product administrative menu:
    COption::SetOptionString("main", "agents_use_crontab", "N"); 
    echo COption::GetOptionString("main", "agents_use_crontab", "N"); 
    
    COption::SetOptionString("main", "check_agents", "N"); 
    echo COption::GetOptionString("main", "check_agents", "Y");
    

    As the result, we should have NN.

  • Remove the definition of the following constants from the /bitrix/php_interface/dbconn.php file:
    define("BX_CRONTAB_SUPPORT", true);
    define("BX_CRONTAB", true);
    

    And add:

    if(!(defined("CHK_EVENT") && CHK_EVENT===true))
       define("BX_CRONTAB_SUPPORT", true);
     
  • Next, create agent verification and system messages multicasts file /bitrix/php_interface/cron_events.php:
    <?
    $_SERVER["DOCUMENT_ROOT"] = realpath(dirname(__FILE__)."/../..");
    $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];
    
    define("NO_KEEP_STATISTIC", true);
    define("NOT_CHECK_PERMISSIONS",true); 
    define('CHK_EVENT', true);
    
    require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
    
    @set_time_limit(0);
    @ignore_user_abort(true);
    
    CAgent::CheckAgents();
    define("BX_CRONTAB_SUPPORT", true);
    define("BX_CRONTAB", true);
    CEvent::CheckEvents();
    ?>
    
  • And add this script into Cron:
     */5 * * * * /usr/bin/php -f /home/bitrix/www/bitrix/php_interface/cron_events.php
    

After that, all agents and forwarding of system Messages will be processed under cron, once each 1 minute.

Note: Cron failing to execute after the command means you have errors in your project. These errors, most likely, are not agent-related. Look for them in the PHP logs. You can enable an extended error display in .settings.php.

Email message queue

The parameter responsible for the quantity of e-mail messages, processed at once, must modified to prevent a large of e-mail messages queue. To do that, execute the following command in PHP-console:

COption::SetOptionString("main", "mail_event_bulk", "20"); 
echo COption::GetOptionString("main", "mail_event_bulk", "5");

When a regular cron_events.php was launched before completing previously triggered script, agents will fail to launch and script terminates (due to agents being blocked during execution time). In this case, processing is no different from processing on a hit; a new hit can occur at the moment, when agents failed to execute on the previous hit.

Usually, scripts, executed under cron do not have limits on execution time. However, using database-handling methods in scripts can encounter an error with executing embedded scripts. To avoid this error, you can modify the value in /bitrix/php_interface/dbconn.php:

// when script is executed by cron, database connection limit is: 600 seconds, otherwise - 60
@set_time_limit(php_sapi_name() == "cli" ? 600 : 60);




Courses developed by Bitrix24