Installation
composer require mjphaynes/php-resque
Ensure Redis is running locally or configured in your environment.
Basic Job Definition
Create a job class extending Resque\Job:
namespace App\Jobs;
use Resque\Job;
class SendEmailJob extends Job
{
protected $email;
protected $message;
public function __construct($email, $message)
{
$this->email = $email;
$this->message = $message;
}
public function perform()
{
// Your logic here (e.g., send email via Laravel Mailer)
\Log::info("Sending email to {$this->email}: {$this->message}");
}
}
Enqueue a Job
use App\Jobs\SendEmailJob;
$job = new SendEmailJob('user@example.com', 'Hello!');
$job->enqueue('emails'); // 'emails' is the queue name
Run the Worker
php artisan resque:work emails
(Ensure php artisan resque:setup is run first to configure Redis.)
SendEmailJob.Enqueueing
$job = new ProcessOrderJob($orderId);
$job->enqueue('orders', [], $delay = 60); // Delay in seconds
orders, high_priority) for organization.Processing
perform().Resque::work(1) for single-process workers or Resque::work($numProcesses) for parallelism.Retrying Failed Jobs
class ProcessPaymentJob extends Job
{
public function perform()
{
try {
// Payment logic
} catch (\Exception $e) {
$this->fail($e); // Marks job as failed (retryable)
}
}
}
config/resque.php).Monitoring
Resque::queues() to list all queues.Resque::size('queue_name').Laravel Integration
php-resque alongside Laravel’s queue system by wrapping jobs in Resque\Job:
class LaravelJobAdapter extends Job
{
protected $laravelJob;
public function __construct($laravelJob)
{
$this->laravelJob = $laravelJob;
}
public function perform()
{
$this->laravelJob->handle();
}
}
$laravelJob = new \App\Jobs\SendNotification;
$resqueJob = new LaravelJobAdapter($laravelJob);
$resqueJob->enqueue('notifications');
Dynamic Queues
$queue = $user->isPremium() ? 'premium' : 'standard';
$job->enqueue($queue);
Priority Queues
high, medium, low) and prioritize workers:
php artisan resque:work high --size=2 # 2 workers for high-priority queue
Job Chaining
$job1 = new ProcessDataJob($data);
$job1->enqueue('data_processing');
$job2 = new AnalyzeDataJob($data);
$job2->enqueue('analysis', [], 30); // After $job1 completes
Redis Connection Issues
config/resque.php) matches your environment.Connection refused → Verify Redis server is running (redis-cli ping).Job Stuck in Queue
php artisan resque:work --verbose).redis-cli info memory).supervisord or pm2 for process management).Serialization Problems
class BadJob extends Job {
public function __construct(User $user) { // User may not be serializable
$this->user = $user;
}
}
class GoodJob extends Job {
public function __construct(int $userId) {
$this->userId = $userId;
}
}
Worker Timeout
$job->enqueue('long_running', [], 0, 300); // 5-minute timeout
Log Worker Output
php artisan resque:work queue_name --log-file=/path/to/worker.log
perform():
\Log::debug("Job data: " . json_encode($this->data));
Inspect Queues
php artisan resque:list queue_name
php artisan resque:remove queue_name job_id
Test Locally with Docker
docker run -p 6379:6379 redis
config/resque.php to point to redis://localhost.Environment-Specific Config
// config/resque.php
'connection' => env('REDIS_URL', 'redis://localhost'),
Custom Job Classes
Resque\Job for shared functionality:
abstract class BaseJob extends Job
{
protected $retryCount = 0;
public function fail(\Exception $e)
{
$this->retryCount++;
if ($this->retryCount < 3) {
parent::fail($e);
}
}
}
Rate Limiting
$job->enqueue('throttled', [], 0, 0, 10); // Max 10 concurrent jobs
Plugins
resque-scheduler for cron-like job scheduling:
composer require mjphaynes/resque-scheduler
$job = new DailyReportJob();
$job->schedule('0 2 * * *');
$job->save();
Performance
$batch = new BatchJob([$job1, $job2, $job3]);
$batch->enqueue('batch_processing');
Resque::work($numProcesses) to parallelize workloads.How can I help you explore Laravel packages today?