Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
danhunsaker committed Apr 9, 2020
2 parents 7dd1474 + 7b5c928 commit b6a920c
Show file tree
Hide file tree
Showing 10 changed files with 738 additions and 44 deletions.
21 changes: 10 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
language: php

php:
- '7.4'
- '7.3'
- '7.2'
- '7.1'
- '7.0'
- '5.6'
- hhvm
matrix:
exclude:
- php: hhvm
env: ENABLE_REDIS_EXT=1
allow_failures:
- php: '7.4'
- php: '7.3'
- php: '7.2'
- php: hhvm

env:
- ENABLE_REDIS_EXT=0
- ENABLE_REDIS_EXT=1
before_script:

matrix:
allow_failures:
- php: '7.4'
- php: '5.6'

install:
- echo 'error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- sh -c "if [ $ENABLE_REDIS_EXT -eq 1 ]; then echo \"extension=redis.so\" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi"
- composer install
87 changes: 84 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ jobs on one or more queues, and processing them later.
[![Downloads](https://img.shields.io/packagist/dt/resque/php-resque.svg?style=flat-square)](https://packagist.org/packages/resque/php-resque)

[![Build Status](https://img.shields.io/travis/resque/php-resque.svg?style=flat-square&logo=travis)](http://travis-ci.org/resque/php-resque)
[![Tested With HHVM](https://img.shields.io/hhvm/resque/php-resque.svg?style=flat-square)](http://travis-ci.org/resque/php-resque)
[![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/)
[![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/)
[![Dependency Status](https://img.shields.io/librariesio/github/resque/php-resque.svg?style=flat-square)](https://libraries.io/github/resque/php-resque)

[![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)
[![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)
[![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)
[![Maintenance Status](https://img.shields.io/maintenance/yes/2019.svg?style=flat-square&logo=github&logoColor=white)](https://github.com/resque/php-resque)
[![Maintenance Status](https://img.shields.io/maintenance/yes/2020.svg?style=flat-square&logo=github&logoColor=white)](https://github.com/resque/php-resque)

[![Contributors](https://img.shields.io/github/contributors/resque/php-resque.svg?style=flat-square&logo=github&logoColor=white)](https://github.com/resque/php-resque)
[![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)
Expand Down Expand Up @@ -56,6 +55,15 @@ It also supports the following additional features:
- Has built in support for `setUp` and `tearDown` methods, called pre and post
jobs

Additionally it includes php-resque-scheduler, a PHP port of [resque-scheduler](http://github.com/resque/resque),
which adds support for scheduling items in the future to Resque. It has been
designed to be an almost direct-copy of the Ruby plugin

At the moment, php-resque-scheduler only supports delayed jobs, which is the
ability to push a job to the queue and have it run at a certain timestamp, or
in a number of seconds. Support for recurring jobs (similar to CRON) is planned
for a future release.

This port was originally made by [Chris
Boulton](https://github.com/chrisboulton), with maintenance by the community.
See <https://github.com/chrisboulton/php-resque> for more on that history.
Expand Down Expand Up @@ -218,6 +226,43 @@ echo Resque_Job_PID::get($token);

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

## Delayed Jobs

To quote the documentation for the Ruby resque-scheduler:

> Delayed jobs are one-off jobs that you want to be put into a queue at some
point in the future. The classic example is sending an email:

require 'Resque/Resque.php';
require 'ResqueScheduler/ResqueScheduler.php';

$in = 3600;
$args = array('id' => $user->id);
ResqueScheduler::enqueueIn($in, 'email', 'SendFollowUpEmail', $args);

The above will store the job for 1 hour in the delayed queue, and then pull the
job off and submit it to the `email` queue in Resque for processing as soon as
a worker is available.

Instead of passing a relative time in seconds, you can also supply a timestamp
as either a DateTime object or integer containing a UNIX timestamp to the
`enqueueAt` method:

require 'Resque/Resque.php';
require 'ResqueScheduler/ResqueScheduler.php';

$time = 1332067214;
ResqueScheduler::enqueueAt($time, 'email', 'SendFollowUpEmail', $args);

$datetime = new DateTime('2012-03-18 13:21:49');
ResqueScheduler::enqueueAt($datetime, 'email', 'SendFollowUpEmail', $args);

NOTE: resque-scheduler does not guarantee a job will fire at the time supplied.
At the time supplied, resque-scheduler will take the job out of the delayed
queue and push it to the appropriate queue in Resque. Your next available Resque
worker will pick the job up. To keep processing as quick as possible, keep your
queues as empty as possible.

## Workers

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

### Setting Redis backend ###

When you have the Redis database on a different host than the one the workers
When you have the Redis database on a different host than the one the workers
are running, you must set the `REDIS_BACKEND` environment variable:

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

### Resque Scheduler

resque-scheduler requires a special worker that runs in the background. This
worker is responsible for pulling items off the schedule/delayed queue and adding
them to the queue for resque. This means that for delayed or scheduled jobs to be
executed, that worker needs to be running.

A basic "up-and-running" `bin/resque-scheduler` file that sets up a
running worker environment is included (`vendor/bin/resque-scheduler` when
installed via composer). It accepts many of the same environment variables as
the main workers for php-resque:

* `REDIS_BACKEND` - Redis server to connect to
* `LOGGING` - Enable logging to STDOUT
* `VERBOSE` - Enable verbose logging
* `VVERBOSE` - Enable very verbose logging
* `INTERVAL` - Sleep for this long before checking scheduled/delayed queues
* `APP_INCLUDE` - Include this file when starting (to launch your app)
* `PIDFILE` - Write the PID of the worker out to this file

It's easy to start the resque-scheduler worker using `bin/resque-scheduler`:
$ php bin/resque-scheduler

## Event/Hook System

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

### afterSchedule

Called after a job has been added to the schedule. Arguments passed are the
timestamp, queue of the job, the class name of the job, and the job's arguments.

### beforeDelayedEnqueue

Called immediately after a job has been pulled off the delayed queue and right
before the job is added to the queue in resque. Arguments passed are the queue
of the job, the class name of the job, and the job's arguments.

## Step-By-Step

For a more in-depth look at what php-resque does under the hood (without needing
Expand All @@ -486,6 +565,7 @@ to directly examine the code), have a look at `HOWITWORKS.md`.
- @andrewjshults
- @atorres757
- @benjisg
- @biinari
- @cballou
- @chaitanyakuber
- @charly22
Expand All @@ -511,6 +591,7 @@ to directly examine the code), have a look at `HOWITWORKS.md`.
- @patrickbajao
- @pedroarnal
- @ptrofimov
- @rayward
- @richardkmiller
- @Rockstar04
- @ruudk
Expand Down
82 changes: 82 additions & 0 deletions bin/resque-scheduler
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env php
<?php

// Find and initialize Composer
$files = array(
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../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
);
}

$REDIS_BACKEND = getenv('REDIS_BACKEND');
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
if(!empty($REDIS_BACKEND)) {
if (empty($REDIS_BACKEND_DB))
Resque::setBackend($REDIS_BACKEND);
else
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
}

// Set log level for resque-scheduler
$logLevel = 0;
$LOGGING = getenv('LOGGING');
$VERBOSE = getenv('VERBOSE');
$VVERBOSE = getenv('VVERBOSE');
if(!empty($LOGGING) || !empty($VERBOSE)) {
$logLevel = ResqueScheduler_Worker::LOG_NORMAL;
}
else if(!empty($VVERBOSE)) {
$logLevel = ResqueScheduler_Worker::LOG_VERBOSE;
}

// Check for jobs every $interval seconds
$interval = 5;
$INTERVAL = getenv('INTERVAL');
if(!empty($INTERVAL)) {
$interval = $INTERVAL;
}

// Load the user's application if one exists
$APP_INCLUDE = getenv('APP_INCLUDE');
if($APP_INCLUDE) {
if(!file_exists($APP_INCLUDE)) {
die('APP_INCLUDE ('.$APP_INCLUDE.") does not exist.\n");
}

require_once $APP_INCLUDE;
}

$PREFIX = getenv('PREFIX');
if(!empty($PREFIX)) {
fwrite(STDOUT, '*** Prefix set to '.$PREFIX."\n");
Resque_Redis::prefix($PREFIX);
}

$worker = new ResqueScheduler_Worker();
$worker->logLevel = $logLevel;

$PIDFILE = getenv('PIDFILE');
if ($PIDFILE) {
file_put_contents($PIDFILE, getmypid()) or
die('Could not write PID information to ' . $PIDFILE);
}

fwrite(STDOUT, "*** Starting scheduler worker\n");
$worker->work($interval);
22 changes: 12 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,31 @@
}
],
"require": {
"php": ">=5.3.0",
"ext-pcntl": "*",
"php": ">=5.6.0",
"colinmollenhour/credis": "~1.7",
"psr/log": "~1.0"
},
"suggest": {
"ext-pcntl": "REQUIRED for forking processes on platforms that support it (so anything but Windows).",
"ext-proctitle": "Allows php-resque to rename the title of UNIX processes to show the status of a worker.",
"ext-redis": "Native PHP extension for Redis connectivity. Credis will automatically utilize when available."
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
"phpunit/phpunit": "^5.7"
},
"bin": [
"bin/resque"
"bin/resque",
"bin/resque-scheduler"
],
"autoload": {
"psr-0": {
"Resque": "lib"
"Resque": "lib",
"ResqueScheduler": "lib"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
13 changes: 13 additions & 0 deletions extras/resque-scheduler.monit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Replace these with your own:
# [PATH/TO/RESQUE]
# [PATH/TO/RESQUE-SCHEDULER]
# [UID]
# [GID]
# [APP_INCLUDE]

check process resque-scheduler_worker
with pidfile /var/run/resque/scheduler-worker.pid
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]
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;'"
if totalmem is greater than 300 MB for 10 cycles then restart # eating up memory?
group resque-scheduler_workers
Loading

0 comments on commit b6a920c

Please sign in to comment.