You can run periodic tasks through the built-in scheduler. You can bind to the main system events or create tasks that run at random times.

Creating a CRON file

The files for the task scheduler are created in the "Modules" section of the hack control. The file must begin with cron and, separated by a hyphen, the name of the task itself. For example, cron-stats.php.

The file must contain the main function. When creating a file from the interface, it will be added automatically. The name of the function should be in the format hack_name_cron_task, where instead of name there will be the name of your hack, and instead of task - task name. For example, hack_proxy_cron_stats. The function takes one parameter as input - the $core object.

<?php
function hack_proxy_cron_stats( $core ) {
  // Cron code here
}

Adding file to the scheduler

The built-in AlterCPA scheduler supports four types of events:

  • cron-1min - runs every minute. It is recommended to use for fast tasks with critical execution time.
  • cron-3min - runs every 3 minutes. It is recommended to use it to run long tasks without critical execution time, such as downloading updates.
  • cron-10min - runs every 10 minutes. Recommended for tasks of medium duration that take minutes.
  • cron-1day - runs at midnight. It is recommended to use for long tasks, database optimization, statistics collection, storage cleanup and other tasks lasting tens of minutes.

The names of these events are used as keys in the initialization array. The value is an array with a short name of the scheduler files responsible for this event.

For example, let's add cron-stats.php and cron-emails.php files for daily execution, and cron-visits.php files for every minute execution >. The initialization array will look like this:

return [
  /* other initialization commands */
  'cron-1min' => [ 'visits' ],
  'cron-1day' => [ 'stats', 'emails' ],
];

Arbitrary periods of time

You can run tasks at arbitrary random intervals. This is useful for running tasks to check for updates, collect statistics, and other events that run every few hours. Such functions are recommended to be added to the three-minute performance block.

We add a check for the current time to the beginning of the function and transfer the timer to the next request:

$timer = $core->config( 'hack-proxy-stats' );
if ( $timer > time() ) return true;
$settimer = time() + rand( 1800, 3600 );
$core->reconf( 'hack-proxy-stats', $settimer );

It is recommended to use the name of the hack and function with the hack prefix as the name of the configuration field. As a transfer time, it is recommended to use a random value in the specified range with a spread of several minutes. For example, rand( 1800, 3600 ) will delay execution by half an hour to one hour.

Your scheduler events

You can create your own scheduler events that are different from the system ones. They will not be automatically installed along with the hack, they always require manual installation.

Create a file with the name of your event in the tasks folder on your system. Let's say your event is called crawl. In this case, the file will be called crawl.php and its content will be like this:

<?php
define ( 'PATH', realpath( dirname(__FILE__) . '/../' ). '/' ) ;
require_once PATH . 'core/mods/cron.php';
crontab( 'crawl' );

Write the file to the server scheduler as user wsvr:

crontab -e -u wsvr

Specify the scheduler line there in the following format:

*/15 * * * * php -f /var/www/network.domain/tasks/crawl.php > /dev/null 2>&1

To initialize the functions of this scheduler, use the cron-crawl key.