Cron Manager

The cron manager is responsible for background processes in the application and runs every minute using crontab.

All executed tasks are recorded in the log file /files/logs/cron.log.

Modules

To specify the need to run a background task for a module, you need to declare the following method:

public function cronSettings()
{
    return array(
        'cronTask1' => array('period' => '*/15 * * * *'),
        'cronTask2' => array('period' => '0 * * * *'),
    );
}

In the example, two tasks were described, where the method cronTask1 will run every 15 minutes, and the method cronTask2 will run once an hour at 0 minutes.

In the module's controller, there should be methods with the specified names and the following structure:

public function cronTask1()
{
    if ( ! bff::cron()) {
        // Check the execution context
        // This way the method will only be accessible in the context of the cron manager
        return;
    }

    // Executable code
}

The method must be public and contain a check for the execution context. It should be declared in the base or frontend controller.

If it is necessary to run a background task once only, it can be done as follows:

bff::cronManager()->executeOnce('module', 'method', ['param'=>1]);

Where the module and method names are specified, and it is also possible to pass parameters to the function. The method call will be executed within the next minute, at the next execution of the cron manager.

Extensions

Plugins and themes can also declare the cronSettings method in the index.php file if necessary, with the same content as described above. One-time execution of a task within the context of an extension looks like this:

$this->cronExecuteOnce('method', ['param'=>1]);

It is also possible to specify the execution time:

$this->cronExecuteOnce('method', ['param'=>1], ['time'=>strtotime('+1 hour')]);

In the example, the execution will be performed in one hour.