php-resque (pronounced like "rescue") is a Redis-backed library for creating background jobs, placing those jobs on multiple queues, and processing them later.
← Go back to main documentation
php-resque has an events system that can be used by your application to change how some of the php-resque internals behaves without needing to change the core code.
You listen in on events (listed below) by using the Resque\Event::listen
method and supplying
a callback that you would like triggered when the event is raised:
Resque\Event::listen(Resque\Event::EVENT_NAME, [callback]);
[callback]
may be anything in PHP that is callable by call_user_func_array
:
- A string with the name of a function
- An array containing an object and method to call
- An array containing an object and a static method to call
- A Closure
Events may pass arguments, such as the worker or job, so your callback can accept these arguments.
You can stop listening to an event by calling Resque\Event::forget
with the same arguments supplied to Resque\Event::listen
.
It is up to your application to register event listeners. When enqueuing events in your application,
it should be as easy as making sure php-resque is loaded and calling Resque\Event::listen
.
When running workers, if you run workers via the default vendor/bin/resque
script, your include
script should
initialise and register any listeners required for operation. If you have rolled your own worker manager,
then it is again your responsibility to register listeners.
As an example, say you wanted to record how long each job took with your own logging software you could do something like this:
Event::listen(Event::JOB_COMPLETE, function($event, $job) {
myLoggerFunction($job->execTime());
});
The Resque\Event::JOB_DONE
is triggered as the last action in the job processing. While Resque\Event::JOB_COMPLETE
is triggered after the jobs perform()
method is finished, the Resque\Event::JOB_DONE
Event is triggered after the job output is stored in Redis backend.
In short: Resque\Event::JOB_COMPLETE
is fired, when the jobs perform method work is done, while Resque\Event::JOB_DONE
is fired, when the whole job processing is coming to an end.
Resque\Event::WORKER_INSTANCE
- New worker is createdResque\Event::WORKER_STARTUP
- Worker is starting upResque\Event::WORKER_SHUTDOWN
- Worker is shutting downResque\Event::WORKER_FORCE_SHUTDOWN
- Worker is being forced to shutdownResque\Event::WORKER_REGISTER
- The worker is registering itselfResque\Event::WORKER_UNREGISTER
- Worker is unregistering itselfResque\Event::WORKER_WORK
- Worker is workingResque\Event::WORKER_FORK
- Worker is about to forkResque\Event::WORKER_FORK_ERROR
- There was an error forkingResque\Event::WORKER_FORK_PARENT
- After forking the parent processResque\Event::WORKER_FORK_CHILD
- After forking the child processResque\Event::WORKER_WORKING_ON
- Worker working on a jobResque\Event::WORKER_DONE_WORKING
- Worker finished working on a jobResque\Event::WORKER_KILLCHILD
- Worker kill the child processResque\Event::WORKER_PAUSE
- Pause the workerResque\Event::WORKER_RESUME
- Resume workerResque\Event::WORKER_WAKEUP
- Wakeup workerResque\Event::WORKER_CLEANUP
- Clean up workerResque\Event::WORKER_LOW_MEMORY
- Low memory errorResque\Event::WORKER_CORRUPT
- Worker is corrupted
Resque\Event::JOB_INSTANCE
- New Job is createdResque\Event::JOB_QUEUE
- The job is about to be added to a queueResque\Event::JOB_QUEUED
- The job has been added to a queueResque\Event::JOB_DELAY
- The job is about to be delayedResque\Event::JOB_DELAYED
- The job has been delayedResque\Event::JOB_QUEUE_DELAYED
- Delayed job about to be queuedResque\Event::JOB_QUEUED_DELAYED
- Delayed job has been queuedResque\Event::JOB_PERFORM
- The job is about to be runResque\Event::JOB_RUNNING
- The job is runningResque\Event::JOB_COMPLETE
- The job has been completedResque\Event::JOB_CANCELLED
- The job has been cancelledResque\Event::JOB_FAILURE
- The job has failedResque\Event::JOB_DONE
- The job is done