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)
This commit is contained in:
Chris Boulton 2013-01-12 22:40:26 +11:00
parent 8d6da21473
commit 2f5b48930f
13 changed files with 103 additions and 43 deletions

View File

@ -39,6 +39,32 @@ pre and post jobs
* PHP 5.2+ * PHP 5.2+
* Redis 2.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 <http://getcomposer.org/>.
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 ## ## Jobs ##
@ -46,8 +72,6 @@ pre and post jobs
Jobs are queued as follows: Jobs are queued as follows:
require_once 'lib/Resque.php';
// Required if redis is located elsewhere // Required if redis is located elsewhere
Resque::setBackend('localhost:6379'); Resque::setBackend('localhost:6379');
@ -136,8 +160,9 @@ class.
Workers work in the exact same way as the Ruby workers. For complete Workers work in the exact same way as the Ruby workers. For complete
documentation on workers, see the original documentation. documentation on workers, see the original documentation.
A basic "up-and-running" resque.php file is included that sets up a A basic "up-and-running" `bin/resque` file is included that sets up a
running worker environment is included in the root directory. running worker environment is included. (`vendor/bin/resque` when installed
via Composer)
The exception to the similarities with the Ruby version of resque is The exception to the similarities with the Ruby version of resque is
how a worker is initially setup. To work under all environments, 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: 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 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 your application underway. You do so by setting the `APP_INCLUDE` environment
variable: 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 Getting your application underway also includes telling the worker your job
classes, by means of either an autoloader or including them. 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` Setting `VERBOSE` will print basic debugging information and `VVERBOSE`
will print detailed information. will print detailed information.
$ VERBOSE QUEUE=file_serve php resque.php $ VERBOSE QUEUE=file_serve bin/resque
$ VVERBOSE QUEUE=file_serve php resque.php $ VVERBOSE QUEUE=file_serve bin/resque
### Priorities and Queue Lists ### ### Priorities and Queue Lists ###
@ -175,7 +204,7 @@ checked in.
As per the original example: 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 The `file_serve` queue will always be checked for new jobs on each
iteration before the `warm_cache` queue is checked. 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 All queues are supported in the same manner and processed in alphabetical
order: order:
$ QUEUE=* php resque.php $ QUEUE=* bin/resque
### Running Multiple Workers ### ### Running Multiple Workers ###
Multiple workers ca be launched and automatically worked by supplying Multiple workers ca be launched and automatically worked by supplying
the `COUNT` environment variable: the `COUNT` environment variable:
$ COUNT=5 php resque.php $ COUNT=5 bin/resque
### Forking ### ### 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 in your application, it should be as easy as making sure php-resque is loaded
and calling `Resque_Event::listen`. 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 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 for operation. If you have rolled your own worker manager, then it is again your
responsibility to register listeners. responsibility to register listeners.

View File

@ -1,12 +1,34 @@
#!/usr/bin/env php
<?php <?php
// Find and initialize Composer
$files = array(
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
);
$found = false;
foreach ($files as $file) {
if (file_exists($file)) {
require_once $file;
break;
}
}
if (!class_exists('Composer\Autoload\ClassLoader', false)) {
die(
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
}
$QUEUE = getenv('QUEUE'); $QUEUE = getenv('QUEUE');
if(empty($QUEUE)) { if(empty($QUEUE)) {
die("Set QUEUE env var containing the list of queues to work.\n"); die("Set QUEUE env var containing the list of queues to work.\n");
} }
require_once 'lib/Resque.php';
require_once 'lib/Resque/Worker.php';
$REDIS_BACKEND = getenv('REDIS_BACKEND'); $REDIS_BACKEND = getenv('REDIS_BACKEND');
if(!empty($REDIS_BACKEND)) { if(!empty($REDIS_BACKEND)) {
Resque::setBackend($REDIS_BACKEND); Resque::setBackend($REDIS_BACKEND);

View File

@ -17,6 +17,9 @@
"require-dev": { "require-dev": {
"phpunit/phpunit": "3.7.*" "phpunit/phpunit": "3.7.*"
}, },
"bin": [
"bin/resque"
],
"autoload": { "autoload": {
"psr-0": { "psr-0": {
"Resque": "lib" "Resque": "lib"

2
composer.lock generated
View File

@ -1,5 +1,5 @@
{ {
"hash": "3df3cf88489d7751f032e8205ebcda7c", "hash": "b05c2c31be6cac834e33b1a7fe61d063",
"packages": [ "packages": [
], ],

View File

@ -3,8 +3,8 @@ if(empty($argv[1])) {
die('Specify the ID of a job to monitor the status of.'); die('Specify the ID of a job to monitor the status of.');
} }
require '../lib/Resque/Job/Status.php'; require __DIR__ . '/init.php';
require '../lib/Resque.php';
date_default_timezone_set('GMT'); date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379'); Resque::setBackend('127.0.0.1:6379');

25
demo/init.php Normal file
View File

@ -0,0 +1,25 @@
<?php
// Find and initialize Composer
// NOTE: You should NOT use this when developing against php-resque.
// The autoload code below is specifically for this demo.
$files = array(
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
);
$found = false;
foreach ($files as $file) {
if (file_exists($file)) {
require_once $file;
break;
}
}
if (!class_exists('Composer\Autoload\ClassLoader', false)) {
die(
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
}

View File

@ -3,7 +3,7 @@ if(empty($argv[1])) {
die('Specify the name of a job to add. e.g, php queue.php PHP_Job'); die('Specify the name of a job to add. e.g, php queue.php PHP_Job');
} }
require '../lib/Resque.php'; require __DIR__ . '/init.php';
date_default_timezone_set('GMT'); date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379'); Resque::setBackend('127.0.0.1:6379');

View File

@ -4,5 +4,5 @@ require 'bad_job.php';
require 'job.php'; require 'job.php';
require 'php_error_job.php'; require 'php_error_job.php';
require '../resque.php'; require '../bin/resque';
?> ?>

View File

@ -9,7 +9,7 @@
check process resque_worker_[QUEUE] check process resque_worker_[QUEUE]
with pidfile /var/run/resque/worker_[QUEUE].pid 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;'" 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? if totalmem is greater than 300 MB for 10 cycles then restart # eating up memory?
group resque_workers group resque_workers

View File

@ -1,7 +1,4 @@
<?php <?php
require_once dirname(__FILE__) . '/Resque/Event.php';
require_once dirname(__FILE__) . '/Resque/Exception.php';
/** /**
* Base Resque class. * Base Resque class.
* *
@ -75,7 +72,6 @@ class Resque
} }
if(is_array($server)) { if(is_array($server)) {
require_once dirname(__FILE__) . '/Resque/RedisCluster.php';
self::$redis = new Resque_RedisCluster($server); self::$redis = new Resque_RedisCluster($server);
} }
else { else {
@ -86,7 +82,6 @@ class Resque
$host = $server; $host = $server;
$port = null; $port = null;
} }
require_once dirname(__FILE__) . '/Resque/Redis.php';
self::$redis = new Resque_Redis($host, $port); self::$redis = new Resque_Redis($host, $port);
} }
@ -148,7 +143,6 @@ class Resque
*/ */
public static function enqueue($queue, $class, $args = null, $trackStatus = false) public static function enqueue($queue, $class, $args = null, $trackStatus = false)
{ {
require_once dirname(__FILE__) . '/Resque/Job.php';
$result = Resque_Job::create($queue, $class, $args, $trackStatus); $result = Resque_Job::create($queue, $class, $args, $trackStatus);
if ($result) { if ($result) {
Resque_Event::trigger('afterEnqueue', array( Resque_Event::trigger('afterEnqueue', array(
@ -169,7 +163,6 @@ class Resque
*/ */
public static function reserve($queue) public static function reserve($queue)
{ {
require_once dirname(__FILE__) . '/Resque/Job.php';
return Resque_Job::reserve($queue); return Resque_Job::reserve($queue);
} }

View File

@ -1,5 +1,4 @@
<?php <?php
require_once dirname(__FILE__) . '/Failure/Interface.php';
/** /**
* Failed Resque job. * Failed Resque job.
@ -37,7 +36,6 @@ class Resque_Failure
public static function getBackend() public static function getBackend()
{ {
if(self::$backend === null) { if(self::$backend === null) {
require dirname(__FILE__) . '/Failure/Redis.php';
self::$backend = 'Resque_Failure_Redis'; self::$backend = 'Resque_Failure_Redis';
} }

View File

@ -1,8 +1,4 @@
<?php <?php
require_once dirname(__FILE__) . '/Event.php';
require_once dirname(__FILE__) . '/Job/Status.php';
require_once dirname(__FILE__) . '/Job/DontPerform.php';
/** /**
* Resque job. * Resque job.
* *
@ -208,7 +204,6 @@ class Resque_Job
)); ));
$this->updateStatus(Resque_Job_Status::STATUS_FAILED); $this->updateStatus(Resque_Job_Status::STATUS_FAILED);
require_once dirname(__FILE__) . '/Failure.php';
Resque_Failure::create( Resque_Failure::create(
$this->payload, $this->payload,
$exception, $exception,

View File

@ -1,9 +1,4 @@
<?php <?php
require_once dirname(__FILE__) . '/Stat.php';
require_once dirname(__FILE__) . '/Event.php';
require_once dirname(__FILE__) . '/Job.php';
require_once dirname(__FILE__) . '/Job/DirtyExitException.php';
/** /**
* Resque worker that handles checking queues for jobs, fetching them * Resque worker that handles checking queues for jobs, fetching them
* off the queues, running them and handling the result. * off the queues, running them and handling the result.