Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed & documented Composer integration & Redis arg fatals #21

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 71 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,55 @@ Because the PHP port is almost a direct API copy of the Ruby version, it is also
compatible with the web interface of the Ruby version, which provides the
ability to view and manage delayed jobs.

## Requirements ##

* PHP 5.3+
* Redis 2.2+
* PHP-Resque 1.2+
* Optional but Recommended: Composer

## Getting Started ##

The easiest way to work with php-resque-scheduler 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-scheduler to your application's composer.json.

```json
{
...
"require": {
"chrisboulton/php-resque-scheduler": "dev-master",
"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)

```php
require 'vendor/autoload.php';
```

## 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);
```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
Expand All @@ -39,16 +75,15 @@ 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);
```php
$time = 1332067214;
ResqueScheduler::enqueueAt($time, 'email', 'SendFollowUpEmail', $args);

$datetime = new DateTime('2012-03-18 13:21:49');
ResqueScheduler::enqueueAt(datetime, '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.
**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
Expand All @@ -61,9 +96,15 @@ worker is responsible for pulling items off the schedule/delayed queue and addin
them to the queue for resque. This means that for delayed or scheduled jobs to be
executed, the worker needs to be running.

A basic "up-and-running" resque-scheduler.php file is included that sets up a
running worker environment is included in the root directory. It accepts many
of the same environment variables as php-resque:
A basic "up-and-running" resque-scheduler file that sets up a running worker
environment is included in the bin/ directory.

To start a worker, it's very similar to the Ruby version:
```sh
$ LOGGING=1 php bin/resque-scheduler
```

It accepts many of the same environment variables as php-resque:

* `REDIS_BACKEND` - Redis server to connect to
* `LOGGING` - Enable logging to STDOUT
Expand All @@ -74,18 +115,15 @@ of the same environment variables as php-resque:
* `PIDFILE` - Write the PID of the worker out to this file

The resque-scheduler worker requires resque to function. The demo
resque-scheduler.php worker allows you to supply a `RESQUE_PHP` environment
variable with the path to Resque.php. If not supplied and resque is not already
loaded, resque-scheduler will attempt to load it from your include path
(`require_once 'Resque/Resque.php';'`)
resque-scheduler worker uses the Composer autoloader to load Resque.php.

It's easy to start the resque-scheduler worker using resque-scheduler.php:
$ RESQUE_PHP=../resque/lib/Resque/Resque.php php resque-scheduler.php
It's easy to start the resque-scheduler worker using the included demo:
$ bin/resque-scheduler

## Event/Hook System

php-resque-scheduler uses the same event system used by php-resque and exposes
the following events.
the following additional events:

### afterSchedule

Expand All @@ -100,9 +138,12 @@ of the job, the class name of the job, and the job's arguments.

## Contributors ##

* chrisboulton
* rayward
* atorres757
* tonypiper
* biinari
* cballou
* [chrisboulton](//github.com/chrisboulton)
* [rayward](//github.com/rayward)
* [atorres757](//github.com/atorres757)
* [tonypiper](//github.com/tonypiper)
* [biinari](//github.com/biinari)
* [cballou](//github.com/cballou)
* [danhunsaker](//github.com/danhunsaker)
* [evertharmeling](//github.com/evertharmeling)
* [stevelacey](//github.com/stevelacey)
32 changes: 21 additions & 11 deletions resque-scheduler.php → bin/resque-scheduler
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
#!/usr/bin/env php
<?php

// Look for an environment variable with
$RESQUE_PHP = getenv('RESQUE_PHP');
if (!empty($RESQUE_PHP)) {
require_once $RESQUE_PHP;
}
// Otherwise, if we have no Resque then assume it is in the include path
else if (!class_exists('Resque')) {
require_once 'Resque/Resque.php';
// Find and initialize Composer
$files = array(
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../../../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
);

foreach ($files as $file) {
if (file_exists($file)) {
require_once $file;
break;
}
}

// Load resque-scheduler
require_once dirname(__FILE__) . '/lib/ResqueScheduler.php';
require_once dirname(__FILE__) . '/lib/ResqueScheduler/Worker.php';
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');
Expand Down
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
"email": "chris@bgigcommerce.com"
}
],
"autoload": {
"psr-0": {
"ResqueScheduler": "lib/"
}
"require": {
"chrisboulton/php-resque": "dev-master"
},
"repositories": [
{ "type": "git", "url": "https://github.com/chrisboulton/php-resque" }
],
"require": {
"chrisboulton/php-resque": "dev-master"
"bin": [
"bin/resque-scheduler"
],
"autoload": {
"psr-0": {
"ResqueScheduler": "lib/"
}
}
}