aureja/job-queue
JobQueue is a PHP package for managing job queues, providing a simple way to enqueue, process, and organize background tasks in your application. Suitable for basic queueing needs with a lightweight setup and straightforward API.
Installation:
composer require aureja/job-queue
Publish the config file:
php artisan vendor:publish --provider="Aureja\JobQueue\JobQueueServiceProvider"
Configuration:
Edit config/job-queue.php to define your queue connections (e.g., database, redis, beanstalk). Example:
'connections' => [
'database' => [
'driver' => 'database',
'table' => 'job_queue',
'connection' => 'mysql',
],
'redis' => [
'driver' => 'redis',
'connection' => 'cache',
],
],
First Use Case: Define a job:
use Aureja\JobQueue\Contracts\JobInterface;
class SendEmailJob implements JobInterface {
public function handle() {
// Your job logic here
}
}
Dispatch the job:
use Aureja\JobQueue\Facades\JobQueue;
JobQueue::dispatch(new SendEmailJob());
Run the Worker:
php artisan job:work database --queue=default
(Replace database with your connection name.)
Job Dispatching:
JobQueue::dispatch() for synchronous dispatching.JobQueue::dispatch(new SendEmailJob())->delay(now()->addMinutes(10));
JobQueue::dispatch(new SendEmailJob())->onQueue('emails');
Job Processing:
JobInterface for custom jobs. The handle() method is executed by the worker.public function handle() {
// Job logic
}
public function middleware() {
return [
\Aureja\JobQueue\Middleware\Retry::class,
\Aureja\JobQueue\Middleware\Log::class,
];
}
Worker Management:
php artisan job:work redis --queue=emails,notifications
supervisord.conf):
[program:laravel-worker]
command=php /path/to/artisan job:work redis --queue=default
autostart=true
autorestart=true
user=www-data
numprocs=4
Batch Processing:
JobQueue::batch(new SendEmailJob(), 50)->dispatch();
job:batch artisan command.Laravel Events: Dispatch jobs from event listeners:
public function handle(UserRegistered $event) {
JobQueue::dispatch(new SendWelcomeEmailJob($event->user));
}
API Endpoints: Trigger jobs via API:
Route::post('/send-email', function () {
JobQueue::dispatch(new SendEmailJob());
return response()->json(['status' => 'queued']);
});
Testing:
Use JobQueue::fake() in tests:
public function test_job_is_dispatched() {
JobQueue::fake();
JobQueue::dispatch(new SendEmailJob());
JobQueue::assertDispatched(SendEmailJob::class);
}
Connection Configuration:
predis/predis package:
composer require predis/predis
table to exist. Run migrations if using the built-in database driver:
php artisan migrate
Worker Stuck Jobs:
--timeout flag to limit execution time:
php artisan job:work redis --timeout=60
php artisan job:failed
Middleware Order:
middleware(). Place Retry before Log to avoid logging retry attempts:
public function middleware() {
return [
\Aureja\JobQueue\Middleware\Retry::class,
\Aureja\JobQueue\Middleware\Log::class,
];
}
Serialization:
__serialize() and __unserialize():
public function __serialize() {
return ['user_id' => $this->user->id];
}
public function __unserialize(array $data) {
$this->user = User::find($data['user_id']);
}
Log Output:
Enable debug mode in config/job-queue.php:
'debug' => env('APP_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Worker Verbosity:
Run workers with -v for verbose output:
php artisan job:work redis -v
Failed Jobs: Retry or delete failed jobs:
php artisan job:retry <job_id>
php artisan job:forget <job_id>
Custom Drivers:
Extend Aureja\JobQueue\Contracts\QueueConnectionInterface to support new backends (e.g., RabbitMQ, AWS SQS).
Job Events:
Listen for job events (e.g., JobProcessed, JobFailed) via Laravel's event system:
Event::listen(JobProcessed::class, function ($event) {
// Handle job completion
});
Job Metadata: Attach metadata to jobs for tracking:
JobQueue::dispatch(new SendEmailJob())->withMetadata(['priority' => 'high']);
Access metadata in handle() via $this->metadata.
Queue Monitoring:
Build a dashboard using the job:list command or create a custom API endpoint to fetch queue stats:
Route::get('/queue/stats', function () {
return JobQueue::stats();
});
How can I help you explore Laravel packages today?