Declaring a cron task

To declare a task, you need to specify its name and frequency in seconds using the taches_generales_cron pipeline:

function myplugin_general_cron_jobs($taches){
	$taches['nom'] = 24*3600; // once per day, every day
}

This task will be called at the appropriate time. The processes are placed in a file in the genie/ directory, with the same name as the (name.php) task and including a genie_name_dist() function.

The function is passed the date at which it last performed that task as an argument. It must return a number:

  • null, if the task has nothing to do
  • positive, if the task has been run
  • negative, if the task started but could not complete. This makes it possible to run tasks in batches (to avoid timeouts on PHP script executions because the processes run too long). In such cases, the negative number indicated corresponds to the number of seconds of interval before the next scheduled task run.

Example

This example is simple, originating from SPIP’s "maintenance" tasks in the genie/maintenance.php file, since it executes functions and always returns 1, indicating that the action has been run.

// Various maintenance tasks
function genie_maintenance_dist ($t) {

	// (re)set .htaccess with deny from all
	// for the two nominated directories inaccessible through http
	include_spip('inc/acces');
	verifier_htaccess(_DIR_ETC);
	verifier_htaccess(_DIR_TMP);

	// Confirm that neither table crashed
	if (!_request('reinstall'))
		verifier_crash_tables();

	return 1;
}

Author Mark Baber Published : Updated : 12/03/23

Translations : English, français