|
| 1 | +php-resque: PHP Resque Worker (and Enqueue) |
| 2 | +=========================================== |
| 3 | + |
| 4 | +Resque is a Redis-backed library for creating background jobs, placing |
| 5 | +those jobs on multiple queues, and processing them later. |
| 6 | + |
| 7 | +Resque was pioneered and is developed by the fine folks at GitHub (yes, |
| 8 | +I am a kiss-ass), and written in Ruby. |
| 9 | + |
| 10 | +What you're seeing here is an almost direct port of the Resque worker |
| 11 | +and enqueue system to PHP, which I've thrown together because I'm sure |
| 12 | +my PHP developers would have a fit if they had to write a line of Ruby. |
| 13 | + |
| 14 | +For more information on Resque, visit the official GitHub project: |
| 15 | + <http://github.com/defunkt/resque/> |
| 16 | + |
| 17 | +And for background information, the launch post on the GitHub blog: |
| 18 | + <http://github.com/blog/542-introducing-resque> |
| 19 | + |
| 20 | +The PHP port does NOT include its own web interface for viewing queue |
| 21 | +stats, as the data is stored in the exact same expected format as the |
| 22 | +Ruby version of Resque. |
| 23 | + |
| 24 | +The PHP port allows for much the same as the Ruby version of Rescue: |
| 25 | + |
| 26 | +* Workers can be distributed between multiple machines |
| 27 | +* Includes support for priorities (queues) |
| 28 | +* Resilient to memory leaks (fork) |
| 29 | +* Expects failure |
| 30 | + |
| 31 | +In addition, it also: |
| 32 | + |
| 33 | +* Has the ability to track the status of jobs |
| 34 | +* Will mark a job as failed, if a forked child running a job does |
| 35 | +not exit with a status code as 0 |
| 36 | + |
| 37 | +## Jobs ## |
| 38 | + |
| 39 | +### Queueing Jobs ### |
| 40 | + |
| 41 | +Jobs are queued as follows: |
| 42 | + |
| 43 | + require_once 'Resque.php'; |
| 44 | + |
| 45 | + // Required if redis is located elsewhere |
| 46 | + Resque::setBackend('localhost', 6379); |
| 47 | + |
| 48 | + $args = array( |
| 49 | + 'name' => 'Chris' |
| 50 | + ); |
| 51 | + Resque::enqueue('default', 'My_Job', $args); |
| 52 | + |
| 53 | +### Defining Jobs ### |
| 54 | + |
| 55 | +Each job should be in it's own class, and include a `perform` method. |
| 56 | +It's important to note that classes are called statically. |
| 57 | + |
| 58 | + class My_Job |
| 59 | + { |
| 60 | + public static function perform($args) |
| 61 | + { |
| 62 | + // Work work work |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +Any exception thrown by a job will result in the job failing - be |
| 67 | +careful here and make sure you handle the exceptions that shouldn't |
| 68 | +result in a job failing. |
| 69 | + |
| 70 | +### Tracking Job Statuses ### |
| 71 | + |
| 72 | +php-resque has the ability to perform basic status tracking of a queued |
| 73 | +job. The status information will allow you to check if a job is in the |
| 74 | +queue, currently being run, has finished, or failed. |
| 75 | + |
| 76 | +To track the status of a job, pass `true` as the fourth argument to |
| 77 | +`Resque::enqueue`. A token used for tracking the job status will be |
| 78 | +returned: |
| 79 | + |
| 80 | + $token = Resque::enqueue('default', 'My_Job', $args); |
| 81 | + echo $token; |
| 82 | + |
| 83 | +To fetch the status of a job: |
| 84 | + |
| 85 | + $status = new Resque_Job_Status($token); |
| 86 | + echo $status->get(); // Outputs the status |
| 87 | + |
| 88 | +Job statuses are defined as constants in the `Resque_Job_Status` class. |
| 89 | +Valid statuses include: |
| 90 | + |
| 91 | +* `Resque_Job_Status::STATUS_WAITING` - Job is still queued |
| 92 | +* `Resque_Job_Status::STATUS_RUNNING` - Job is currently running |
| 93 | +* `Resque_Job_Status::STATUS_FAILED` - Job has failed |
| 94 | +* `Resque_Job_Status::STATUS_COMPLETE` - Job is complete |
| 95 | +* `false` - Failed to fetch the status - is the token valid? |
| 96 | + |
| 97 | +Statuses are available for up to 24 hours after a job has completed |
| 98 | +or failed, and are then automatically expired. A status can also |
| 99 | +forcefully be expired by calling the `stop()` method on a status |
| 100 | +class. |
| 101 | + |
| 102 | +## Workers ## |
| 103 | + |
| 104 | +Workers work in the exact same way as the Ruby workers. For complete |
| 105 | +documentation on workers, see the original documentation. |
| 106 | + |
| 107 | +A basic "up-and-running" resque.php file is included that sets up a |
| 108 | +running worker environment is included in the root directory. |
| 109 | + |
| 110 | +The exception to the similarities with the Ruby version of resque is |
| 111 | +how a worker is initially setup. To work under all environments, |
| 112 | +not having a single environment such as with Ruby, the PHP port makes |
| 113 | +*no* assumptions about your setup. |
| 114 | + |
| 115 | +To start a worker, it's very similar to the Ruby version: |
| 116 | + |
| 117 | + $ QUEUE=file_serve php resque.php |
| 118 | + |
| 119 | +It's your responsibility to tell the worker which file to include to get |
| 120 | +your application underway. You do so by setting the `APP_INCLUDE` environment |
| 121 | +variable: |
| 122 | + |
| 123 | + $ QUEUE=file_serve APP_INCLUDE=../application/init.php php resque.php |
| 124 | + |
| 125 | +Getting your application underway also includes telling the worker your job |
| 126 | +classes, by means of either an autoloader or including them. |
| 127 | + |
| 128 | +### Logging ### |
| 129 | + |
| 130 | +The port supports the same environment variables for logging to STDOUT. |
| 131 | +Setting `VERBOSE` will print basic debugging information and `VVERBOSE` |
| 132 | +will print detailed information. |
| 133 | + |
| 134 | + $ VERBOSE QUEUE=file_serve php resque.php |
| 135 | + $ VVERBOSE QUEUE=file_serve php resque.php |
| 136 | + |
| 137 | +### Priorities and Queue Lists ### |
| 138 | + |
| 139 | +Similarly, priority and queue list functionality works exactly |
| 140 | +the same as the Ruby workers. Multiple queues should be separated with |
| 141 | +a comma, and the order that they're supplied in is the order that they're |
| 142 | +checked in. |
| 143 | + |
| 144 | +As per the original example: |
| 145 | + |
| 146 | + $ QUEUES=file_serve,warm_cache php resque.php |
| 147 | + |
| 148 | +The `file_serve` queue will always be checked for new jobs on each |
| 149 | +iteration before the `warm_cache` queue is checked. |
| 150 | + |
| 151 | +### Running All Queues ### |
| 152 | + |
| 153 | +All queues are supported in the same manner and processed in alphabetical |
| 154 | +order: |
| 155 | + |
| 156 | + $ QUEUES=* php resque.php |
| 157 | + |
| 158 | +### Running Multiple Workers ### |
| 159 | + |
| 160 | +Multiple workers ca be launched and automatically worked by supplying |
| 161 | +the `COUNT` environment variable: |
| 162 | + |
| 163 | + $ COUNT=5 php resque.php |
| 164 | + |
| 165 | +### Forking ### |
| 166 | + |
| 167 | +Similarly to the Ruby versions, supported platforms will immediately |
| 168 | +fork after picking up a job. The forked child will exit as soon as |
| 169 | +the job finishes. |
| 170 | + |
| 171 | +The difference with php-resque is that if a forked child does not |
| 172 | +exit nicely (PHP error or such), php-resque will automatically fail |
| 173 | +the job. |
| 174 | + |
| 175 | +### Signals ### |
| 176 | + |
| 177 | +Signals also work on supported platforms exactly as in the Ruby |
| 178 | +version of Resque: |
| 179 | + |
| 180 | +* `QUIT` - Wait for child to finish processing then exit |
| 181 | +* `TERM` / `INT` - Immediately kill child then exit |
| 182 | +* `USR1` - Immediately kill child but don't exit |
| 183 | +* `USR2` - Pause worker, no new jobs will be processed |
| 184 | +* `CONT` - Resume worker. |
| 185 | + |
| 186 | +### Process Titles/Statuses ### |
| 187 | + |
| 188 | +The Ruby version of Resque has a nifty feature whereby the process |
| 189 | +title of the worker is updated to indicate what the worker is doing, |
| 190 | +and any forked children also set their process title with the job |
| 191 | +being run. This helps identify running processes on the server and |
| 192 | +their resque status. |
| 193 | + |
| 194 | +**PHP does not have this functionality by default.** |
| 195 | + |
| 196 | +A PECL module (<http://pecl.php.net/package/proctitle>) exists that |
| 197 | +adds this funcitonality to PHP, so if you'd like process titles updated, |
| 198 | +install the PECL module as well. php-resque will detect and use it. |
0 commit comments