New 2 handly functions#1229
Conversation
barryhughes
left a comment
There was a problem hiding this comment.
@Arifursdev sorry for the long delay. Can you confirm if you are still interested in driving this forward? If you are, please review the comments I left.
- My biggest concern with this approach is the potential for a race condition leading to the same action being processed twice (because we skip the claim process/there is no awareness of existing claims).
- New tests would be merited (see
tests/phpunit/procedural_api/). - We'd also want to update the API ref at actionscheduler.org (see
docs/api.md). - I definitely see some utility in these functions, but it would be great to hear about your specific use case, to help us make the right calls before committing this to the public API surface.
If you are no longer interested, no worries—we'll probably close the PR in due course.
| $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); |
There was a problem hiding this comment.
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.
| $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); |
There was a problem hiding this comment.
Same concern as before (possible race condition here).
| * @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 = '') { |
There was a problem hiding this comment.
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)?
| * @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 = '') { |
There was a problem hiding this comment.
Worth running the new functions through the linter (composer run phpcs).
This pull request introduces two new functions to the
functions.phpfile to enhance the functionality of the action scheduler. These functions allow for running all queued actions or the next queued action for a given hook, arguments, and group.New functionality for action scheduler:
as_run_queued_actionsfunction to run all queued actions for a given hook, arguments, and group. This function initializes the action scheduler, queries the actions, and processes each action while handling exceptions.as_run_next_queued_actionfunction to run the next queued action for a given hook, arguments, and group. Similar toas_run_queued_actions, this function initializes the action scheduler, queries the next action, and processes it while handling exceptions.