Skip to content

Commit 54966de

Browse files
authored
Introduce Namespaces (#56)
* Introduce namespaces * Rename Resque\Exceptions\Exception to Resque\Exceptions\ResqueException * Fix style of use declarations * Fix style of catch statement in JobHandler * Update documentation with new class names * Update sample-plugin.php for new class names * Small copy-paste fix in RedisTest * Remove no longer needed autoloader configuration for test classes
1 parent 2a6a4a5 commit 54966de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+707
-526
lines changed

HOWITWORKS.md

Lines changed: 87 additions & 87 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ Jobs are queued as follows:
9797

9898
```php
9999
// Required if redis is located elsewhere
100-
Resque::setBackend('localhost:6379');
100+
Resque\Resque::setBackend('localhost:6379');
101101

102102
$args = array(
103103
'name' => 'Chris'
104104
);
105-
Resque::enqueue('default', 'My_Job', $args);
105+
Resque\Resque::enqueue('default', 'My_Job', $args);
106106
```
107107

108108
### Defining Jobs
@@ -157,24 +157,24 @@ This method can be used to conveniently remove a job from a queue.
157157

158158
```php
159159
// Removes job class 'My_Job' of queue 'default'
160-
Resque::dequeue('default', ['My_Job']);
160+
Resque\Resque::dequeue('default', ['My_Job']);
161161

162162
// Removes job class 'My_Job' with Job ID '087df5819a790ac666c9608e2234b21e' of queue 'default'
163-
Resque::dequeue('default', ['My_Job' => '087df5819a790ac666c9608e2234b21e']);
163+
Resque\Resque::dequeue('default', ['My_Job' => '087df5819a790ac666c9608e2234b21e']);
164164

165165
// Removes job class 'My_Job' with arguments of queue 'default'
166-
Resque::dequeue('default', ['My_Job' => array('foo' => 1, 'bar' => 2)]);
166+
Resque\Resque::dequeue('default', ['My_Job' => array('foo' => 1, 'bar' => 2)]);
167167

168168
// Removes multiple jobs
169-
Resque::dequeue('default', ['My_Job', 'My_Job2']);
169+
Resque\Resque::dequeue('default', ['My_Job', 'My_Job2']);
170170
```
171171

172172
If no jobs are given, this method will dequeue all jobs matching the provided
173173
queue.
174174

175175
```php
176176
// Removes all jobs of queue 'default'
177-
Resque::dequeue('default');
177+
Resque\Resque::dequeue('default');
178178
```
179179

180180
### Tracking Job Statuses
@@ -184,27 +184,27 @@ status information will allow you to check if a job is in the queue, is
184184
currently being run, has finished, or has failed.
185185

186186
To track the status of a job, pass `true` as the fourth argument to
187-
`Resque::enqueue`. A token used for tracking the job status will be returned:
187+
`Resque\Resque::enqueue`. A token used for tracking the job status will be returned:
188188

189189
```php
190-
$token = Resque::enqueue('default', 'My_Job', $args, true);
190+
$token = Resque\Resque::enqueue('default', 'My_Job', $args, true);
191191
echo $token;
192192
```
193193

194194
To fetch the status of a job:
195195

196196
```php
197-
$status = new Resque_Job_Status($token);
197+
$status = new Resque\Job\Status($token);
198198
echo $status->get(); // Outputs the status
199199
```
200200

201-
Job statuses are defined as constants in the `Resque_Job_Status` class. Valid
201+
Job statuses are defined as constants in the `Resque\Job\Status` class. Valid
202202
statuses include:
203203

204-
- `Resque_Job_Status::STATUS_WAITING` - Job is still queued
205-
- `Resque_Job_Status::STATUS_RUNNING` - Job is currently running
206-
- `Resque_Job_Status::STATUS_FAILED` - Job has failed
207-
- `Resque_Job_Status::STATUS_COMPLETE` - Job is complete
204+
- `Resque\Job\Status::STATUS_WAITING` - Job is still queued
205+
- `Resque\Job\Status::STATUS_RUNNING` - Job is currently running
206+
- `Resque\Job\Status::STATUS_FAILED` - Job has failed
207+
- `Resque\Job\Status::STATUS_COMPLETE` - Job is complete
208208
- `false` - Failed to fetch the status; is the token valid?
209209

210210
Statuses are available for up to 24 hours after a job has completed or failed,
@@ -213,13 +213,13 @@ calling the `stop()` method on a status class.
213213

214214
### Obtaining job PID ###
215215

216-
You can obtain the PID of the actual process doing the work through `Resque_Job_PID`. On a forking OS this will be the
216+
You can obtain the PID of the actual process doing the work through `Resque\Job\PID`. On a forking OS this will be the
217217
PID of the forked process.
218218

219219
CAUTION: on a non-forking OS, the PID returned will be of the worker itself.
220220

221221
```php
222-
echo Resque_Job_PID::get($token);
222+
echo Resque\Job\PID::get($token);
223223
```
224224

225225
Function returns `0` if the `perform` hasn't started yet, or if it has already ended.
@@ -231,12 +231,12 @@ To quote the documentation for the Ruby resque-scheduler:
231231
> Delayed jobs are one-off jobs that you want to be put into a queue at some
232232
point in the future. The classic example is sending an email:
233233

234-
require 'Resque/Resque.php';
235-
require 'ResqueScheduler/ResqueScheduler.php';
234+
require 'Resque.php';
235+
require 'Scheduler.php';
236236

237237
$in = 3600;
238238
$args = array('id' => $user->id);
239-
ResqueScheduler::enqueueIn($in, 'email', 'SendFollowUpEmail', $args);
239+
Resque\Scheduler::enqueueIn($in, 'email', 'SendFollowUpEmail', $args);
240240

241241
The above will store the job for 1 hour in the delayed queue, and then pull the
242242
job off and submit it to the `email` queue in Resque for processing as soon as
@@ -246,14 +246,14 @@ Instead of passing a relative time in seconds, you can also supply a timestamp
246246
as either a DateTime object or integer containing a UNIX timestamp to the
247247
`enqueueAt` method:
248248

249-
require 'Resque/Resque.php';
250-
require 'ResqueScheduler/ResqueScheduler.php';
249+
require 'Resque.php';
250+
require 'Scheduler.php';
251251

252252
$time = 1332067214;
253-
ResqueScheduler::enqueueAt($time, 'email', 'SendFollowUpEmail', $args);
253+
Resque\Scheduler::enqueueAt($time, 'email', 'SendFollowUpEmail', $args);
254254

255255
$datetime = new DateTime('2012-03-18 13:21:49');
256-
ResqueScheduler::enqueueAt($datetime, 'email', 'SendFollowUpEmail', $args);
256+
Resque\Scheduler::enqueueAt($datetime, 'email', 'SendFollowUpEmail', $args);
257257

258258
NOTE: resque-scheduler does not guarantee a job will fire at the time supplied.
259259
At the time supplied, resque-scheduler will take the job out of the delayed
@@ -426,11 +426,11 @@ It's easy to start the resque-scheduler worker using `bin/resque-scheduler`:
426426
php-resque has a basic event system that can be used by your application to
427427
customize how some of the php-resque internals behave.
428428

429-
You listen in on events (as listed below) by registering with `Resque_Event` and
429+
You listen in on events (as listed below) by registering with `Resque\Event` and
430430
supplying a callback that you would like triggered when the event is raised:
431431

432-
```sh
433-
Resque_Event::listen('eventName', [callback]);
432+
```php
433+
Resque\Event::listen('eventName', [callback]);
434434
```
435435

436436
`[callback]` may be anything in PHP that is callable by `call_user_func_array`:
@@ -443,12 +443,12 @@ Resque_Event::listen('eventName', [callback]);
443443
Events may pass arguments (documented below), so your callback should accept
444444
these arguments.
445445

446-
You can stop listening to an event by calling `Resque_Event::stopListening` with
447-
the same arguments supplied to `Resque_Event::listen`.
446+
You can stop listening to an event by calling `Resque\Event::stopListening` with
447+
the same arguments supplied to `Resque\Event::listen`.
448448

449449
It is up to your application to register event listeners. When enqueuing events
450450
in your application, it should be as easy as making sure php-resque is loaded
451-
and calling `Resque_Event::listen`.
451+
and calling `Resque\Event::listen`.
452452

453453
When running workers, if you run workers via the default `bin/resque` script,
454454
your `APP_INCLUDE` script should initialize and register any listeners required
@@ -462,20 +462,20 @@ A sample plugin is included in the `extras` directory.
462462
#### beforeFirstFork
463463

464464
Called once, as a worker initializes. Argument passed is the instance of
465-
`Resque_Worker` that was just initialized.
465+
`Resque\Worker\ResqueWorker` that was just initialized.
466466

467467
#### beforeFork
468468

469469
Called before php-resque forks to run a job. Argument passed contains the
470-
instance of `Resque_Job` for the job about to be run.
470+
instance of `Resque\JobHandler` for the job about to be run.
471471

472472
`beforeFork` is triggered in the **parent** process. Any changes made will be
473473
permanent for as long as the **worker** lives.
474474

475475
#### afterFork
476476

477477
Called after php-resque forks to run a job (but before the job is run). Argument
478-
passed contains the instance of `Resque_Job` for the job about to be run.
478+
passed contains the instance of `Resque\JobHandler` for the job about to be run.
479479

480480
`afterFork` is triggered in the **child** process after forking out to complete
481481
a job. Any changes made will only live as long as the **job** is being
@@ -484,16 +484,16 @@ processed.
484484
#### beforePerform
485485

486486
Called before the `setUp` and `perform` methods on a job are run. Argument
487-
passed contains the instance of `Resque_Job` for the job about to be run.
487+
passed contains the instance of `Resque\JobHandler` for the job about to be run.
488488

489489
You can prevent execution of the job by throwing an exception of
490-
`Resque_Job_DontPerform`. Any other exceptions thrown will be treated as if they
490+
`Resque\Exceptions\DoNotPerformException`. Any other exceptions thrown will be treated as if they
491491
were thrown in a job, causing the job to fail.
492492

493493
#### afterPerform
494494

495495
Called after the `perform` and `tearDown` methods on a job are run. Argument
496-
passed contains the instance of `Resque_Job` that was just run.
496+
passed contains the instance of `Resque\JobHandler` that was just run.
497497

498498
Any exceptions thrown will be treated as if they were thrown in a job, causing
499499
the job to be marked as having failed.
@@ -503,11 +503,11 @@ the job to be marked as having failed.
503503
Called whenever a job fails. Arguments passed (in this order) include:
504504

505505
- Exception - The exception that was thrown when the job failed
506-
- Resque_Job - The job that failed
506+
- Resque\JobHandler - The job that failed
507507

508508
#### beforeEnqueue
509509

510-
Called immediately before a job is enqueued using the `Resque::enqueue` method.
510+
Called immediately before a job is enqueued using the `Resque\Resque::enqueue` method.
511511
Arguments passed (in this order) include:
512512

513513
- Class - string containing the name of the job to be enqueued
@@ -516,11 +516,11 @@ Arguments passed (in this order) include:
516516
- ID - string containing the token of the job to be enqueued
517517

518518
You can prevent enqueing of the job by throwing an exception of
519-
`Resque_Job_DontCreate`.
519+
`Resque\Exceptions\DoNotCreateException`.
520520

521521
#### afterEnqueue
522522

523-
Called after a job has been queued using the `Resque::enqueue` method. Arguments
523+
Called after a job has been queued using the `Resque\Resque::enqueue` method. Arguments
524524
passed (in this order) include:
525525

526526
- Class - string containing the name of scheduled job

bin/resque

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ $REDIS_BACKEND = getenv('REDIS_BACKEND');
4242
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
4343
if(!empty($REDIS_BACKEND)) {
4444
if (empty($REDIS_BACKEND_DB))
45-
Resque::setBackend($REDIS_BACKEND);
45+
\Resque\Resque::setBackend($REDIS_BACKEND);
4646
else
47-
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
47+
\Resque\Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
4848
}
4949

5050
$logLevel = false;
@@ -70,7 +70,7 @@ if($APP_INCLUDE) {
7070
// See if the APP_INCLUDE containes a logger object,
7171
// If none exists, fallback to internal logger
7272
if (!isset($logger) || !is_object($logger)) {
73-
$logger = new Resque_Log($logLevel);
73+
$logger = new \Resque\Logger($logLevel);
7474
}
7575

7676
$BLOCKING = getenv('BLOCKING') !== FALSE;
@@ -89,8 +89,8 @@ if(!empty($COUNT) && $COUNT > 1) {
8989

9090
$PREFIX = getenv('PREFIX');
9191
if(!empty($PREFIX)) {
92-
$logger->log(Psr\Log\LogLevel::INFO, 'Prefix set to {prefix}', array('prefix' => $PREFIX));
93-
Resque_Redis::prefix($PREFIX);
92+
$logger->log(\Psr\Log\LogLevel::INFO, 'Prefix set to {prefix}', array('prefix' => $PREFIX));
93+
\Resque\Redis::prefix($PREFIX);
9494
}
9595

9696
function cleanup_children($signal){
@@ -100,19 +100,19 @@ function cleanup_children($signal){
100100
if($count > 1) {
101101
$children = array();
102102
$GLOBALS['send_signal'] = FALSE;
103-
103+
104104
$die_signals = array(SIGTERM, SIGINT, SIGQUIT);
105105
$all_signals = array_merge($die_signals, array(SIGUSR1, SIGUSR2, SIGCONT, SIGPIPE));
106-
106+
107107
for($i = 0; $i < $count; ++$i) {
108-
$pid = Resque::fork();
108+
$pid = \Resque\Resque::fork();
109109
if($pid == -1) {
110110
die("Could not fork worker ".$i."\n");
111111
}
112112
// Child, start the worker
113113
elseif(!$pid) {
114114
$queues = explode(',', $QUEUE);
115-
$worker = new Resque_Worker($queues);
115+
$worker = new \Resque\Worker\ResqueWorker($queues);
116116
$worker->setLogger($logger);
117117
$worker->hasParent = TRUE;
118118
fwrite(STDOUT, '*** Starting worker '.$worker."\n");
@@ -127,27 +127,27 @@ if($count > 1) {
127127
foreach ($all_signals as $signal) {
128128
pcntl_signal($signal, "cleanup_children");
129129
}
130-
130+
131131
$PIDFILE = getenv('PIDFILE');
132132
if ($PIDFILE) {
133133
if(file_put_contents($PIDFILE, getmypid()) === false){
134-
$logger->log(Psr\Log\LogLevel::NOTICE, 'Could not write PID information to {pidfile}', array('pidfile' => $PIDFILE));
134+
$logger->log(\Psr\Log\LogLevel::NOTICE, 'Could not write PID information to {pidfile}', array('pidfile' => $PIDFILE));
135135
die(2);
136136
}
137137
}
138-
138+
139139
$registered = TRUE;
140140
}
141-
141+
142142
if(function_exists('setproctitle')) {
143-
setproctitle('resque-' . Resque::VERSION . ": Monitoring {$count} children: [".implode(',', array_keys($children))."]");
143+
setproctitle('resque-' . \Resque\Resque::VERSION . ": Monitoring {$count} children: [".implode(',', array_keys($children))."]");
144144
}
145-
145+
146146
$childPID = pcntl_waitpid(-1, $childStatus, WNOHANG);
147147
if ($childPID != 0) {
148148
fwrite(STDOUT, "*** A child worker died: {$childPID}\n");
149149
unset($children[$childPID]);
150-
$i--;
150+
$i--;
151151
}
152152
usleep(250000);
153153
if ($GLOBALS['send_signal'] !== FALSE){
@@ -169,19 +169,19 @@ if($count > 1) {
169169
// Start a single worker
170170
else {
171171
$queues = explode(',', $QUEUE);
172-
$worker = new Resque_Worker($queues);
172+
$worker = new \Resque\Worker\ResqueWorker($queues);
173173
$worker->setLogger($logger);
174174
$worker->hasParent = FALSE;
175175

176176
$PIDFILE = getenv('PIDFILE');
177177
if ($PIDFILE) {
178178
if(file_put_contents($PIDFILE, getmypid()) === false) {
179-
$logger->log(Psr\Log\LogLevel::NOTICE, 'Could not write PID information to {pidfile}', array('pidfile' => $PIDFILE));
179+
$logger->log(\Psr\Log\LogLevel::NOTICE, 'Could not write PID information to {pidfile}', array('pidfile' => $PIDFILE));
180180
die(2);
181181
}
182182
}
183183

184-
$logger->log(Psr\Log\LogLevel::NOTICE, 'Starting worker {worker}', array('worker' => $worker));
184+
$logger->log(\Psr\Log\LogLevel::NOTICE, 'Starting worker {worker}', array('worker' => $worker));
185185
$worker->work($interval, $BLOCKING);
186186
}
187187
?>

bin/resque-scheduler

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ if (!class_exists('Composer\Autoload\ClassLoader', false)) {
2828
$REDIS_BACKEND = getenv('REDIS_BACKEND');
2929
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
3030
if(!empty($REDIS_BACKEND)) {
31-
if (empty($REDIS_BACKEND_DB))
32-
Resque::setBackend($REDIS_BACKEND);
31+
if (empty($REDIS_BACKEND_DB))
32+
\Resque\Resque::setBackend($REDIS_BACKEND);
3333
else
34-
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
34+
\Resque\Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
3535
}
3636

3737
// Set log level for resque-scheduler
@@ -40,10 +40,10 @@ $LOGGING = getenv('LOGGING');
4040
$VERBOSE = getenv('VERBOSE');
4141
$VVERBOSE = getenv('VVERBOSE');
4242
if(!empty($LOGGING) || !empty($VERBOSE)) {
43-
$logLevel = ResqueScheduler_Worker::LOG_NORMAL;
43+
$logLevel = \Resque\Worker\SchedulerWorker::LOG_NORMAL;
4444
}
4545
else if(!empty($VVERBOSE)) {
46-
$logLevel = ResqueScheduler_Worker::LOG_VERBOSE;
46+
$logLevel = \Resque\Worker\SchedulerWorker::LOG_VERBOSE;
4747
}
4848

4949
// Check for jobs every $interval seconds
@@ -66,10 +66,10 @@ if($APP_INCLUDE) {
6666
$PREFIX = getenv('PREFIX');
6767
if(!empty($PREFIX)) {
6868
fwrite(STDOUT, '*** Prefix set to '.$PREFIX."\n");
69-
Resque_Redis::prefix($PREFIX);
69+
\Resque\Redis::prefix($PREFIX);
7070
}
7171

72-
$worker = new ResqueScheduler_Worker();
72+
$worker = new \Resque\Worker\SchedulerWorker();
7373
$worker->logLevel = $logLevel;
7474

7575
$PIDFILE = getenv('PIDFILE');

0 commit comments

Comments
 (0)