Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,91 @@ function as_get_datetime_object( $date_string = null, $timezone = 'UTC' ) {
}
return $date;
}

/**
* Run all queued actions for a given hook, arguments, and group.
*
* @param string $hook The hook that the actions are associated with.
* @param array $args Optional. An array of arguments to filter the actions. Default is an empty array.
* @param string $group Optional. The group the actions are assigned to. Default is an empty string.
*/
function as_run_queued_actions($hook, $args = array(), $group = '') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth running the new functions through the linter (composer run phpcs).

if (!ActionScheduler::is_initialized(__FUNCTION__)) {
return;
}

$params = array(
'hook' => $hook,
'status' => ActionScheduler_Store::STATUS_PENDING,
'orderby' => 'date',
'order' => 'ASC',
'group' => $group,
);

if (is_array($args)) {
$params['args'] = $args;
}

$actions = ActionScheduler::store()->query_actions($params);
Comment on lines +509 to +521

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A concern here is that we may pick up claimed actions (once an action is claimed, its status initially remains pending) that are on the verge of being processed by a conventional queue runner, leading to them being processed twice.


$queue_runner = ActionScheduler::runner();

foreach ($actions as $action_id) {
try {
$queue_runner->process_action($action_id);
} catch (Exception $exception) {
ActionScheduler::logger()->log(
$action_id,
sprintf(
'Caught exception while running action "%1$s": %2$s',
$hook,
$exception->getMessage()
)
);
}
}
}

/**
* Run the next queued action for a given hook, arguments, and group.
*
* @param string $hook The hook that the action is associated with.
* @param array $args Optional. An array of arguments to filter the action. Default is an empty array.
* @param string $group Optional. The group the action is assigned to. Default is an empty string.
*/
function as_run_next_queued_action($hook, $args = array(), $group = '') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For both functions, what are your thoughts on a return type? That might be a boolean for this function (because it is only interested in running a single action) or possibly an int for the other function (a count of how many actions were processed)?

if (!ActionScheduler::is_initialized(__FUNCTION__)) {
return;
}

$params = array(
'hook' => $hook,
'status' => ActionScheduler_Store::STATUS_PENDING,
'orderby' => 'date',
'order' => 'ASC',
'group' => $group,
);

if (is_array($args)) {
$params['args'] = $args;
}

$action_id = ActionScheduler::store()->query_action($params);
Comment on lines +553 to +565

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as before (possible race condition here).


if ($action_id) {
$queue_runner = ActionScheduler::runner();

try {
$queue_runner->process_action($action_id);
} catch (Exception $exception) {
ActionScheduler::logger()->log(
$action_id,
sprintf(
'Caught exception while running action "%1$s": %2$s',
$hook,
$exception->getMessage()
)
);
}
}
}