Skip to content

Commit 2f5b489

Browse files
committed
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)
1 parent 8d6da21 commit 2f5b489

File tree

13 files changed

+103
-43
lines changed

13 files changed

+103
-43
lines changed

README.md

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,39 @@ pre and post jobs
3939

4040
* PHP 5.2+
4141
* Redis 2.2+
42+
* Optional but Recommended: Composer
43+
44+
## Getting Started ##
45+
46+
The easiest way to work with php-resque is when it's installed as a
47+
Composer package inside your project. Composer isn't strictly
48+
required, but makes life a lot easier.
49+
50+
If you're not familiar with Composer, please see <http://getcomposer.org/>.
51+
52+
1. Add php-resque to your application's composer.json.
53+
54+
{
55+
...
56+
"require": {
57+
"php": ">=5.3.0"
58+
},
59+
...
60+
}
61+
62+
2. Run `composer install`.
63+
64+
3. If you haven't already, add the Composer autoload to your project's
65+
initialization file. (example)
66+
67+
require 'vendor/autoload.php';
4268

4369
## Jobs ##
4470

4571
### Queueing Jobs ###
4672

4773
Jobs are queued as follows:
4874

49-
require_once 'lib/Resque.php';
50-
5175
// Required if redis is located elsewhere
5276
Resque::setBackend('localhost:6379');
5377

