Score:0

How can I delete an item depending on the processing result?

fr flag

I have a queue that is processed while cron run. processItem($data) checks some requirements.

How can I delete an item when some requirements are not met? Can I update an item (the $data array) and "mark" it for deletion in another cron job?

Score:3
us flag

Every item passed to processItem() is automatically deleted once processItem() returns, except in the case processItem() throws an exception. In that case, Drupal will log the exception and leave the item in the queue to be processed again later. If then processItem() throws \Drupal\Core\Queue\SuspendQueueException, Drupal will log the exception, leave the item in the queue, and not use other items from that queue until the next cron run.

See the code in Cron::processQueues().

while ($this->time->getCurrentTime() < $end && ($item = $queue->claimItem($lease_time))) {
  try {
    $queue_worker->processItem($item->data);
    $queue->deleteItem($item);
  } catch (DelayedRequeueException $e) {
    // The worker requested the task not be immediately re-queued.
    // - If the queue doesn't support ::delayItem(), we should leave the
    // item's current expiry time alone.
    // - If the queue does support ::delayItem(), we should allow the
    // queue to update the item's expiry using the requested delay.
    if ($queue instanceof DelayableQueueInterface) {
      // This queue can handle a custom delay; use the duration provided
      // by the exception.
      $queue->delayItem($item, $e->getDelay());
    }
  } catch (RequeueException $e) {
    // The worker requested the task be immediately requeued.
    $queue->releaseItem($item);
  } catch (SuspendQueueException $e) {
    // If the worker indicates there is a problem with the whole queue,
    // release the item and skip to the next queue.
    $queue->releaseItem($item);
    watchdog_exception('cron', $e);

    // Skip to the next queue.
    continue 2;
  } catch (\Exception $e) {
    // In case of any other kind of exception, log it and leave the item
    // in the queue to be processed again later.
    watchdog_exception('cron', $e);
  }
}
I sit in a Tesla and translated this thread with Ai:

mangohost

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.