Constants

WP_CRON_HOOK

WP_CRON_HOOK = 'action_scheduler_run_queue'

WP_CRON_SCHEDULE

WP_CRON_SCHEDULE = 'every_minute'

Properties

$cleaner

$cleaner : \ActionScheduler_QueueCleaner

ActionScheduler_QueueCleaner instance.

Type

normal

$monitor

$monitor : \ActionScheduler_FatalErrorMonitor

ActionScheduler_FatalErrorMonitor instance.

Type

normal

$store

$store : \ActionScheduler_Store

ActionScheduler_Store instance.

Type

normal

$async_request

$async_request : \ActionScheduler_AsyncRequest_QueueRunner

ActionScheduler_AsyncRequest_QueueRunner instance.

Type

normal

$created_time

$created_time : int

The created time.

Represents when the queue runner was constructed and used when calculating how long a PHP request has been running. For this reason it should be as close as possible to the PHP request start time.

Type

normal

$runner

$runner : \ActionScheduler_QueueRunner

ActionScheduler_QueueRunner instance.

Type

normal

$processed_actions_count

$processed_actions_count : int

Number of processed actions.

Type

normal

Methods

__construct()

__construct(\ActionScheduler_Store|null  $store = null, \ActionScheduler_FatalErrorMonitor|null  $monitor = null, \ActionScheduler_QueueCleaner|null  $cleaner = null, \ActionScheduler_AsyncRequest_QueueRunner|null  $async_request = null) : mixed

ActionScheduler_QueueRunner constructor.

Parameters

\ActionScheduler_Store|null $store

Store object.

\ActionScheduler_FatalErrorMonitor|null $monitor

Monitor object.

\ActionScheduler_QueueCleaner|null $cleaner

Cleaner object.

\ActionScheduler_AsyncRequest_QueueRunner|null $async_request

Async request runner object.

Returns

mixed

process_action()

process_action(int  $action_id, string  $context = '') : mixed

Process an individual action.

Parameters

int $action_id

The action ID to process.

string $context

Optional identifier for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' Generally, this should be capitalised and not localised as it's a proper noun.

Throws

\Exception

When error running action.

Returns

mixed

get_allowed_concurrent_batches()

get_allowed_concurrent_batches() : int

Get the number of concurrent batches a runner allows.

Returns

int

has_maximum_concurrent_batches()

has_maximum_concurrent_batches() : bool

Check if the number of allowed concurrent batches is met or exceeded.

Returns

bool

run()

run(string  $context = 'WP Cron') : int

Process actions in the queue. Attached to self::WP_CRON_HOOK i.e. 'action_scheduler_run_queue'

The $context param of this method defaults to 'WP Cron', because prior to Action Scheduler 3.0.0 that was the only context in which this method was run, and the self::WP_CRON_HOOK hook had no context passed along with it. New code calling this method directly, or by triggering the self::WP_CRON_HOOK, should set a context as the first parameter. For an example of this, refer to the code seen in

Parameters

string $context

Optional identifier for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' Generally, this should be capitalised and not localised as it's a proper noun.

Returns

int —

The number of actions processed.

instance()

instance() : \ActionScheduler_QueueRunner

Get instance.

Returns

\ActionScheduler_QueueRunner

init()

init() : mixed

Initialize.

Returns

mixed

hook_dispatch_async_request()

hook_dispatch_async_request() : mixed

Hook check for dispatching an async request.

Returns

mixed

unhook_dispatch_async_request()

unhook_dispatch_async_request() : mixed

Unhook check for dispatching an async request.

Returns

mixed

maybe_dispatch_async_request()

maybe_dispatch_async_request() : mixed

Check if we should dispatch an async request to process actions.

This method is attached to 'shutdown', so is called frequently. To avoid slowing down the site, it mitigates the work performed in each request by:

  1. checking if it's in the admin context and then
  2. haven't run on the 'shutdown' hook within the lock time (60 seconds by default)
  3. haven't exceeded the number of allowed batches.

