Skip to content

Commit

Permalink
Updated readme. + is not needed as we run queue/run instead of queue/…
Browse files Browse the repository at this point in the history
…exec
  • Loading branch information
Oliver Stark committed Oct 16, 2017
1 parent 4cd0f06 commit fc10460
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
# Async (Background) Queue

Inpired by this discussion
https://github.com/craftcms/cms/issues/1952
With Craft's job queue you can run heavy tasks in the background. Unfortunately, this is not entirely true, when `runQueueAutomatically => true` (default), the job queue is handled by a ajax (FPM) call.
With many jobs in the queue and limited PHP-FPM processes this break your site.

This plugin replaces Craft's default queue handler and moves queue execution to a non-blocking background process.
The command `craft queue/run` gets executed right after you push a Job to the queue`.

[Here](https://github.com/craftcms/cms/issues/1952) you can find the initial discussion I started at `craftcms/cms`.

## Installation

1. Install with Composer via `composer require ostark/craft-async-queue` from your project directory
2. Install plugin with this command `php ./craft install/plugin async-queue` or in the Craft Control Panel under Settings > Plugins

## Configuration (optional)

The plugin expects the `php` binary in `/usr/bin/`. If the binary is located somewhere else, you can overwrite the default with the `PATH_PHP_BINARY ENV var, e.g. in your .env file like this:
```
PATH_PHP_BINARY=/usr/local/Cellar/php71/7.1.0_11/bin/php'
```

## Under the hood: Process list

**Empty queue** (only php-fpm master is running)
```
$ ps auxf | grep php
root 2953 0.0 0.0 399552 13520 ? Ss 12:27 0:00 php-fpm: master process (/etc/php/fpm.conf)
````

**New job pushed** (php-fpm master + child + /usr/bin/php daemon started)
```
$ ps auxf | grep php
root 2953 0.0 0.0 399552 13520 ? Ss 12:27 0:00 php-fpm: master process (/etc/php/fpm.conf)
app 3031 2.2 0.2 718520 45992 ? S 12:31 0:00 \_ php-fpm: pool www
app 3033 1.2 0.2 280936 32808 ? S 12:31 0:00 /usr/bin/php craft queue/run
app 3034 0.0 0.0 4460 784 ? S 12:31 0:00 \_ sh -c /usr/bin/php craft queue/exec "1234" "0" "1"
app 3035 1.2 0.2 280928 32280 ? S 12:31 0:00 \_ /usr/bin/php craft queue/exec 1234 0 1
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ostark/craft-async-queue",
"description": "A queue handler that moves queue execution to a non-blocking background process",
"type": "craft-plugin",
"version": "1.1.0",
"version": "1.1.1",
"keywords": [
"craft",
"cms",
Expand Down
26 changes: 10 additions & 16 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@


/**
* Craft plugins are very much like little applications in and of themselves. We’ve made
* it as simple as we can, but the training wheels are off. A little prior knowledge is
* going to be required to write a plugin.
*
* For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL,
* as well as some semi-advanced concepts like object-oriented programming and PHP namespaces.
*
* https://craftcms.com/docs/plugins/introduction
* AsyncQueue
*
* @author Oliver Stark
* @package AsyncQueue
Expand All @@ -49,36 +42,37 @@ public function init()
Craft::$app->getConfig()->getGeneral()->runQueueAutomatically = false;

// Run queue in the background
$this->startBackgroundProcess((string)$event->id);
$this->startBackgroundProcess();
});

}


/**
* @param string $id
* Runs craft queue/run in the background
*/
protected function startBackgroundProcess(string $id)
protected function startBackgroundProcess()
{
$process = new Process($this->getCommand($id), CRAFT_BASE_PATH);
$process = new Process($this->getCommand(), CRAFT_BASE_PATH);
$process->run();
}


/**
* Construct queue command
*
* @param string $id
*
* @return string
*/
protected function getCommand(string $id): string
protected function getCommand(): string
{
$cmd = "%s craft queue/run --verbose=1";
$cmd = "%s craft queue/run";
$cmd = $this->getBackgroundCommand($cmd);
$binary = getenv('PATH_PHP_BINARY') ?? '/usr/bin/php';

return sprintf($cmd, $binary);
}


/**
* Extend command with background syntax
*
Expand Down

0 comments on commit fc10460

Please sign in to comment.