@@ -87,12 +111,12 @@ The `tearDown` method if defined, will be called after the job finishes.
87111
{
88112
// ... Set up environment for this job
89113
}
90-
114+
91115
public function perform()
92116
{
93117
// .. Run job
94118
}
95-
119+
96120
public function tearDown()
97121
{
98122
// ... Remove environment for this job
@@ -136,8 +160,9 @@ class.
136160
Workers work in the exact same way as the Ruby workers. For complete
137161
documentation on workers, see the original documentation.
138162

139-
A basic "up-and-running" resque.php file is included that sets up a
140-
running worker environment is included in the root directory.
163+
A basic "up-and-running" `bin/resque` file is included that sets up a
164+
running worker environment is included. (`vendor/bin/resque` when installed
165+
via Composer)
141166

142167
The exception to the similarities with the Ruby version of resque is
143168
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
146171

147172
To start a worker, it's very similar to the Ruby version:
148173

149-
$ QUEUE=file_serve php resque.php
174+
$ QUEUE=file_serve php bin/resque
150175

151176
It's your responsibility to tell the worker which file to include to get
152177
your application underway. You do so by setting the `APP_INCLUDE` environment
153178
variable:
154179

155-
$ QUEUE=file_serve APP_INCLUDE=../application/init.php php resque.php
180+
$ QUEUE=file_serve APP_INCLUDE=../application/init.php php bin/resque
181+
182+
*Pro tip: Using Composer? More than likely, you don't need to worry about
183+
`APP_INCLUDE`, because hopefully Composer is responsible for autoloading
184+
your application too!*
156185

157186
Getting your application underway also includes telling the worker your job
158187
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.
163192
Setting `VERBOSE` will print basic debugging information and `VVERBOSE`
164193
will print detailed information.
165194

166-
$ VERBOSE QUEUE=file_serve php resque.php
167-
$ VVERBOSE QUEUE=file_serve php resque.php
195+
$ VERBOSE QUEUE=file_serve bin/resque
196+
$ VVERBOSE QUEUE=file_serve bin/resque
168197

169198
### Priorities and Queue Lists ###
170199

@@ -175,7 +204,7 @@ checked in.
175204

176205
As per the original example:
177206

178-
$ QUEUE=file_serve,warm_cache php resque.php
207+
$ QUEUE=file_serve,warm_cache bin/resque
179208

180209
The `file_serve` queue will always be checked for new jobs on each
181210
iteration before the `warm_cache` queue is checked.
@@ -185,14 +214,14 @@ iteration before the `warm_cache` queue is checked.
185214
All queues are supported in the same manner and processed in alphabetical
186215
order:
187216

188-
$ QUEUE=* php resque.php
217+
$ QUEUE=* bin/resque
189218

190219
### Running Multiple Workers ###
191220

192221
Multiple workers ca be launched and automatically worked by supplying
193222
the `COUNT` environment variable:
194223

195-
$ COUNT=5 php resque.php
224+
$ COUNT=5 bin/resque
196225

197226
### Forking ###
198227

@@ -257,7 +286,7 @@ It is up to your application to register event listeners. When enqueuing events
257286
in your application, it should be as easy as making sure php-resque is loaded
258287
and calling `Resque_Event::listen`.
259288

260-
When running workers, if you run workers via the default `resque.php` script,
289+
When running workers, if you run workers via the default `bin/resque` script,
261290
your `APP_INCLUDE` script should initialize and register any listeners required
262291
for operation. If you have rolled your own worker manager, then it is again your
263292
responsibility to register listeners.

resque.php renamed to bin/resque

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1+
#!/usr/bin/env php
12
<?php
3+
4+
// Find and initialize Composer
5+
$files = array(
6+
__DIR__ . '/../../vendor/autoload.php',
7+
__DIR__ . '/../../../../autoload.php',
8+
__DIR__ . '/../vendor/autoload.php',
9+
);
10+
11+
$found = false;
12+
foreach ($files as $file) {
13+
if (file_exists($file)) {
14+
require_once $file;
15+
break;
16+
}
17+
}
18+
19+
if (!class_exists('Composer\Autoload\ClassLoader', false)) {
20+
die(
21+
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
22+
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
23+
'php composer.phar install' . PHP_EOL
24+
);
25+
}
26+
227
$QUEUE = getenv('QUEUE');
328
if(empty($QUEUE)) {
429
die("Set QUEUE env var containing the list of queues to work.\n");
530
}
631

7-
require_once 'lib/Resque.php';
8-
require_once 'lib/Resque/Worker.php';
9-
1032
$REDIS_BACKEND = getenv('REDIS_BACKEND');
1133
if(!empty($REDIS_BACKEND)) {
1234
Resque::setBackend($REDIS_BACKEND);
@@ -66,7 +88,7 @@
6688
$queues = explode(',', $QUEUE);
6789
$worker = new Resque_Worker($queues);
6890
$worker->logLevel = $logLevel;
69-
91+
7092
$PIDFILE = getenv('PIDFILE');
7193
if ($PIDFILE) {
7294
file_put_contents($PIDFILE, getmypid()) or

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"require-dev": {
1818
"phpunit/phpunit": "3.7.*"
1919
},
20+
"bin": [
21+
"bin/resque"
22+
],
2023
"autoload": {
2124
"psr-0": {
2225
"Resque": "lib"

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/check_status.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
die('Specify the ID of a job to monitor the status of.');
44
}
55

6-
require '../lib/Resque/Job/Status.php';
7-
require '../lib/Resque.php';
6+
require __DIR__ . '/init.php';
7+
88
date_default_timezone_set('GMT');
99
Resque::setBackend('127.0.0.1:6379');
1010

demo/init.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
// Find and initialize Composer
3+
// NOTE: You should NOT use this when developing against php-resque.
4+
// The autoload code below is specifically for this demo.
5+
$files = array(
6+
__DIR__ . '/../../vendor/autoload.php',
7+
__DIR__ . '/../../../../autoload.php',
8+
__DIR__ . '/../vendor/autoload.php',
9+
);
10+
11+
$found = false;
12+
foreach ($files as $file) {
13+
if (file_exists($file)) {
14+
require_once $file;
15+
break;
16+
}
17+
}
18+
19+
if (!class_exists('Composer\Autoload\ClassLoader', false)) {
20+
die(
21+
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
22+
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
23+
'php composer.phar install' . PHP_EOL
24+
);
25+
}

demo/queue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
die('Specify the name of a job to add. e.g, php queue.php PHP_Job');
44
}
55

6-
require '../lib/Resque.php';
6+
require __DIR__ . '/init.php';
77
date_default_timezone_set('GMT');
88
Resque::setBackend('127.0.0.1:6379');
99

demo/resque.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
require 'job.php';
55
require 'php_error_job.php';
66

7-
require '../resque.php';
7+
require '../bin/resque';
88
?>

extras/resque.monit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

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

lib/Resque.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<?php
2-
require_once dirname(__FILE__) . '/Resque/Event.php';
3-
require_once dirname(__FILE__) . '/Resque/Exception.php';
4-
52
/**
63
* Base Resque class.
74
*
@@ -75,7 +72,6 @@ public static function redis()
7572
}
7673

7774
if(is_array($server)) {
78-
require_once dirname(__FILE__) . '/Resque/RedisCluster.php';
7975
self::$redis = new Resque_RedisCluster($server);
8076
}
8177
else {
@@ -86,7 +82,6 @@ public static function redis()
8682
$host = $server;
8783
$port = null;
8884
}
89-
require_once dirname(__FILE__) . '/Resque/Redis.php';
9085
self::$redis = new Resque_Redis($host, $port);
9186
}
9287

@@ -148,7 +143,6 @@ public static function size($queue)
148143
*/
149144
public static function enqueue($queue, $class, $args = null, $trackStatus = false)
150145
{
151-
require_once dirname(__FILE__) . '/Resque/Job.php';
152146
$result = Resque_Job::create($queue, $class, $args, $trackStatus);
153147
if ($result) {
154148
Resque_Event::trigger('afterEnqueue', array(
@@ -169,7 +163,6 @@ public static function enqueue($queue, $class, $args = null, $trackStatus = fals
169163
*/
170164
public static function reserve($queue)
171165
{
172-
require_once dirname(__FILE__) . '/Resque/Job.php';
173166
return Resque_Job::reserve($queue);
174167
}
175168

0 commit comments

Comments
 (0)