Advanced queue module: How to stop processing queue in cron, when some conditions apply in job event/job preprocessing?

fr flag

I have a custom module handling jobs from the Advanced Queue module. To limit the number of jobs allowed to be processed in one cron run, I have some configuration variables in my custom module. With an event listener my function gets called correctly, when the queue is processed.

How can I fetch the current running process of the queue and stop it?

public static function getSubscribedEvents() {
  $events[AdvancedQueueEvents::PRE_PROCESS][] = ['checkLimits'];
  return $events;

public function checkLimits(JobEvent $event) {
  $job_type = $event->getJob()->getType();
  if ($job_type === 'MYMODULE_send_reports') {
    $config = \Drupal::configFactory()->getEditable('MYMODULE.settings');
    if (!empty($config)) {
      $number_of_max_emails_to_send_per_day  = $config->get('number_of_max_emails_to_send_per_day');
      $number_of_max_emails_to_send_per_hour = $config->get('number_of_max_emails_to_send_per_hour');
      $remaining_emails_day = $config->get('remaining_emails_day');
      $remaining_emails_hour = $config->get('remaining_emails_hour');

      if ($remaining_emails_day <= 0) {
        $queue = Queue::load($event->getJob()->getQueueId());

        // $processor_id is only a string like "cron".
        $processor_id = $queue->getProcessor();

id flag
It seems like an open feature request:

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.