Using ReactPHP to run Drupal tasks

Parent Feed: 
ReactPHP code

ReactPHP is an event-driven non-blocking PHP framework that allows to you work in a long-running script through an event loop. At its core, ReactPHP provides an event loop and utilities to trigger events at specific intervals and run your code. This is different than normal PHP script execution which of a short lifecycle and per individual requests. 

ReactPHP has been used to build web server applications, web socket servers and more. But, what if we used ReactPHP to execute operations and tasks on a Drupal application?

Technically this could be feasible with a set of cron jobs scheduled at specific intervals which invoke Drush or Drupal Console commands. But there is a limitation there: The ability to manipulate cron jobs entries for the user whenever a deployment occurs. Hosting providers like support this, but only on the main application container. Worker containers do not support cron job definitions. Also, what about handling errors?

We can combine the ReactPHP event loop with the ReactPHP ChildProcess library to run our command line tools. The child process attaches to the event loop and allows us to stream output from STDOUT and STDERR. This allows us to log output from the command or handle errors which occur during these background processes.

It is easiest to create a function that executes a new child process, provides an event loop for it, and binds events to the output streams.  

function run_command(string $command): void {
  $loop = React\EventLoop\Factory::create();
  $process = new React\ChildProcess\Process($command);
  $process->on('exit', function ($exitCode) use ($command) {
    // Trigger alerts that the command finished.
  $process->stdout->on('data', function ($chunk) {
    // Optinally log the output.
  $process->stdout->on('error', function (Exception $e) use ($command) {
    // Log an error.
  $process->stderr->on('data', function ($chunk) use ($command) {
    if (!empty(trim($chunk))) {
      // Log output from stderr
  $process->stderr->on('error', function (Exception $e) use ($command) {
    // Log an error.

I am a big fan of Rollbar and have logs sent there.

Now, let's create our main event loop which will run our commands at different intervals. We can run cron every twenty minutes

$loop = React\EventLoop\Factory::create();
// Run cron every twenty minutes.
$loop->addPeriodicTimer(1200, function () {
  run_command('drush cron');

If you're using a queuing system to process jobs asynchronously via the Advanced queue module, you may want a more continuous processing that acts as a daemon.

$loop = React\EventLoop\Factory::create();
// Every thirty seconds, process jobs from queue1
$loop->addPeriodicTimer(30, function () {
  run_command(sprintf('drush advancedqueue:queue:process queue1'));
// Every two minutes, process jobs from queue2
$loop->addPeriodicTimer(120, function () {
  run_command(sprintf('drush advancedqueue:queue:process queue2'));

This has allowed us to run Drush as a child process in a ReactPHP event loop to run tasks. What if we actually bootstrapped Drupal and invoked our code directly instead of through child processes? We can!

To start, we need to bootstrap Drupal. This requires the creation of a request object and a mocked route. The DrupalKernel and other components are coupled to the request containing some meta information about a route. Luckily, Drupal supports a <none> route.

We require the autoloader and create our request object. I usually have my PHP scripts in a scripts directory at my project root, so my autoloader is in ../vendor/autoload.php.

$autoloader = require __DIR__ . '/../vendor/autoload.php';

$request = Symfony\Component\HttpFoundation\Request::createFromGlobals();
    new Symfony\Component\Routing\Route('<none>')

Next, we bootstrap the DrupalKernel. We need to run the bootEnvironment method, this sets up some required information for Drupal. Next, we need to specify the site path which contains the settings.php we want to use; generally, it is sites/default. Then we just boot the kernel and run the pre-handle of the request to get everything up and running.

$kernel = new Drupal\Core\DrupalKernel('prod', $autoloader);
Drupal\Core\Site\Settings::initialize($kernel->getAppRoot(), $kernel->getSitePath(), $autoloader);

Now we can have our event loop tick away and execute our code as needed.

$loop = React\EventLoop\Factory::create();
$loop->addPeriodicTimer(10, function () {
    $cron = \Drupal::service('cron');

Here is a link to a Github gist of the complete files exampled here:

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web