Installation
composer require allprogrammic/resque
Ensure php-redis is installed (pecl install redis).
Configuration Publish the config file:
php artisan vendor:publish --provider="Allprogrammic\Resque\ResqueServiceProvider"
Update .env with Redis connection details:
RESQUE_CONNECTION=redis
RESQUE_QUEUE=default
First Job Create a job class:
namespace App\Jobs;
use Allprogrammic\Resque\Job as ResqueJob;
class ProcessData extends ResqueJob
{
public function perform()
{
// Your job logic here
\Log::info('Processing data...');
}
}
Enqueue a Job
use App\Jobs\ProcessData;
ProcessData::dispatch();
Run the Worker
php artisan resque:work
Dispatching Jobs
Use dispatch() or dispatchOn() for queue-specific jobs:
ProcessData::dispatchOn('high_priority')->delay(now()->addMinutes(5));
Job Chaining Chain jobs sequentially:
ProcessData::dispatch()
->then(new \App\Jobs\CleanupData());
Bulk Processing Enqueue multiple jobs in a loop:
foreach ($items as $item) {
ProcessItem::dispatch($item)->onQueue('batch');
}
Multiple Workers Run workers in separate terminals for parallel processing:
php artisan resque:work --queue=default &
php artisan resque:work --queue=high_priority &
Supervisor Setup
Use supervisord to manage workers as services (recommended for production).
Failed Jobs
Implement onFailure() in jobs:
public function onFailure($exception)
{
\Log::error("Job failed: " . $exception->getMessage());
}
Retry Logic
Configure retries in config/resque.php:
'retry' => [
'max_attempts' => 3,
'delay' => 60, // seconds
],
Redis Connection Issues Ensure Redis is running and accessible. Test with:
redis-cli ping
Verify .env has correct Redis host/port.
Job Stuck in Queue
Check for infinite loops or deadlocks. Use resque:remove to debug:
php artisan resque:remove ProcessData --queue=default
Worker Crashes
Logs may not persist if Redis connection drops. Use monolog for job-level logging.
Inspect Queues List queued jobs:
php artisan resque:list
Peek at a job:
php artisan resque:peek ProcessData --queue=default
Xdebug with Workers Attach Xdebug to the worker process for step-through debugging.
Custom Job Serialization
Override serialize()/unserialize() for complex payloads:
protected function serialize()
{
return json_encode(['data' => $this->data]);
}
Middleware for Jobs
Use Resque::middleware() to add pre/post hooks:
Resque::middleware(function ($job, $next) {
\Log::info("Job {$job->class} started");
return $next($job);
});
Custom Worker Classes
Extend Allprogrammic\Resque\Worker for specialized behavior (e.g., custom failure handling).
How can I help you explore Laravel packages today?