An open API service indexing awesome lists of open source software.

https://github.com/mpclarkson/resque-bundle

This repository is deprecated - please use https://github.com/resquebundle/resque
https://github.com/mpclarkson/resque-bundle

Last synced: 4 months ago
JSON representation

This repository is deprecated - please use https://github.com/resquebundle/resque

Awesome Lists containing this project

README

        

# ResqueBundle

[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mpclarkson/resque-bundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mpclarkson/resque-bundle/?branch=master)
[![Build Status](https://travis-ci.org/mpclarkson/resque-bundle.svg?branch=master)](https://travis-ci.org/mpclarkson/resque-bundle)

**THIS REPO HAS BEEN MOVED TO ITS OWN GITHUB ORGANISATION, THE CODE HERE IS FOR THOSE THAT DEPEND ON IT BUT NEW CODE, BUG FIXES, PULL REQUESTS ETC SHOULD BE MADE IN THE NEW REPO HERE: https://github.com/resquebundle/resque**

----

This is a fork of the BCCResqueBundle, as it is no longer being maintained. There are a lot of outstanding issues, pull requests and bugs that need to be fixed.
**Contributions are welcome - I don't have the bandwidth to maintain this alone.**

The resque bundle provides integration of [php-resque](https://github.com/chrisboulton/php-resque/) to Symfony2-3.
It is inspired from resque, a Redis-backed Ruby library for creating background jobs, placing them on multiple queues, and processing them later.

## Features:

- Creating a Job, with container access in order to leverage your Symfony services
- Enqueue a Job with parameters on a given queue
- Creating background worker on a given queue
- An interface to monitor your queues, workers and job statuses
- Schedule jobs to run at a specific time or after a number of seconds delay
- Auto re-queue failed jobs, with back-off strategies

## Todos:

- [x] PSR4
- [x] Update admin to Bootstrap 3
- [x] Migration from BCC notes
- [x] Travis CI
- [x] Symfony 3 compatibility
- [ ] Implement Full Unit Tests
- [ ] Make decision to support PHP 7+ ;-)
- [ ] Code quality - Scrutinizer 9.5+
- [ ] Replicate functionality of the resque-web ruby lib (i.e .retry and delete failed jobs etc)
- [ ] Community contributions / Ignored PRs
- [ ] Fix bugs

ORIGINAL TODOs:
- [ ] Log management
- [ ] Job status tracking
- [ ] Redis configuration
- [ ] Localisation
- [ ] Tests

## Migrating from BCCResqueBundle:

Here are some notes to make it easier to migrate from the BCCResqueBundle:

- Find and replace all instances of `BCC\ResqueBundle` with `Mpclarkson\ResqueBundle` throughout your app (e.g. use statements)
- Update your `routing.yml` by replacing `@BCCResque` with `@ResqueBundle`
- The `bcc:` prefix for all commands has been dropped
- Stop and restart all workers
- The container service definition`bcc_resque.resque` has been replaced with `resque`. You can either search and replace this or create an alias as follows:
```yaml
bcc_resque.resque:
alias: resque
lazy: true
```

## Installation and configuration:

### Requirements

Make sure you have redis installed on your machine: http://redis.io/

### Get the bundle

Add `mpclarkson/resque-bundle` to your dependencies:

``` json
{
"require": {
...
"mpclarkson/resque-bundle": "dev-master"
}
...
}
```

To install, run `php composer.phar [update|install]`.

### Add ResqueBundle to your application kernel

``` php
get('resque');

// create your job
$job = new MyJob();
$job->args = array(
'file' => '/tmp/file',
'content' => 'hello',
);

// enqueue your job
$resque->enqueue($job);
```

## Running a worker on a queue

Executing the following commands will create a work on :
- the `default` queue : `app/console resque:worker-start default`
- the `q1` and `q2` queue : `app/console resque:worker-start q1,q2` (separate name with `,`)
- all existing queues : `app/console resque:worker-start "*"`

You can also run a worker foreground by adding the `--foreground` option;

By default `VERBOSE` environment variable is set when calling php-resque
- `--verbose` option sets `VVERBOSE`
- `--quiet` disables both so no debug output is thrown

See php-resque logging option : https://github.com/chrisboulton/php-resque#logging

## Adding a delayed job to a queue

You can specify that a job is run at a specific time or after a specific delay (in seconds).

From your controller you can do:

``` php
get('resque');

// create your job
$job = new MyJob();
$job->args = array(
'file' => '/tmp/file',
'content' => 'hello',
);

// enqueue your job to run at a specific \DateTime or int unix timestamp
$resque->enqueueAt(\DateTime|int $at, $job);

// or

// enqueue your job to run after a number of seconds
$resque->enqueueIn($seconds, $job);

```

You must also run a `scheduledworker`, which is responsible for taking items out of the special delayed queue and putting
them into the originally specified queue.

`app/console resque:scheduledworker-start`

Stop it later with `app/console resque:scheduledworker-stop`.

Note that when run in background mode it creates a PID file in 'cache//resque_scheduledworker.pid'. If you
clear your cache while the scheduledworker is running you won't be able to stop it with the `scheduledworker-stop` command.

Alternatively, you can run the scheduledworker in the foreground with the `--foreground` option.

Note also you should only ever have one scheduledworker running, and if the PID file already exists you will have to use
the `--force` option to start a scheduledworker.

## Manage production workers with supervisord

It's probably best to use supervisord (http://supervisord.org) to run the workers in production, rather than re-invent job
spawning, monitoring, stopping and restarting.

Here's a sample conf file

```ini
[program:myapp_phpresque_default]
command = /usr/bin/php /home/sites/myapp/bin/console resque:worker-start high --env=prod --foreground --verbose
user = myusername
stopsignal=QUIT

[program:myapp_phpresque_scheduledworker]
command = /usr/bin/php /home/sites/myapp/prod/bin/console resque:scheduledworker-start --env=prod --foreground --verbose
user = myusername
stopsignal=QUIT

[group:myapp]
programs=myapp_phpresque_default,myapp_phpresque_scheduledworker
```

(If you use a custom Resque prefix, add an extra environment variable: PREFIX='my-resque-prefix')

Then in Capifony you can do

`sudo supervisorctl stop myapp:*` before deploying your app and `sudo supervisorctl start myapp:*` afterwards.

## More features

### Changing the queue

You can change a job queue just by setting the `queue` field of the job:

From within the job:

``` php
queue = 'my_queue';
}

public function run($args)
{
...
}
}
```

Or from outside the job:

``` php
queue = 'my_queue';
```

### Access the container from inside your job

Just extend the `ContainerAwareJob`:

``` php
getContainer()->getDoctrine();
...
}
}
```

### Stop a worker

Use the `app/console resque:worker-stop` command.

- No argument will display running workers that you can stop.
- Add a worker id to stop it: `app/console resque:worker-stop ubuntu:3949:default`
- Add the `--all` option to stop all the workers.

### Auto retry

You can have the bundle auto retry failed jobs by adding `retry strategy` for either a specific job, or for all jobs in general:

The following will allow `Some\Job` to retry 3 times.

* right away
* after a 10 second delay
* after a 60 second delay

```yml
resque:
redis:
....
auto_retry:
Some\Job: [0, 10, 60]
```

Setting strategy for all jobs:

```yml
resque:
auto_retry: [0, 10, 60]
```

With default strategy for all but specific jobs:

```yml
resque:
auto_retry:
default: [0, 10, 60]
Some\Job: [0, 10, 120, 240]
Some\Other\Job: [10, 30, 120, 600]
```

The `default` strategy (if provided) will be applied to all jobs that does not have a specific strategy attached. If not provided these jobs will not have auto retry.

You can disable `auto_retry` for selected jobs by using an empty array:

```yml
resque:
auto_retry:
default: [0, 10, 60]
Some\Job: []
Some\Other\Job: [10, 30, 120, 600]
```

Here `Some\Job` will not have any `auto_retry` attached.

**Please note**

To use the `auto_retry` feature, you must also run the scheduler job:

`app/console resque:scheduledworker-start`