Queue

Enjoy power of queues on your site! Using the Queue Add-on to schedule jobs frees up your server when users are performing tasks on your site for a faster, and more enjoyable, user experience.

Features

The Queue Add-on changes the way certain events execute on your site so they are executed asynchronously and run independently of the main program flow. They are executed in a non-blocking scheme, allowing the main action to continue processing without unnecessary delays. This not only improves the user experience due to faster processing, but it also means that an error in one of the events won't affect the main program flow. Lets put all of this into a practical example:

When a listing, review or media is submitted there are many actions that take place, other than saving the review in the database. These include:

  • Sending one or more e-mail notifications
  • Posting the review to Twitter
  • Posting the review to the JomSocial or EasySocial streams (Joomla)
  • Triggering native Joomla or WordPress events
  • Tasks generated by other Add-ons (i.e. EngageUsers)

These events are all executed in series, one after the other, and it can amount to a substantial delay. Even if one of these events results in error, then the review for submission could hang and the rest of the events will not be executed.

Requirements

  • PHP PDO extension
  • JReviews v3 or higher
  • A process monitor that can execute queued events. We recommend using Supervisor, a process monitor for Linux that will automatically restart the queue process if it fails.
Processing Tasks via JReviews Cron
The Add-on allows processing queued tasks via cron as an alternative to Supervisor. We still recommend using Supervisor, but if you are unable to set it up, make sure you setup the JReviews Cron to run every minute

Setup

In the Add-on administration you can choose the types of events (emails, Twitter, Facebook, etc.) that should be routed to the queue, instead of running them directly with the main program flow.

Another advantage of running events through the queue is that it is possible to introduce an additional delay before the event is executed. For example, when a listing is submitted and photos are uploaded right after, if the listing is posted to Twitter or Facebook, by introducing a delay for these events, the listing will already have a photo by the time it gets posted to to these social networks.

Supervisor

Installation

Before Installing Supervisor
It is important that you go to the Add-on configuration screen inside JReviews and save it at least once. This will generate a configuration file that is used by Add-on

To install Supervisor on Ubuntu, you need to log in through SSH and run the following command:

sudo apt-get install supervisor

Configuration

Supervisor configuration files are typically stored in the /etc/supervisor/conf.d directory. Within this directory, you may create any number of configuration files that instruct supervisor how your processes should be monitored. For example, let's create a jreviews-worker.conf file that starts and monitors the JReviews queue. If you have several JReviews sites on the same server and want to use the Queue Add-on on all of them, then you should create a separate configuration file for each site and adjust the paths.

[program:jreviews-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/queue-addon/worker-many.php
autostart=true
autorestart=true
numprocs=8
redirect_stderr=true
stderr_logfile=/path/to/folder/below/public/jreviews-worker.err.log
stdout_logfile=/path/to/folder/below/public/jreviews-worker.out.log

In this example, the ''numprocs'' directive will instruct Supervisor to run 8 processes and monitor all of them, automatically restarting them if they fail.

On some hosts, like DigitalOcean, you may need to specify the PHP version to use in the command.

command=php7.0-sp /path/to/queue-addon/worker-many.php

The path to the queue Add-on must be from the server root and looks like this:

Joomla

/path/to/site/components/com_jreviews_addons/queue/worker-many.php

WordPress

/path/to/site/wp-content/plugins/jreviews_addons/queue/worker-many.php

Before you continue, check if the "command" line is correct by executing it directly in the shell. Instead of worker-many.php, use worker-one.php for testing.

php7.0-sp /path/to/queue-addon/worker-one.php

You need to update both the call to "php" and the path based on your server configuration. Once you get that running without errors, then you can replace worker-one.php with worker-many.php and use that as the value for "command" in the config file.

Starting Supervisor

Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start

For more information on Supervisor, consult the Supervisor documentation

Checking If Supervior Is Running

Once you complete all the above steps you can easily check if Supervisor is running by using the supervisorctl command. The result will look something like this.

root@server-name:~# supervisorctl
jreviews-worker:jreviews-worker_00 RUNNING    pid 20543, uptime 66 days, 19:23:30
jreviews-worker:jreviews-worker_01 RUNNING    pid 20542, uptime 66 days, 19:23:30
jreviews-worker:jreviews-worker_02 RUNNING    pid 20545, uptime 66 days, 19:23:30
jreviews-worker:jreviews-worker_03 RUNNING    pid 20544, uptime 66 days, 19:23:30
jreviews-worker:jreviews-worker_04 RUNNING    pid 20539, uptime 66 days, 19:23:30
jreviews-worker:jreviews-worker_05 RUNNING    pid 20538, uptime 66 days, 19:23:30
jreviews-worker:jreviews-worker_06 RUNNING    pid 20541, uptime 66 days, 19:23:30
jreviews-worker:jreviews-worker_07 RUNNING    pid 20540, uptime 66 days, 19:23:30

It shows the name of the program jreviews-worker defined in the configuration file and it also shows the number of processes running (8), also defined in the configuration file through numprocs=8.

Restarting Supervisor After Updates

For Supervisor to reflect any code changes it's necessary to restart it whenever you upgrade JReviews. You can run the following command to do that:

sudo supervisorctl restart all

Failed Jobs

When an event is added to the queue, we call this a job. If a job fails, it will be attempted later. If the maximum number of attempts is exceeded, the job will be moved to the failed queue table. The Add-on administration includes failed jobs page you know if there are issues when processing the queued jobs.