Documentation

TaskRunner
in package

Runs background tasks (a.k.a. cron jobs), including scheduled tasks.

MOD AUTHORS:

To add a new background task, do the following:

  1. Create a class that extends SMF\Tasks\BackgroundTask. Put your task's code in its execute() method.
  2. Add your background task to the task queue on the fly wherever needed in the rest of your mod's code.

To add a new scheduled task, do the following:

  1. Create a class that extends SMF\Tasks\ScheduledTask. Put your task's code in its execute() method.
  2. Use the integrate_scheduled_tasks hook to add information about your scheduled task to SMF\TaskRunner::$scheduled_tasks.
  3. Add an entry for your scheduled task to the {db_prefix}scheduled_tasks table when your mod is being installed.

Table of Contents

Constants

MAX_CLAIM_THRESHOLD  = 300
If a task fails for whatever reason it will still be marked as claimed.
MAX_CRON_TIME  = 10
This setting is worth bearing in mind. If you are running this from proper cron, make sure you don't run this file any more frequently than indicated here. It might turn ugly if you do. But on proper cron you can always increase this value provided you don't go beyond max_limit.

Properties

$scheduled_tasks  : array<string|int, mixed>

Methods

__construct()  : mixed
Constructor.
calculateNextTrigger()  : void
Calculate the next time the passed tasks should be triggered.
execute()  : void
This is the one that gets stuff done.
handleError()  : void
The error handling function
runOneTask()  : void
Similar to execute(), but it only runs one task at most, and doesn't exit when finished.
runScheduledTasks()  : void
Runs the given scheduled tasks immediately.
cleanRequest()  : void
Cleans up the request variables.
fetchTask()  : bool|array<string|int, mixed>
The heart of this cron handler.
getNextScheduledTime()  : int
Simply returns a time stamp of the next instance of these time parameters.
getScheduledTaskDetails()  : array<string|int, mixed>
Gets the necessary info to load the class for a scheduled task.
obExit()  : void
The exit function.
performTask()  : bool
This actually handles the task.
scheduleTask()  : void
Checks whether there are any scheduled tasks to run, and if there are, adds them to the queue of background tasks.

Constants

MAX_CLAIM_THRESHOLD

If a task fails for whatever reason it will still be marked as claimed.

public mixed MAX_CLAIM_THRESHOLD = 300

This is the threshold by which if a task has not completed in this time, the task should become available again.

MAX_CRON_TIME

This setting is worth bearing in mind. If you are running this from proper cron, make sure you don't run this file any more frequently than indicated here. It might turn ugly if you do. But on proper cron you can always increase this value provided you don't go beyond max_limit.

public mixed MAX_CRON_TIME = 10

Properties

$scheduled_tasks

public static array<string|int, mixed> $scheduled_tasks = ['daily_maintenance' => ['class' => 'SMF\Tasks\DailyMaintenance'], 'weekly_maintenance' => ['class' => 'SMF\Tasks\WeeklyMaintenance'], 'daily_digest' => ['class' => 'SMF\Tasks\SendDigests', 'data' => ['is_weekly' => 0]], 'weekly_digest' => ['class' => 'SMF\Tasks\SendDigests', 'data' => ['is_weekly' => 1]], 'fetchSMfiles' => ['class' => 'SMF\Tasks\FetchSMFiles'], 'fetch_calendar_subs' => ['class' => 'SMF\Tasks\FetchCalendarSubscriptions'], 'birthdayemails' => ['class' => 'SMF\Tasks\Birthday_Notify'], 'paid_subscriptions' => ['class' => 'SMF\Tasks\PaidSubs'], 'remove_temp_attachments' => ['class' => 'SMF\Tasks\RemoveTempAttachments'], 'remove_topic_redirect' => ['class' => 'SMF\Tasks\RemoveTopicRedirects'], 'remove_old_drafts' => ['class' => 'SMF\Tasks\RemoveOldDrafts'], 'prune_log_topics' => ['class' => 'SMF\Tasks\PruneLogTopics']]

Info about the classes to load for scheduled tasks.

Methods

__construct()

Constructor.

public __construct() : mixed

calculateNextTrigger()

Calculate the next time the passed tasks should be triggered.

public static calculateNextTrigger([string|array<string|int, mixed> $tasks = [] ][, bool $force_update = false ]) : void
Parameters
$tasks : string|array<string|int, mixed> = []

IDs or names of one or more scheduled tasks.

$force_update : bool = false

Whether to force the tasks to run now.

execute()

This is the one that gets stuff done.

public execute() : void

Internally, this calls $this->fetchTask() to get the details of a background task, then passes those details to $this->performTask(), then calls $this->obExit() to end execution.

handleError()

The error handling function

public static handleError(int $error_level, string $error_string, string $file, int $line) : void
Parameters
$error_level : int

One of the PHP error level constants (see )

$error_string : string

The error message

$file : string

The file where the error occurred

$line : int

What line of the specified file the error occurred on

runOneTask()

Similar to execute(), but it only runs one task at most, and doesn't exit when finished.

public runOneTask() : void

runScheduledTasks()

Runs the given scheduled tasks immediately.

public runScheduledTasks(array<string|int, mixed> $tasks) : void
Parameters
$tasks : array<string|int, mixed>

The IDs or names of the scheduled tasks to run.

cleanRequest()

Cleans up the request variables.

protected cleanRequest() : void

fetchTask()

The heart of this cron handler.

protected fetchTask() : bool|array<string|int, mixed>

..

Return values
bool|array<string|int, mixed>

False if there's nothing to do or an array of info about the task.

getNextScheduledTime()

Simply returns a time stamp of the next instance of these time parameters.

protected static getNextScheduledTime(int $regularity, string $unit, int $offset) : int
Parameters
$regularity : int

The regularity

$unit : string

What unit are we using - 'm' for minutes, 'd' for days, 'w' for weeks or anything else for seconds

$offset : int

The offset

Return values
int

The timestamp for the specified time

getScheduledTaskDetails()

Gets the necessary info to load the class for a scheduled task.

protected getScheduledTaskDetails(int $id, string $task[, bool $is_callable = false ]) : array<string|int, mixed>
Parameters
$id : int

The ID of the task.

$task : string

The name of the task, or a callable to call.

$is_callable : bool = false

Whether $task is the name of a callable rather than a task name.

Return values
array<string|int, mixed>

The file, class name, and basic additional data for the task.

obExit()

The exit function.

protected obExit() : void
Tags
todo:

As of PHP 8.1, this return type can be 'never'

performTask()

This actually handles the task.

protected performTask(array<string|int, mixed> $task_details) : bool
Parameters
$task_details : array<string|int, mixed>

An array of info about the task.

Return values
bool

Whether the task should be cleared from the queue.

scheduleTask()

Checks whether there are any scheduled tasks to run, and if there are, adds them to the queue of background tasks.

protected scheduleTask() : void

        
On this page

Search results