Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue - When UserJob has no more tasks, update status & fire hook #23711

Merged
merged 2 commits into from
Jun 7, 2022

Conversation

totten
Copy link
Member

@totten totten commented Jun 7, 2022

Overview

Suppose you setup a queue with several tasks and then run them all. What happens to the queue's status?

@eileenmcnaughton - This allows the civicrm_queue.status to actually become completed. This serves a couple purposes:

  • On finite/fixed-purpose queues (UserJobs), the queue-status should flip from active to completed so that the background-worker can stop polling for new tasks.
  • It's a step toward allowing jobs that can move between AJAX+background. The issue is that background workers don't (and shouldn't) support the same onEnd callback as AJAX. This provides a safer mechanism (hook_queueStatus) which works in both media.

Before

The civicrm_queue.status always remains active.

After

Depends on the use-case:

  • If you have an open-ended queue providing an on-going service (no UserJob), then the status remains active.
  • If you have a finite/fixed-purpose queue attached to a UserJob, then the status changes from active to completed, and it fires an event:
    function hook_civicrm_queueStatus(CRM_Queue_Queue $queue, string $status)
    

Technical Details

  • Also fixes an incidental bug in setStatus().
  • There are two main ways that items get removed from a queue (Queue.runNext API and civicrm/queue/ajax/runNext). Both of these fire an internal event (civi.queue.check) to consult the status. The nice thing is that it's easy to incorporate this into both AJAX+background workflows.
  • The contact-importer doesn't currently store the queue_id relation, so this doesn't affect statuses on there. For r-run, I used demoqueue, eg
    cv en demoqueue
    cv open civicrm/demo-queue
    cv sql
    ## Periodically run these queries (while job is active; and again after completion)
    ## - select * from civicrm_queue; 
    ## - select id, queue_id, status_id from civicrm_user_job;
    

totten added 2 commits June 7, 2022 02:58
… and fire hook

Suppose you setup a queue with several tasks and then run them all. What happens
to the queue's status?

Before
------

The status always remains `active`.

After
-----

Depends on the use-case:

* If you have an open-ended queue providing an on-going service (no `UserJob`),
  then the status remains `active`.
* If you have a fixed-purpose queue attached to a `UserJob`, then the status
  changes from `active` to `completed`, and it fires an event:
    ```
    function hook_civicrm_queueStatus(CRM_Queue_Queue $queue, string $status)
    ```

Technical Details
-----------------

* There are two main ways that items get removed from a queue (`Queue.runNext`
  API and `civicrm/queue/ajax/runNext`).  Both of these fire an internal event
  (`civi.queue.check`) to consult the status.
@civibot
Copy link

civibot bot commented Jun 7, 2022

(Standard links)

@civibot civibot bot added the master label Jun 7, 2022
@totten totten added the has-test label Jun 7, 2022
@eileenmcnaughton
Copy link
Contributor

I think this seems OK - the full thing will be tested as a thing once it is all working so I'm merging this

@eileenmcnaughton eileenmcnaughton merged commit 32e6b5b into civicrm:master Jun 7, 2022
@totten totten deleted the master-queue-status branch June 14, 2022 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants