PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/README.markdown

https://github.com/d11wtq/php-resque
Markdown | 332 lines | 229 code | 103 blank | 0 comment | 0 complexity | 6d6ddda3266ac3cd773d7425b760e6aa MD5 | raw file
  1. (This is currently just a personal fork with some fixes for personal
  2. projects. I aim to push changes back to the main project, which you
  3. should use instead of this one.)
  4. php-resque: PHP Resque Worker (and Enqueue)
  5. ===========================================
  6. Resque is a Redis-backed library for creating background jobs, placing
  7. those jobs on multiple queues, and processing them later.
  8. Resque was pioneered and is developed by the fine folks at GitHub (yes,
  9. I am a kiss-ass), and written in Ruby.
  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. For more information on Resque, visit the official GitHub project:
  14. <http://github.com/defunkt/resque/>
  15. And for background information, the launch post on the GitHub blog:
  16. <http://github.com/blog/542-introducing-resque>
  17. The PHP port does NOT include its own web interface for viewing queue
  18. stats, as the data is stored in the exact same expected format as the
  19. Ruby version of Resque.
  20. The PHP port allows for much the same as the Ruby version of Rescue:
  21. * Workers can be distributed between multiple machines
  22. * Includes support for priorities (queues)
  23. * Resilient to memory leaks (fork)
  24. * Expects failure
  25. In addition, it also:
  26. * Has the ability to track the status of jobs
  27. * Will mark a job as failed, if a forked child running a job does
  28. not exit with a status code as 0
  29. * Has built in support for `setUp` and `tearDown` methods, called
  30. pre and post jobs
  31. Note: php-resque requires at least Redis 2.2.
  32. ## Jobs ##
  33. ### Queueing Jobs ###
  34. Jobs are queued as follows:
  35. require_once 'lib/Resque.php';
  36. // Required if redis is located elsewhere
  37. Resque::setBackend('localhost:6379');
  38. $args = array(
  39. 'name' => 'Chris'
  40. );
  41. Resque::enqueue('default', 'My_Job', $args);
  42. ### Defining Jobs ###
  43. Each job should be in it's own class, and include a `perform` method.
  44. class My_Job
  45. {
  46. public function perform()
  47. {
  48. // Work work work
  49. echo $this->args['name'];
  50. }
  51. }
  52. When the job is run, the class will be instantiated and any arguments
  53. will be set as an array on the instantiated object, and are accessible
  54. via `$this->args`.
  55. Any exception thrown by a job will result in the job failing - be
  56. careful here and make sure you handle the exceptions that shouldn't
  57. result in a job failing.
  58. Jobs can also have `setUp` and `tearDown` methods. If a `setUp` method
  59. is defined, it will be called before the `perform` method is run.
  60. The `tearDown` method if defined, will be called after the job finishes.
  61. class My_Job
  62. {
  63. public function setUp()
  64. {
  65. // ... Set up environment for this job
  66. }
  67. public function perform()
  68. {
  69. // .. Run job
  70. }
  71. public function tearDown()
  72. {
  73. // ... Remove environment for this job
  74. }
  75. }
  76. ### Tracking Job Statuses ###
  77. php-resque has the ability to perform basic status tracking of a queued
  78. job. The status information will allow you to check if a job is in the
  79. queue, currently being run, has finished, or failed.
  80. To track the status of a job, pass `true` as the fourth argument to
  81. `Resque::enqueue`. A token used for tracking the job status will be
  82. returned:
  83. $token = Resque::enqueue('default', 'My_Job', $args, true);
  84. echo $token;
  85. To fetch the status of a job:
  86. $status = new Resque_Job_Status($token);
  87. echo $status->get(); // Outputs the status
  88. Job statuses are defined as constants in the `Resque_Job_Status` class.
  89. Valid statuses include:
  90. * `Resque_Job_Status::STATUS_WAITING` - Job is still queued
  91. * `Resque_Job_Status::STATUS_RUNNING` - Job is currently running
  92. * `Resque_Job_Status::STATUS_FAILED` - Job has failed
  93. * `Resque_Job_Status::STATUS_COMPLETE` - Job is complete
  94. * `false` - Failed to fetch the status - is the token valid?
  95. Statuses are available for up to 24 hours after a job has completed
  96. or failed, and are then automatically expired. A status can also
  97. forcefully be expired by calling the `stop()` method on a status
  98. class.
  99. ## Workers ##
  100. Workers work in the exact same way as the Ruby workers. For complete
  101. documentation on workers, see the original documentation.
  102. A basic "up-and-running" resque.php file is included that sets up a
  103. running worker environment is included in the root directory.
  104. The exception to the similarities with the Ruby version of resque is
  105. how a worker is initially setup. To work under all environments,
  106. not having a single environment such as with Ruby, the PHP port makes
  107. *no* assumptions about your setup.
  108. To start a worker, it's very similar to the Ruby version:
  109. $ QUEUE=file_serve php resque.php
  110. It's your responsibility to tell the worker which file to include to get
  111. your application underway. You do so by setting the `APP_INCLUDE` environment
  112. variable:
  113. $ QUEUE=file_serve APP_INCLUDE=../application/init.php php resque.php
  114. Getting your application underway also includes telling the worker your job
  115. classes, by means of either an autoloader or including them.
  116. ### Logging ###
  117. The port supports the same environment variables for logging to STDOUT.
  118. Setting `VERBOSE` will print basic debugging information and `VVERBOSE`
  119. will print detailed information.
  120. $ VERBOSE QUEUE=file_serve php resque.php
  121. $ VVERBOSE QUEUE=file_serve php resque.php
  122. ### Priorities and Queue Lists ###
  123. Similarly, priority and queue list functionality works exactly
  124. the same as the Ruby workers. Multiple queues should be separated with
  125. a comma, and the order that they're supplied in is the order that they're
  126. checked in.
  127. As per the original example:
  128. $ QUEUE=file_serve,warm_cache php resque.php
  129. The `file_serve` queue will always be checked for new jobs on each
  130. iteration before the `warm_cache` queue is checked.
  131. ### Running All Queues ###
  132. All queues are supported in the same manner and processed in alphabetical
  133. order:
  134. $ QUEUE=* php resque.php
  135. ### Running Multiple Workers ###
  136. Multiple workers ca be launched and automatically worked by supplying
  137. the `COUNT` environment variable:
  138. $ COUNT=5 php resque.php
  139. ### Forking ###
  140. Similarly to the Ruby versions, supported platforms will immediately
  141. fork after picking up a job. The forked child will exit as soon as
  142. the job finishes.
  143. The difference with php-resque is that if a forked child does not
  144. exit nicely (PHP error or such), php-resque will automatically fail
  145. the job.
  146. ### Signals ###
  147. Signals also work on supported platforms exactly as in the Ruby
  148. version of Resque:
  149. * `QUIT` - Wait for child to finish processing then exit
  150. * `TERM` / `INT` - Immediately kill child then exit
  151. * `USR1` - Immediately kill child but don't exit
  152. * `USR2` - Pause worker, no new jobs will be processed
  153. * `CONT` - Resume worker.
  154. ### Process Titles/Statuses ###
  155. The Ruby version of Resque has a nifty feature whereby the process
  156. title of the worker is updated to indicate what the worker is doing,
  157. and any forked children also set their process title with the job
  158. being run. This helps identify running processes on the server and
  159. their resque status.
  160. **PHP does not have this functionality by default.**
  161. A PECL module (<http://pecl.php.net/package/proctitle>) exists that
  162. adds this funcitonality to PHP, so if you'd like process titles updated,
  163. install the PECL module as well. php-resque will detect and use it.
  164. ## Event/Hook System ##
  165. php-resque has a basic event system that can be used by your application
  166. to customize how some of the php-resque internals behave.
  167. You listen in on events (as listed below) by registering with `Resque_Event`
  168. and supplying a callback that you would like triggered when the event is
  169. raised:
  170. Resque_Event::listen('eventName', [callback]);
  171. `[callback]` may be anything in PHP that is callable by `call_user_func_array`:
  172. * A string with the name of a function
  173. * An array containing an object and method to call
  174. * An array containing an object and a static method to call
  175. * A closure (PHP 5.3)
  176. Events may pass arguments (documented below), so your callback should accept
  177. these arguments.
  178. You can stop listening to an event by calling `Resque_Event::stopListening`
  179. with the same arguments supplied to `Resque_Event::listen`.
  180. It is up to your application to register event listeners. When enqueuing events
  181. in your application, it should be as easy as making sure php-resque is loaded
  182. and calling `Resque_Event::listen`.
  183. When running workers, if you run workers via the default `resque.php` script,
  184. your `APP_INCLUDE` script should initialize and register any listeners required
  185. for operation. If you have rolled your own worker manager, then it is again your
  186. responsibility to register listeners.
  187. A sample plugin is included in the `extras` directory.
  188. ### Events ###
  189. #### beforeFirstFork ####
  190. Called once, as a worker initializes. Argument passed is the instance of `Resque_Worker`
  191. that was just initialized.
  192. #### beforeFork ####
  193. Called before php-resque forks to run a job. Argument passed contains the instance of
  194. `Resque_Job` for the job about to be run.
  195. `beforeFork` is triggered in the **parent** process. Any changes made will be permanent
  196. for as long as the worker lives.
  197. #### afterFork ####
  198. Called after php-resque forks to run a job (but before the job is run). Argument
  199. passed contains the instance of `Resque_Job` for the job about to be run.
  200. `afterFork` is triggered in the child process after forking out to complete a job. Any
  201. changes made will only live as long as the job is being processed.
  202. #### beforePerform ####
  203. Called before the `setUp` and `perform` methods on a job are run. Argument passed
  204. contains the instance of `Resque_Job` about for the job about to be run.
  205. You can prevent execution of the job by throwing an exception of `Resque_Job_DontPerform`.
  206. Any other exceptions thrown will be treated as if they were thrown in a job, causing the
  207. job to fail.
  208. #### afterPerform ####
  209. Called after the `perform` and `tearDown` methods on a job are run. Argument passed
  210. contains the instance of `Resque_Job` that was just run.
  211. Any exceptions thrown will be treated as if they were thrown in a job, causing the job
  212. to be marked as having failed.
  213. #### onFailure ####
  214. Called whenever a job fails. Arguments passed (in this order) include:
  215. * Exception - The exception that was thrown when the job failed
  216. * Resque_Job - The job that failed
  217. #### afterEnqueue ####
  218. Called after a job has been queued using the `Resque::enqueue` method. Arguments passed
  219. (in this order) include:
  220. * Class - string containing the name of the class the job was scheduled in
  221. * Arguments - array of arguments supplied to the job
  222. ## Contributors ##
  223. * chrisboulton
  224. * thedotedge
  225. * hobodave
  226. * scraton
  227. * KevBurnsJr
  228. * jmathai
  229. * dceballos