Answers.Body: <p>I don't believe there are any events triggered by queue jobs that you can listen for. Feed Me does have an event called <code>afterProcessFeed</code> (more on that <a href="https://docs.craftcms.com/feed-me/v4/developers/events.html#the-afterprocessfeed-event" rel="nofollow noreferrer">here</a>) that's triggered after a feed is processed (i.e. all feed items are completed), but that won't be triggered if the Feed Me queue job fails for some reason.</p>
  <p>If you're worried about a queue job timing out and failing, there are techniques to handle queue jobs running out of memory or execution time and failing. PutYourLightsOn's excellent <a href="https://putyourlightson.com/plugins/campaign" rel="nofollow noreferrer">Campaign</a> plugin takes pains to handle this by allocating as much memory as possible and monitoring both memory and timeout values during the queue job. If the values are exceeded (i.e. the job is about to fail), a new job is scheduled to take over after a delay.</p>
  <p>You can check out how this is done in the Campaign <code>SendoutJob</code> class <a href="https://github.com/putyourlightson/craft-campaign/blob/f3b4b23d3ae9cd59bd1cfdd3a5a31dcf39613fdb/src/jobs/SendoutJob.php" rel="nofollow noreferrer">here</a>. I must credit PYLO's Ben Croker for pointing me in the direction of his code when I was asking similar questions about handling queue job failures.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-11T10:11:30.363Z
Answers.LastActivityDate: 2022-05-11T10:11:30.363Z
Answers.UserDisplayName: Martin Spain
Answers.Body: <p>Something like this might potentially work to catch queue job errors, regardless of what it is. Potential actions for the EVENT_AFTER_ERROR could be releasing and requeuing the job that failed.</p>
  <pre class="lang-php prettyprint-override"><code>Event::on(Queue::class,
       Queue::EVENT_AFTER_ERROR,
       static function(ExecEvent $event) {
           $queue = Craft::$app-&gt;getQueue();
           $queue-&gt;release($event-&gt;id);
           $queue-&gt;delay(30)-&gt;push($event-&gt;job);
       }
  );
  </code></pre>
  <p>Credit to Oliver Stark from Fortrabbit for the concept: <a href="https://gist.github.com/ostark/e0444e3e231cc937a31908b651e845b3" rel="nofollow noreferrer">https://gist.github.com/ostark/e0444e3e231cc937a31908b651e845b3</a>.</p>
  <p>This came out of investigating queue job handling when certain updates or application config changes are made which causes our non-stop queue/listen worker to restart which could occur during a running queue job and cause it to error due to the SIGTERM 15 signal. You might not need the EVENT_WORKER_START event as this is more specific to the environment we have, but EVENT_AFTER_ERROR should give you access to hook into queue jobs that raised an error and do whatever handling you might want.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-25T08:36:30.173Z
Answers.LastActivityDate: 2022-06-25T09:22:42.020Z
Answers.LastEditDate: 2022-06-25T09:22:42.020Z
Answers.UserDisplayName: James White
Body: <p>I have a queue that runs from a cron tab and I'd like to know immediately if it ever fails. Is there an event that fires or any other kind of way to hook into the queue behavior in a module when it runs/completes/fails a job?</p>
  <p>This is specifically a Feed Me queue that I'm talking about, but I'm looking for any kind of help with catching completed/failed queue jobs even if it's not Feed Me specific.</p>
  
Tags: <modules><plugin-feedme><queue>
Title: Catching queue fail event
ThreadText: # Catching queue fail event
  
  (Asked by: username_0 on 2022-05-10)
  
  I have a queue that runs from a cron tab and I'd like to know immediately if it ever fails. Is there an event that fires or any other kind of way to hook into the queue behavior in a module when it runs/completes/fails a job?
  
  This is specifically a Feed Me queue that I'm talking about, but I'm looking for any kind of help with catching completed/failed queue jobs even if it's not Feed Me specific.
  
  **Tags:** modules, plugin-feedme, queue
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-05-11. Score: 2 | Accepted answer
  
  I don't believe there are any events triggered by queue jobs that you can listen for. Feed Me does have an event called `afterProcessFeed` (more on that [here](https://docs.craftcms.com/feed-me/v4/developers/events.html#the-afterprocessfeed-event)) that's triggered after a feed is processed (i.e. all feed items are completed), but that won't be triggered if the Feed Me queue job fails for some reason.
  
  If you're worried about a queue job timing out and failing, there are techniques to handle queue jobs running out of memory or execution time and failing. PutYourLightsOn's excellent [Campaign](https://putyourlightson.com/plugins/campaign) plugin takes pains to handle this by allocating as much memory as possible and monitoring both memory and timeout values during the queue job. If the values are exceeded (i.e. the job is about to fail), a new job is scheduled to take over after a delay.
  
  You can check out how this is done in the Campaign `SendoutJob` class [here](https://github.com/putyourlightson/craft-campaign/blob/f3b4b23d3ae9cd59bd1cfdd3a5a31dcf39613fdb/src/jobs/SendoutJob.php). I must credit PYLO's Ben Croker for pointing me in the direction of his code when I was asking similar questions about handling queue job failures.
  
  
  ---
  
  ### Answer by username_2 on 2022-06-25. Score: 1
  
  Something like this might potentially work to catch queue job errors, regardless of what it is. Potential actions for the EVENT\_AFTER\_ERROR could be releasing and requeuing the job that failed.
  
  ```php
  Event::on(Queue::class,
       Queue::EVENT_AFTER_ERROR,
       static function(ExecEvent $event) {
           $queue = Craft::$app->getQueue();
           $queue->release($event->id);
           $queue->delay(30)->push($event->job);
       }
  );
  
  ```
  
  Credit to Oliver Stark from Fortrabbit for the concept: <https://gist.github.com/ostark/e0444e3e231cc937a31908b651e845b3>.
  
  This came out of investigating queue job handling when certain updates or application config changes are made which causes our non-stop queue/listen worker to restart which could occur during a running queue job and cause it to error due to the SIGTERM 15 signal. You might not need the EVENT\_WORKER\_START event as this is more specific to the environment we have, but EVENT\_AFTER\_ERROR should give you access to hook into queue jobs that raised an error and do whatever handling you might want.
  
  
  ---
  
  