The order of these checks is important, because they run from a check on a value:

  1. in memory - is_admin() maps to $GLOBALS or the WP_ADMIN constant
  2. in memory - transients use autoloaded options by default
  3. from a database query - has_maximum_concurrent_batches() run the query $this->store->get_claim_count() to find the current number of claims in the DB.

If all of these conditions are met, then we request an async runner check whether it should dispatch a request to process pending actions.

Returns

mixed

add_wp_cron_schedule()

add_wp_cron_schedule(array<string,array<string,int|string>>  $schedules) : array<string,array<string,int|string>>

Add schedule to WP cron.

Parameters

array> $schedules

Schedules.

Returns

array>

schedule_next_instance()

schedule_next_instance(\ActionScheduler_Action  $action, int  $action_id) : mixed

Schedule the next instance of the action if necessary.

Parameters

\ActionScheduler_Action $action

Action.

int $action_id

Action ID.

Returns

mixed

run_cleanup()

run_cleanup() : mixed

Run the queue cleaner.

Returns

mixed

get_time_limit()

get_time_limit() : int

Get the maximum number of seconds a batch can run for.

Returns

int —

The number of seconds.

get_execution_time()

get_execution_time() : int

Get the number of seconds the process has been running.

Returns

int —

The number of seconds.

time_likely_to_be_exceeded()

time_likely_to_be_exceeded(int  $processed_actions) : bool

Check if the host's max execution time is (likely) to be exceeded if processing more actions.

Parameters

int $processed_actions

The number of actions processed so far - used to determine the likelihood of exceeding the time limit if processing another action.

Returns

bool

get_memory_limit()

get_memory_limit() : int

Get memory limit

Based on WP_Background_Process::get_memory_limit()

Returns

int

memory_exceeded()

memory_exceeded() : bool

Memory exceeded

Ensures the batch process never exceeds 90% of the maximum WordPress memory.

Based on WP_Background_Process::memory_exceeded()

Returns

bool

batch_limits_exceeded()

batch_limits_exceeded(int  $processed_actions) : bool

See if the batch limits have been exceeded, which is when memory usage is almost at the maximum limit, or the time to process more actions will exceed the max time limit.

Based on WC_Background_Process::batch_limits_exceeded()

Parameters

int $processed_actions

The number of actions processed so far - used to determine the likelihood of exceeding the time limit if processing another action.

Returns

bool

get_maximum_execution_time()

get_maximum_execution_time() : int

Get the maximum number of seconds a batch can run for.

Returns

int —

The number of seconds.

do_batch()

do_batch(int  $size = 100, string  $context = '') : int

Process a batch of actions pending in the queue.

Actions are processed by claiming a set of pending actions then processing each one until either the batch size is completed, or memory or time limits are reached, defined by @see $this->batch_limits_exceeded().

Parameters

int $size

The maximum number of actions to process in the batch.

string $context

Optional identifier for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' Generally, this should be capitalised and not localised as it's a proper noun.

Returns

int —

The number of actions processed.

clear_caches()

clear_caches() : mixed

Flush the cache if possible (intended for use after a batch of actions has been processed).

This is useful because running large batches can eat up memory and because invalid data can accrue in the runtime cache, which may lead to unexpected results.

Returns

mixed

handle_action_error()

handle_action_error(int  $action_id, \Exception  $e, string  $context, bool  $valid_action) : void

Marks actions as either having failed execution or failed validation, as appropriate.

Parameters

int $action_id

Action ID.

\Exception $e

Exception instance.

string $context

Execution context.

bool $valid_action

If the action is valid.

recurring_action_is_consistently_failing()

recurring_action_is_consistently_failing(\ActionScheduler_Action  $action, int  $action_id) : bool

Determine if the specified recurring action has been consistently failing.

Parameters

\ActionScheduler_Action $action

The recurring action to be rescheduled.

int $action_id

The ID of the recurring action.

Returns

bool