Moodle PHP Documentation 4.2
Moodle 4.2.8 (Build: 20240610) (2d41ac46f45)
core\cron Class Reference

Static Public Member Functions

static prepare_core_renderer ($restore=false)
 Prepare the output renderer for the cron run.
 
static reset_user_cache ()
 Resets the cache for the cron user used by setup_user().
 
static run_adhoc_task (int $taskid)
 Execute an adhoc task.
 
static run_adhoc_tasks (int $startruntime, $keepalive=0, $checklimits=true, ?int $startprocesstime=null, ?int $maxtasks=null, ?string $classname=null,)
 Execute all queued adhoc tasks, applying necessary concurrency limits and time limits.
 
static run_failed_adhoc_tasks (?string $classname=null)
 Execute all failed adhoc tasks.
 
static run_inner_adhoc_task (\core\task\adhoc_task $task)
 Shared code that handles running of a single adhoc task within the cron.
 
static run_inner_scheduled_task (\core\task\task_base $task)
 Shared code that handles running of a single scheduled task within the cron.
 
static run_main_process (?int $keepalive=null)
 Execute cron tasks.
 
static run_scheduled_tasks (int $startruntime, ?int $startprocesstime=null,)
 Execute all queued scheduled tasks, applying necessary concurrency limits and time limits.
 
static set_process_title (string $title)
 Sets the process title.
 
static setup_user (?stdClass $user=null, ?stdClass $course=null, bool $leavepagealone=false)
 Sets up a user and course environment in cron.
 
static trace_time_and_memory ()
 Output some standard information during cron runs.
 

Public Attributes

int const DEFAULT_MAIN_PROCESS_KEEPALIVE = 3 * MINSECS
 Use a default value of 3 minutes.
 
int const MAX_MAIN_PROCESS_KEEPALIVE = 15 * MINSECS
 The max keepalive value for the main cron runner.
 

Static Protected Attributes

static stdClass stdClass $cronsession = null
 The cron user's session data.
 
static stdClass stdClass $cronuser = null
 A copy of the standard cron 'user'.
 

Member Function Documentation

◆ prepare_core_renderer()

static core\cron::prepare_core_renderer ( $restore = false)
static

Prepare the output renderer for the cron run.

This involves creating a new $PAGE, and $OUTPUT fresh for each task and prevents any one task from influencing any other.

Parameters
bool$restoreWhether to restore the original PAGE and OUTPUT

◆ run_adhoc_task()

static core\cron::run_adhoc_task ( int $taskid)
static

Execute an adhoc task.

Parameters
int$taskid

◆ run_adhoc_tasks()

static core\cron::run_adhoc_tasks ( int $startruntime,
$keepalive = 0,
$checklimits = true,
?int $startprocesstime = null,
?int $maxtasks = null,
?string $classname = null )
static

Execute all queued adhoc tasks, applying necessary concurrency limits and time limits.

Parameters
int$startruntimeThe time this run started.
int$keepaliveKeep this public static function alive for N seconds and poll for new adhoc tasks.
bool$checklimitsShould we check limits?
null | int$startprocesstimeThe time this process started.
int | null$maxtasksLimit number of tasks to run`
null | string$classnameRun only tasks of this class
Exceptions
moodle_exception

◆ run_failed_adhoc_tasks()

static core\cron::run_failed_adhoc_tasks ( ?string $classname = null)
static

Execute all failed adhoc tasks.

Parameters
string | null$classnameRun only tasks of this class

◆ run_inner_adhoc_task()

static core\cron::run_inner_adhoc_task ( \core\task\adhoc_task $task)
static

Shared code that handles running of a single adhoc task within the cron.

Parameters
core\task\adhoc_task$task

◆ run_inner_scheduled_task()

static core\cron::run_inner_scheduled_task ( \core\task\task_base $task)
static

Shared code that handles running of a single scheduled task within the cron.

Not intended for calling directly outside of this library!

Parameters
core\task\task_base$task

◆ run_main_process()

static core\cron::run_main_process ( ?int $keepalive = null)
static

Execute cron tasks.

Parameters
int | null$keepaliveThe keepalive time for this cron run.

◆ run_scheduled_tasks()

static core\cron::run_scheduled_tasks ( int $startruntime,
?int $startprocesstime = null )
static

Execute all queued scheduled tasks, applying necessary concurrency limits and time limits.

Parameters
int$startruntimeThe time this run started.
null | int$startprocesstimeThe time the process that owns this runner started.
Exceptions
moodle_exception

◆ set_process_title()

static core\cron::set_process_title ( string $title)
static

Sets the process title.

This makes it very easy for a sysadmin to immediately see what task a cron process is running at any given moment.

Parameters
string$titleprocess status title

◆ setup_user()

static core\cron::setup_user ( ?stdClass $user = null,
?stdClass $course = null,
bool $leavepagealone = false )
static

Sets up a user and course environment in cron.

Note: This function is intended only for use in:

  • the cron runner scripts
  • individual tasks which extend the adhoc_task and scheduled_task classes
  • unit tests related to tasks
  • other parts of the cron/task system

Please note that this function stores cache data statically.

See also
reset_user_cache() to reset this cache.
Parameters
null | stdClass$userfull user object, null means default cron user (admin)
null | stdClass$coursefull course record, null means $SITE
null | bool$leavepagealoneIf specified, stops it messing with global page object

◆ trace_time_and_memory()

static core\cron::trace_time_and_memory ( )
static

Output some standard information during cron runs.

Specifically current time and memory usage. This method also does gc_collect_cycles() (before displaying memory usage) to try to help PHP manage memory better.

Member Data Documentation

◆ DEFAULT_MAIN_PROCESS_KEEPALIVE

int const core\cron::DEFAULT_MAIN_PROCESS_KEEPALIVE = 3 * MINSECS

Use a default value of 3 minutes.

The recommended cron frequency is every minute, and the default adhoc concurrency is 3. A default value of 3 minutes allows all adhoc tasks to be run concurrently at their default value.

The default keepalive value for the main cron runner


The documentation for this class was generated from the following file: