1

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.

5
  • 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. May 12 at 0:51
  • @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() May 12 at 1:15
  • 1
    You should be able to extract what you need from Cron::processQueues()
    – Clive
    May 12 at 3:58
  • 3
    Let's ask: Why you need to queue it when you want it to run immediately?
    – leymannx
    May 12 at 13:18
  • 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. May 14 at 23:35

1 Answer 1

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.