Score:1

How to create a QueueWorker and run it without cron

in flag

Can someone please help me with an example on how to create a QueueWorker class, add items, and run it inside hook_ENTITY_TYPE_presave() or anywhere in a module file?

Basically, what I want is adding an item to the queue and run it soon after a node save, without waiting until the next cron runs.

cn flag
The hook you want is probably `hook_ENTITY_TYPE_insert()`, which runs after successful node save. `hook_ENTITY_TYPE_presave()` is run before save and will be run even if the save fails.
Dynamdilshan avatar
in flag
@PatrickKenny Main thing I'm after is a good example on how to run the queueworker from the hook. I created the QueueWorker and added an item, but, I'm not sure how to programmatically run it from `hook_ENTITY_TYPE_insert()`
cn flag
You should be able to extract what you need from [`Cron::processQueues()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Cron.php/function/Cron%3A%3AprocessQueues/9)
leymannx avatar
ne flag
Let's ask: Why you need to queue it when you want it to run immediately?
Dynamdilshan avatar
in flag
Thanks @Clive it was helpful. Fair question @leymannx. I have some business logic inside `hook_ENTITY_TYPE_presave()` which require me to save a node from one content type when updating another content type node. When I try to save the other node inside the `hook_ENTITY_TYPE_presave()` with `$other_entity->save()` it simply doesn't save it. That's when I thought maybe queueing it and claiming the queue would be the best thing to do.
Score:4
cn flag

What you probably want is a different kind of queue worker. See https://mglaman.dev/blog/flush-and-run-using-kernelterminate-improve-page-speed-performance

A simple example:

/src/EventSubscriber/TaskOnTerminateSubscriber.php

<?php

namespace Drupal\mymodule\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * A subscriber running a task by request
 */
class TaskOnTerminateSubscriber implements EventSubscriberInterface {

  /**
   * TRUE if a task run is requested.
   *
   * @var bool
   */
  protected $runTask = FALSE;

  /**
   * Request a task run
   */
  public function runTask() {
    $this->runTask = TRUE;
  }

  /**
   * Run task if requested.
   *
   * @param \Symfony\Component\HttpKernel\Event\TerminateEvent $event
   *   The Event to process.
   */
  public function onTerminate(TerminateEvent $event) {
    if ($this->runTask) {
      // replace this with your long-running task
      sleep(10);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [KernelEvents::TERMINATE => [['onTerminate', 150]]];
  }

}

mymodule.services.yml

services:
  mymodule.task_on_terminate_subscriber:
    class: Drupal\mymodule\EventSubscriber\TaskOnTerminateSubscriber
    tags:
      - { name: event_subscriber }

Trigger the task in hook_ENTITY_TYPE_presave/insert/update:

\Drupal::service('mymodule.task_on_terminate_subscriber')->runTask();

runTask() could contain arguments, like the entity ID. You can also make the service store multiple tasks in an array class property, which would then be a queue stored in memory processed right after the response is flushed to the client.

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.