softwarepunt / php-resque
Redis backed library for creating background jobs and processing them later. Based on resque for Ruby.
Requires
- php: >=8.2
- ext-pcntl: *
- colinmollenhour/credis: dev-master
- psr/log: ~1.0
Requires (Dev)
- phpunit/phpunit: ^10
Suggests
- 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.
This package is auto-updated.
Last update: 2024-10-29 15:53:01 UTC
README
php-resque is a Redis-based library for enqueuing and running background jobs.
This is a lightly maintained fork of chrisboulton/php-resque, with fixes and improvements, compatible with PHP 8.2+.
⚠️ Not recommended for new projects. We are only maintaining this for legacy projects.
Installation
Add the package with composer:
composer require softwarepunt/php-resque
Usage
Configuration
If you are not using default Redis configuration, set the backend manually:
Resque::setBackend('localhost:6379');
Define jobs
Each job should be in its own class, and include a perform method:
namespace MyApp; class Greeter_Job { public function setUp() { // Optional: Set up before (called before perform()) } public function perform() { // Required: Main work method // Perform work; context data is accessible from $this->args echo "Hello, {$this->args['name']}!"; } public function tearDown() { // Optional: Tear down after (called after job finishes) } }
Enqueue jobs
Jobs instances are placed in specific queues with a set of context data (args):
// Enqueue an instance of "My_Job" in the "default" queue Resque::enqueue('default', 'MyApp\Greeter_Job', ['name' => "Hank"]);
Dequeue jobs
Jobs can be removed from queues:
// Remove all jobs of a certain type from a queue Resque::dequeue('default', ['MyApp\Greeter_Job']); // Remove specific job from a queue Resque::dequeue('default', ['MyApp\Greeter_Job' => '087df5819a790ac666c9608e2234b21e']);
Workers
Start a worker with the QUEUE
environment variable to begin processing jobs from that queue:
QUEUE=default php vendor/bin/resque
Environment variables
You can set the following environment variables on the worker:
Events
You can listen for specific events to track status changes:
Resque_Event::listen('eventName', $callback);
$callback
may be anything in PHP that is callable by call_user_func_array
.
Advanced
Getting self job info
You can get the ID, queue, etc. from within an executing job:
public function perform() { echo $this->queue; // my_queue (Queue name) echo $this->job->payload['id']; // aa4c8b768d7b89a1b90d000cfefdf785 (Job ID) echo $this->job->payload['queue_time']; // 1695993877.3551 (Job enqueued at) }