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+
* 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 ##
@ -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.

View File

@ -1,12 +1,34 @@
#!/usr/bin/env 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');
if(empty($QUEUE)) {
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');
if(!empty($REDIS_BACKEND)) {
Resque::setBackend($REDIS_BACKEND);
@ -66,7 +88,7 @@ else {
$queues = explode(',', $QUEUE);
$worker = new Resque_Worker($queues);
$worker->logLevel = $logLevel;
$PIDFILE = getenv('PIDFILE');
if ($PIDFILE) {
file_put_contents($PIDFILE, getmypid()) or

View File

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

2
composer.lock generated
View File

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

View File

@ -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');

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');
}
require '../lib/Resque.php';
require __DIR__ . '/init.php';
date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379');

View File

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

View File

@ -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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,4 @@
<?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
* off the queues, running them and handling the result.