Skip to content

Proposal: Async (Background) Queue #1952

@ostark

Description

@ostark

First off, using the new yii2-queue is a great improvement!

But the HTTP/ajax based queue is still the default, right? What's wrong with it? In PHP FPM environments, when all PHP FPM processes are blocked by ajax requests, the site can become unresponsive. Fortunately, we can turn off runQueueAutomatically and run the processes in the background.

However, when we trigger craft queue/run by a cron job, the queue handling is delayed a bit. And for craft queue/listen we may use a process monitor to ensure that the queue listener does not stop running.

Proposal to fix this

If we could run a daemonized php process in the background to handle the jobs right after the job was created - without blocking the rendering of the site. For laravel there is a package that does exactly this.

Since there is no package for Yii so far, I wrote a plugin for Craft 3 - just as a proof of concept:
https://github.com/ostark/craft-async-queue

Please let me know what you think. Is this worth a PR here?


This is how the process execution looks like:

Running processes

Empty queue (only php-fpm master is running)

xxx:~$ 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 jobs pushed (php-fpm master + child + bin/php daemon started)

xxx:~$ 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 -v
app       3034  0.0  0.0   4460   784 ?        S    12:31   0:00  \_ sh -c /opt/php/7.1/bin/php craft queue/exec "297" "0" "1" --verbose=1 --color=
app       3035  1.2  0.2 280928 32280 ?        S    12:31   0:00      \_ /opt/php/7.1/bin/php craft queue/exec 297 0 1 --verbose=1 --color=

No requests (only php-fpm master + bin/php daemon still running until queue is empty)

xxx:~$ 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       3033  0.3  0.2 280936 32844 ?        S    12:31   0:00 /usr/bin/php craft queue/run -v
app       3044  0.0  0.0   4460   688 ?        S    12:31   0:00  \_ sh -c /opt/php/7.1/bin/php craft queue/exec "301" "0" "1" --verbose=1 --color=
app       3045  1.1  0.2 280928 32396 ?        S    12:31   0:00      \_ /opt/php/7.1/bin/php craft queue/exec 301 0 1 --verbose=1 --color=

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementimprovements to existing features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions