Skip to content

Commit b6a920c

Browse files
committed
Merge branch 'develop'
2 parents 7dd1474 + 7b5c928 commit b6a920c

File tree

10 files changed

+738
-44
lines changed

10 files changed

+738
-44
lines changed

.travis.yml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
language: php
2+
23
php:
34
- '7.4'
45
- '7.3'
56
- '7.2'
67
- '7.1'
78
- '7.0'
89
- '5.6'
9-
- hhvm
10-
matrix:
11-
exclude:
12-
- php: hhvm
13-
env: ENABLE_REDIS_EXT=1
14-
allow_failures:
15-
- php: '7.4'
16-
- php: '7.3'
17-
- php: '7.2'
18-
- php: hhvm
10+
1911
env:
2012
- ENABLE_REDIS_EXT=0
2113
- ENABLE_REDIS_EXT=1
22-
before_script:
14+
15+
matrix:
16+
allow_failures:
17+
- php: '7.4'
18+
- php: '5.6'
19+
20+
install:
21+
- echo 'error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
2322
- sh -c "if [ $ENABLE_REDIS_EXT -eq 1 ]; then echo \"extension=redis.so\" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi"
2423
- composer install

README.md

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ jobs on one or more queues, and processing them later.
1212
[![Downloads](https://img.shields.io/packagist/dt/resque/php-resque.svg?style=flat-square)](https://packagist.org/packages/resque/php-resque)
1313

1414
[![Build Status](https://img.shields.io/travis/resque/php-resque.svg?style=flat-square&logo=travis)](http://travis-ci.org/resque/php-resque)
15-
[![Tested With HHVM](https://img.shields.io/hhvm/resque/php-resque.svg?style=flat-square)](http://travis-ci.org/resque/php-resque)
1615
[![Code Quality](https://img.shields.io/scrutinizer/g/resque/php-resque.svg?style=flat-square&logo=scrutinizer)](https://scrutinizer-ci.com/g/resque/php-resque/)
1716
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/resque/php-resque.svg?style=flat-square&logo=scrutinizer)](https://scrutinizer-ci.com/g/resque/php-resque/)
1817
[![Dependency Status](https://img.shields.io/librariesio/github/resque/php-resque.svg?style=flat-square)](https://libraries.io/github/resque/php-resque)
1918

2019
[![Latest Release](https://img.shields.io/github/release/resque/php-resque.svg?style=flat-square&logo=github&logoColor=white)](https://github.com/resque/php-resque)
2120
[![Latest Release Date](https://img.shields.io/github/release-date/resque/php-resque.svg?style=flat-square&logo=github&logoColor=white)](https://github.com/resque/php-resque)
2221
[![Commits Since Latest Release](https://img.shields.io/github/commits-since/resque/php-resque/latest.svg?style=flat-square&logo=github&logoColor=white)](https://github.com/resque/php-resque)
23-
[![Maintenance Status](https://img.shields.io/maintenance/yes/2019.svg?style=flat-square&logo=github&logoColor=white)](https://github.com/resque/php-resque)
22+
[![Maintenance Status](https://img.shields.io/maintenance/yes/2020.svg?style=flat-square&logo=github&logoColor=white)](https://github.com/resque/php-resque)
2423

2524
[![Contributors](https://img.shields.io/github/contributors/resque/php-resque.svg?style=flat-square&logo=github&logoColor=white)](https://github.com/resque/php-resque)
2625
[![Chat on Slack](https://img.shields.io/badge/chat-Slack-blue.svg?style=flat-square&logo=slack&logoColor=white)](https://join.slack.com/t/php-resque/shared_invite/enQtNTIwODk0OTc1Njg3LWYyODczMTZjMzI2N2JkYWUzM2FlNDk5ZjY2ZGM4Njc4YjFiMzU2ZWFjOGQxMDIyNmE5MTBlNWEzODBiMmVmOTI)
@@ -56,6 +55,15 @@ It also supports the following additional features:
5655
- Has built in support for `setUp` and `tearDown` methods, called pre and post
5756
jobs
5857

58+
Additionally it includes php-resque-scheduler, a PHP port of [resque-scheduler](http://github.com/resque/resque),
59+
which adds support for scheduling items in the future to Resque. It has been
60+
designed to be an almost direct-copy of the Ruby plugin
61+
62+
At the moment, php-resque-scheduler only supports delayed jobs, which is the
63+
ability to push a job to the queue and have it run at a certain timestamp, or
64+
in a number of seconds. Support for recurring jobs (similar to CRON) is planned
65+
for a future release.
66+
5967
This port was originally made by [Chris
6068
Boulton](https://github.com/chrisboulton), with maintenance by the community.
6169
See <https://github.com/chrisboulton/php-resque> for more on that history.
@@ -218,6 +226,43 @@ echo Resque_Job_PID::get($token);
218226

219227
Function returns `0` if the `perform` hasn't started yet, or if it has already ended.
220228

229+
## Delayed Jobs
230+
231+
To quote the documentation for the Ruby resque-scheduler:
232+
233+
> Delayed jobs are one-off jobs that you want to be put into a queue at some
234+
point in the future. The classic example is sending an email:
235+
236+
require 'Resque/Resque.php';
237+
require 'ResqueScheduler/ResqueScheduler.php';
238+
239+
$in = 3600;
240+
$args = array('id' => $user->id);
241+
ResqueScheduler::enqueueIn($in, 'email', 'SendFollowUpEmail', $args);
242+
243+
The above will store the job for 1 hour in the delayed queue, and then pull the
244+
job off and submit it to the `email` queue in Resque for processing as soon as
245+
a worker is available.
246+
247+
Instead of passing a relative time in seconds, you can also supply a timestamp
248+
as either a DateTime object or integer containing a UNIX timestamp to the
249+
`enqueueAt` method:
250+
251+
require 'Resque/Resque.php';
252+
require 'ResqueScheduler/ResqueScheduler.php';
253+
254+
$time = 1332067214;
255+
ResqueScheduler::enqueueAt($time, 'email', 'SendFollowUpEmail', $args);
256+
257+
$datetime = new DateTime('2012-03-18 13:21:49');
258+
ResqueScheduler::enqueueAt($datetime, 'email', 'SendFollowUpEmail', $args);
259+
260+
NOTE: resque-scheduler does not guarantee a job will fire at the time supplied.
261+
At the time supplied, resque-scheduler will take the job out of the delayed
262+
queue and push it to the appropriate queue in Resque. Your next available Resque
263+
worker will pick the job up. To keep processing as quick as possible, keep your
264+
queues as empty as possible.
265+
221266
## Workers
222267

223268
Workers work in the exact same way as the Ruby workers. For complete
@@ -315,7 +360,7 @@ $ PREFIX=my-app-name bin/resque
315360

316361
### Setting Redis backend ###
317362

318-
When you have the Redis database on a different host than the one the workers
363+
When you have the Redis database on a different host than the one the workers
319364
are running, you must set the `REDIS_BACKEND` environment variable:
320365

321366
```sh
@@ -355,6 +400,29 @@ functionality to PHP before 5.5, so if you'd like process titles updated,
355400
install the PECL module as well. php-resque will automatically detect and use
356401
it.
357402

403+
### Resque Scheduler
404+
405+
resque-scheduler requires a special worker that runs in the background. This
406+
worker is responsible for pulling items off the schedule/delayed queue and adding
407+
them to the queue for resque. This means that for delayed or scheduled jobs to be
408+
executed, that worker needs to be running.
409+
410+
A basic "up-and-running" `bin/resque-scheduler` file that sets up a
411+
running worker environment is included (`vendor/bin/resque-scheduler` when
412+
installed via composer). It accepts many of the same environment variables as
413+
the main workers for php-resque:
414+
415+
* `REDIS_BACKEND` - Redis server to connect to
416+
* `LOGGING` - Enable logging to STDOUT
417+
* `VERBOSE` - Enable verbose logging
418+
* `VVERBOSE` - Enable very verbose logging
419+
* `INTERVAL` - Sleep for this long before checking scheduled/delayed queues
420+
* `APP_INCLUDE` - Include this file when starting (to launch your app)
421+
* `PIDFILE` - Write the PID of the worker out to this file
422+
423+
It's easy to start the resque-scheduler worker using `bin/resque-scheduler`:
424+
$ php bin/resque-scheduler
425+
358426
## Event/Hook System
359427

360428
php-resque has a basic event system that can be used by your application to
@@ -462,6 +530,17 @@ passed (in this order) include:
462530
- Queue - string containing the name of the queue the job was added to
463531
- ID - string containing the new token of the enqueued job
464532

533+
### afterSchedule
534+
535+
Called after a job has been added to the schedule. Arguments passed are the
536+
timestamp, queue of the job, the class name of the job, and the job's arguments.
537+
538+
### beforeDelayedEnqueue
539+
540+
Called immediately after a job has been pulled off the delayed queue and right
541+
before the job is added to the queue in resque. Arguments passed are the queue
542+
of the job, the class name of the job, and the job's arguments.
543+
465544
## Step-By-Step
466545

467546
For a more in-depth look at what php-resque does under the hood (without needing
@@ -486,6 +565,7 @@ to directly examine the code), have a look at `HOWITWORKS.md`.
486565
- @andrewjshults
487566
- @atorres757
488567
- @benjisg
568+
- @biinari
489569
- @cballou
490570
- @chaitanyakuber
491571
- @charly22
@@ -511,6 +591,7 @@ to directly examine the code), have a look at `HOWITWORKS.md`.
511591
- @patrickbajao
512592
- @pedroarnal
513593
- @ptrofimov
594+
- @rayward
514595
- @richardkmiller
515596
- @Rockstar04
516597
- @ruudk

bin/resque-scheduler

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
// Find and initialize Composer
5+
$files = array(
6+
__DIR__ . '/../../vendor/autoload.php',
7+
__DIR__ . '/../../../autoload.php',
8+
__DIR__ . '/../../../../autoload.php',
9+
__DIR__ . '/../vendor/autoload.php',
10+
);
11+
12+
$found = false;
13+
foreach ($files as $file) {
14+
if (file_exists($file)) {
15+
require_once $file;
16+
break;
17+
}
18+
}
19+
20+
if (!class_exists('Composer\Autoload\ClassLoader', false)) {
21+
die(
22+
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
23+
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
24+
'php composer.phar install' . PHP_EOL
25+
);
26+
}
27+
28+
$REDIS_BACKEND = getenv('REDIS_BACKEND');
29+
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
30+
if(!empty($REDIS_BACKEND)) {
31+
if (empty($REDIS_BACKEND_DB))
32+
Resque::setBackend($REDIS_BACKEND);
33+
else
34+
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
35+
}
36+
37+
// Set log level for resque-scheduler
38+
$logLevel = 0;
39+
$LOGGING = getenv('LOGGING');
40+
$VERBOSE = getenv('VERBOSE');
41+
$VVERBOSE = getenv('VVERBOSE');
42+
if(!empty($LOGGING) || !empty($VERBOSE)) {
43+
$logLevel = ResqueScheduler_Worker::LOG_NORMAL;
44+
}
45+
else if(!empty($VVERBOSE)) {
46+
$logLevel = ResqueScheduler_Worker::LOG_VERBOSE;
47+
}
48+
49+
// Check for jobs every $interval seconds
50+
$interval = 5;
51+
$INTERVAL = getenv('INTERVAL');
52+
if(!empty($INTERVAL)) {
53+
$interval = $INTERVAL;
54+
}
55+
56+
// Load the user's application if one exists
57+
$APP_INCLUDE = getenv('APP_INCLUDE');
58+
if($APP_INCLUDE) {
59+
if(!file_exists($APP_INCLUDE)) {
60+
die('APP_INCLUDE ('.$APP_INCLUDE.") does not exist.\n");
61+
}
62+
63+
require_once $APP_INCLUDE;
64+
}
65+
66+
$PREFIX = getenv('PREFIX');
67+
if(!empty($PREFIX)) {
68+
fwrite(STDOUT, '*** Prefix set to '.$PREFIX."\n");
69+
Resque_Redis::prefix($PREFIX);
70+
}
71+
72+
$worker = new ResqueScheduler_Worker();
73+
$worker->logLevel = $logLevel;
74+
75+
$PIDFILE = getenv('PIDFILE');
76+
if ($PIDFILE) {
77+
file_put_contents($PIDFILE, getmypid()) or
78+
die('Could not write PID information to ' . $PIDFILE);
79+
}
80+
81+
fwrite(STDOUT, "*** Starting scheduler worker\n");
82+
$worker->work($interval);

composer.json

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,31 @@
2828
}
2929
],
3030
"require": {
31-
"php": ">=5.3.0",
32-
"ext-pcntl": "*",
31+
"php": ">=5.6.0",
3332
"colinmollenhour/credis": "~1.7",
3433
"psr/log": "~1.0"
3534
},
3635
"suggest": {
36+
"ext-pcntl": "REQUIRED for forking processes on platforms that support it (so anything but Windows).",
3737
"ext-proctitle": "Allows php-resque to rename the title of UNIX processes to show the status of a worker.",
3838
"ext-redis": "Native PHP extension for Redis connectivity. Credis will automatically utilize when available."
3939
},
4040
"require-dev": {
41-
"phpunit/phpunit": "3.7.*"
41+
"phpunit/phpunit": "^5.7"
4242
},
4343
"bin": [
44-
"bin/resque"
44+
"bin/resque",
45+
"bin/resque-scheduler"
4546
],
4647
"autoload": {
4748
"psr-0": {
48-
"Resque": "lib"
49+
"Resque": "lib",
50+
"ResqueScheduler": "lib"
4951
}
50-
},
51-
"extra": {
52-
"branch-alias": {
53-
"dev-master": "1.0-dev"
54-
}
52+
},
53+
"extra": {
54+
"branch-alias": {
55+
"dev-master": "1.0-dev"
5556
}
57+
}
5658
}

extras/resque-scheduler.monit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Replace these with your own:
2+
# [PATH/TO/RESQUE]
3+
# [PATH/TO/RESQUE-SCHEDULER]
4+
# [UID]
5+
# [GID]
6+
# [APP_INCLUDE]
7+
8+
check process resque-scheduler_worker
9+
with pidfile /var/run/resque/scheduler-worker.pid
10+
start program = "/bin/sh -c 'APP_INCLUDE=[APP_INCLUDE] RESQUE_PHP=[PATH/TO/RESQUE] PIDFILE=/var/run/resque/scheduler-worker.pid nohup php -f [PATH/TO/RESQUE-SCHEDULER]/resque-scheduler.php > /var/log/resque/scheduler-worker.log &'" as uid [UID] and gid [GID]
11+
stop program = "/bin/sh -c 'kill -s QUIT `cat /var/run/resque/scheduler-worker.pid` && rm -f /var/run/resque/scheduler-worker.pid; exit 0;'"
12+
if totalmem is greater than 300 MB for 10 cycles then restart # eating up memory?
13+
group resque-scheduler_workers

0 commit comments

Comments
 (0)