catch-of-the-day/php-resque
Redis-backed background job queue for PHP (Resque port). Enqueue jobs onto one or more prioritized queues and process them with distributed workers. Supports forking for leak resilience, failure handling, optional job status tracking, and setUp/tearDown hooks.
Installation:
composer require catch-of-the-day/php-resque
Add require 'vendor/autoload.php'; to your project’s bootstrap file.
First Job:
Define a job class with a perform() method:
class SendWelcomeEmail {
public function perform() {
// Your logic here
echo "Email sent to: " . $this->args['email'];
}
}
Enqueue a Job:
Resque::setBackend('redis://localhost:6379');
Resque::enqueue('emails', 'SendWelcomeEmail', ['email' => 'user@example.com']);
Run a Worker:
QUEUE=emails APP_INCLUDE=path/to/autoload.php php vendor/bin/resque
ProcessPayment, SendNotification).setUp()/tearDown() for resource management (e.g., DB connections, logging).
class ProcessPayment {
public function setUp() {
$this->logger = new Logger();
}
public function perform() {
$this->logger->log("Processing payment...");
}
public function tearDown() {
$this->logger->close();
}
}
QUEUE=critical,default,low php vendor/bin/resque
QUEUE=${ENV_QUEUE} php vendor/bin/resque
COUNT=4 QUEUE=emails php vendor/bin/resque
monit or systemd to manage worker processes.public function boot() {
Resque::setBackend(config('resque.redis'));
Resque_Event::listen('beforeEnqueue', [$this, 'logJob']);
}
class ResqueFacade extends \Illuminate\Support\Facades\Facade {
protected static function getFacadeAccessor() { return 'resque'; }
}
Resque_Event::listen('onFailure', function($exception, $job) {
if ($job->retries < 3) {
Resque::enqueue($job->queue, get_class($job), $job->args, true, $job->retries + 1);
}
});
Redis Connection Issues:
$redis = new Redis();
$redis->connect('localhost', 6379);
Resque::setBackend('tcp://user:pass@host:port') for authentication.Job Failures:
perform() mark jobs as failed. Use try-catch:
public function perform() {
try {
// Risky logic
} catch (\Exception $e) {
throw new \RuntimeException("Failed: " . $e->getMessage());
}
}
Worker Stuck on Jobs:
USR1 signal to kill stuck jobs:
kill -USR1 <worker_pid>
Autoloading Jobs:
autoload.psr-4:
{
"autoload": {
"psr-4": {
"App\\Jobs\\": "app/Jobs/"
}
}
}
VVERBOSE=1 QUEUE=emails php vendor/bin/resque
redis-cli LRANGE resque:queue:emails 0 -1
$token = Resque::enqueue('emails', 'SendWelcomeEmail', ['email' => 'user@example.com'], true);
$status = new Resque_Job_Status($token);
echo $status->get(); // STATUS_WAITING, STATUS_RUNNING, etc.
Resque::enqueue('batch', 'ProcessBatch', ['ids' => [1, 2, 3]]);
class My_Job {
private static $redis;
public function setUp() {
self::$redis = new Redis();
self::$redis->connect('localhost', 6379);
}
public function perform() {
self::$redis->lPush('batch:ids', $this->args['id']);
}
}
Resque_Backend for non-Redis storage (e.g., database).Resque_Job for custom data formats.Resque_Event::listen('afterPerform', function($job) {
$metrics->record('job_completed', get_class($job));
});
config/resque.php:
'redis' => [
'host' => env('REDIS_HOST', 'localhost'),
'port' => env('REDIS_PORT', 6379),
'password' => env('REDIS_PASSWORD', null),
],
class LogJobMiddleware {
public function handle($job, Closure $next) {
Log::info("Job started: " . get_class($job));
$next($job);
Log::info("Job completed: " . get_class($job));
}
}
Register via Resque_Event::listen('beforePerform', [$middleware, 'handle']).
How can I help you explore Laravel packages today?