From 2f5b48930f7fcc51931d608b2d37897a350a7eea Mon Sep 17 00:00:00 2001 From: Chris Boulton Date: Sat, 12 Jan 2013 22:40:26 +1100 Subject: [PATCH] make better use of composer across php-resque * recommend php-resque be installed via Composer * provide quick getting started steps * move ./resque.php to bin/resque, make it available as a Composer bin * have classes autoloaded via Composer (or some other means if not using Composer) --- README.md | 57 ++++++++++++++++++++++++++++++---------- resque.php => bin/resque | 30 ++++++++++++++++++--- composer.json | 3 +++ composer.lock | 2 +- demo/check_status.php | 4 +-- demo/init.php | 25 ++++++++++++++++++ demo/queue.php | 2 +- demo/resque.php | 2 +- extras/resque.monit | 2 +- lib/Resque.php | 7 ----- lib/Resque/Failure.php | 2 -- lib/Resque/Job.php | 5 ---- lib/Resque/Worker.php | 5 ---- 13 files changed, 103 insertions(+), 43 deletions(-) rename resque.php => bin/resque (74%) create mode 100644 demo/init.php diff --git a/README.md b/README.md index afc8956..231aeba 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,32 @@ pre and post jobs * PHP 5.2+ * Redis 2.2+ +* Optional but Recommended: Composer + +## Getting Started ## + +The easiest way to work with php-resque is when it's installed as a +Composer package inside your project. Composer isn't strictly +required, but makes life a lot easier. + +If you're not familiar with Composer, please see . + +1. Add php-resque to your application's composer.json. + + { + ... + "require": { + "php": ">=5.3.0" + }, + ... + } + +2. Run `composer install`. + +3. If you haven't already, add the Composer autoload to your project's + initialization file. (example) + + require 'vendor/autoload.php'; ## Jobs ## @@ -46,8 +72,6 @@ pre and post jobs Jobs are queued as follows: - require_once 'lib/Resque.php'; - // Required if redis is located elsewhere Resque::setBackend('localhost:6379'); @@ -87,12 +111,12 @@ The `tearDown` method if defined, will be called after the job finishes. { // ... Set up environment for this job } - + public function perform() { // .. Run job } - + public function tearDown() { // ... Remove environment for this job @@ -136,8 +160,9 @@ class. Workers work in the exact same way as the Ruby workers. For complete documentation on workers, see the original documentation. -A basic "up-and-running" resque.php file is included that sets up a -running worker environment is included in the root directory. +A basic "up-and-running" `bin/resque` file is included that sets up a +running worker environment is included. (`vendor/bin/resque` when installed +via Composer) The exception to the similarities with the Ruby version of resque is how a worker is initially setup. To work under all environments, @@ -146,13 +171,17 @@ not having a single environment such as with Ruby, the PHP port makes To start a worker, it's very similar to the Ruby version: - $ QUEUE=file_serve php resque.php + $ QUEUE=file_serve php bin/resque It's your responsibility to tell the worker which file to include to get your application underway. You do so by setting the `APP_INCLUDE` environment variable: - $ QUEUE=file_serve APP_INCLUDE=../application/init.php php resque.php + $ QUEUE=file_serve APP_INCLUDE=../application/init.php php bin/resque + +*Pro tip: Using Composer? More than likely, you don't need to worry about +`APP_INCLUDE`, because hopefully Composer is responsible for autoloading +your application too!* Getting your application underway also includes telling the worker your job classes, by means of either an autoloader or including them. @@ -163,8 +192,8 @@ The port supports the same environment variables for logging to STDOUT. Setting `VERBOSE` will print basic debugging information and `VVERBOSE` will print detailed information. - $ VERBOSE QUEUE=file_serve php resque.php - $ VVERBOSE QUEUE=file_serve php resque.php + $ VERBOSE QUEUE=file_serve bin/resque + $ VVERBOSE QUEUE=file_serve bin/resque ### Priorities and Queue Lists ### @@ -175,7 +204,7 @@ checked in. As per the original example: - $ QUEUE=file_serve,warm_cache php resque.php + $ QUEUE=file_serve,warm_cache bin/resque The `file_serve` queue will always be checked for new jobs on each iteration before the `warm_cache` queue is checked. @@ -185,14 +214,14 @@ iteration before the `warm_cache` queue is checked. All queues are supported in the same manner and processed in alphabetical order: - $ QUEUE=* php resque.php + $ QUEUE=* bin/resque ### Running Multiple Workers ### Multiple workers ca be launched and automatically worked by supplying the `COUNT` environment variable: - $ COUNT=5 php resque.php + $ COUNT=5 bin/resque ### Forking ### @@ -257,7 +286,7 @@ It is up to your application to register event listeners. When enqueuing events in your application, it should be as easy as making sure php-resque is loaded and calling `Resque_Event::listen`. -When running workers, if you run workers via the default `resque.php` script, +When running workers, if you run workers via the default `bin/resque` script, your `APP_INCLUDE` script should initialize and register any listeners required for operation. If you have rolled your own worker manager, then it is again your responsibility to register listeners. diff --git a/resque.php b/bin/resque similarity index 74% rename from resque.php rename to bin/resque index 02ecc1a..687f6c4 100644 --- a/resque.php +++ b/bin/resque @@ -1,12 +1,34 @@ +#!/usr/bin/env php logLevel = $logLevel; - + $PIDFILE = getenv('PIDFILE'); if ($PIDFILE) { file_put_contents($PIDFILE, getmypid()) or diff --git a/composer.json b/composer.json index 79d3840..a3880ae 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,9 @@ "require-dev": { "phpunit/phpunit": "3.7.*" }, + "bin": [ + "bin/resque" + ], "autoload": { "psr-0": { "Resque": "lib" diff --git a/composer.lock b/composer.lock index 64c1bce..91301f7 100644 --- a/composer.lock +++ b/composer.lock @@ -1,5 +1,5 @@ { - "hash": "3df3cf88489d7751f032e8205ebcda7c", + "hash": "b05c2c31be6cac834e33b1a7fe61d063", "packages": [ ], diff --git a/demo/check_status.php b/demo/check_status.php index c195911..061a83a 100644 --- a/demo/check_status.php +++ b/demo/check_status.php @@ -3,8 +3,8 @@ if(empty($argv[1])) { die('Specify the ID of a job to monitor the status of.'); } -require '../lib/Resque/Job/Status.php'; -require '../lib/Resque.php'; +require __DIR__ . '/init.php'; + date_default_timezone_set('GMT'); Resque::setBackend('127.0.0.1:6379'); diff --git a/demo/init.php b/demo/init.php new file mode 100644 index 0000000..9078bcd --- /dev/null +++ b/demo/init.php @@ -0,0 +1,25 @@ + \ No newline at end of file diff --git a/extras/resque.monit b/extras/resque.monit index 654815d..b611f8f 100644 --- a/extras/resque.monit +++ b/extras/resque.monit @@ -9,7 +9,7 @@ check process resque_worker_[QUEUE] with pidfile /var/run/resque/worker_[QUEUE].pid - start program = "/bin/sh -c 'APP_INCLUDE=[APP_INCLUDE] QUEUE=[QUEUE] VERBOSE=1 PIDFILE=/var/run/resque/worker_[QUEUE].pid nohup php -f [PATH/TO/RESQUE]/resque.php > /var/log/resque/worker_[QUEUE].log &'" as uid [UID] and gid [GID] + start program = "/bin/sh -c 'APP_INCLUDE=[APP_INCLUDE] QUEUE=[QUEUE] VERBOSE=1 PIDFILE=/var/run/resque/worker_[QUEUE].pid nohup php -f [PATH/TO/RESQUE]/bin/resque > /var/log/resque/worker_[QUEUE].log &'" as uid [UID] and gid [GID] stop program = "/bin/sh -c 'kill -s QUIT `cat /var/run/resque/worker_[QUEUE].pid` && rm -f /var/run/resque/worker_[QUEUE].pid; exit 0;'" if totalmem is greater than 300 MB for 10 cycles then restart # eating up memory? group resque_workers \ No newline at end of file diff --git a/lib/Resque.php b/lib/Resque.php index a3150cb..c3254a3 100644 --- a/lib/Resque.php +++ b/lib/Resque.php @@ -1,7 +1,4 @@ updateStatus(Resque_Job_Status::STATUS_FAILED); - require_once dirname(__FILE__) . '/Failure.php'; Resque_Failure::create( $this->payload, $exception, diff --git a/lib/Resque/Worker.php b/lib/Resque/Worker.php index 5aa1ccd..d103810 100644 --- a/lib/Resque/Worker.php +++ b/lib/Resque/Worker.php @@ -1,9 +1,4 @@