acassan/worker-bundle
Laravel/PHP worker bundle providing a queue/worker manager to run and supervise background jobs. Includes tooling to start, stop, and monitor workers and process queued tasks, helping you manage asynchronous job execution in your application.
Installation
composer require acassan/worker-bundle
php artisan vendor:publish --provider="Acassan\WorkerBundle\WorkerBundle" --tag=config
Publish the default config and migrations if needed.
Configure Queue Connection
Edit config/worker.php to specify your queue connection (e.g., database, redis, beanstalk):
'connection' => env('QUEUE_CONNECTION', 'database'),
Define a Worker Job
Create a job class extending Acassan\WorkerBundle\Contracts\WorkerJob:
namespace App\Jobs;
use Acassan\WorkerBundle\Contracts\WorkerJob;
class ProcessData implements WorkerJob
{
public function handle()
{
// Your job logic here
return true; // Return true for success, false for failure
}
}
Dispatch a Job
use App\Jobs\ProcessData;
ProcessData::dispatch();
Run the Worker
php artisan worker:run
Or use the Tinker console for testing:
php artisan tinker
Worker::run();
Use the bundle to offload a time-consuming task (e.g., processing CSV files) by dispatching a job and letting the worker handle it asynchronously.
Dispatch Jobs
// Dispatch with payload
ProcessData::dispatch(['data' => $payload]);
// Dispatch with delay (in seconds)
ProcessData::dispatch()->delay(60);
Worker Management
php artisan worker:run) or programmatically.config/worker.php:
'workers' => [
'max_processes' => 4, // Default: 1
'timeout' => 60, // Default: 60 (seconds)
],
Worker::stop() to halt workers cleanly.Job Retries
Configure retries in config/worker.php:
'retries' => 3,
'retry_delay' => 10, // Seconds between retries
Monitoring
handle():
\Log::info('Processing data', ['data' => $this->data]);
jobs) to track job status.JobProcessed and JobFailed events for post-processing:
event(new JobProcessed($job));
Acassan\WorkerBundle\Middleware\WorkerMiddleware to add pre/post-processing logic.Worker::fake() in tests to simulate job processing:
$this->fake(Worker::class);
ProcessData::dispatch();
$this->assertProcessed();
Connection Issues
.env.php artisan queue:failed-table # Check failed jobs
php artisan queue:work --once # Test manually
Worker Stuck in Loop
handle() or deadlocks in database transactions.timeout in config to force restarts:
'timeout' => 30,
Payload Size Limits
serialize()/unserialize() for complex data or store references in the database.Missing Dependencies
pdo, mbstring) are installed for database/Redis queues.php artisan worker:run >> /var/log/worker.log 2>&1
# config/worker.php
'xdebug' => env('APP_DEBUG', false),
\Log::debug('Job data', ['payload' => $this->data]);
Custom Worker Classes
Extend Acassan\WorkerBundle\Worker to add features like:
namespace App\Workers;
use Acassan\WorkerBundle\Worker;
class CustomWorker extends Worker
{
protected function getQueue(): string
{
return 'custom_queue';
}
}
Job Middleware
Register middleware in config/worker.php:
'middleware' => [
\App\Middleware\LogJob::class,
],
Event Listeners Listen for worker lifecycle events:
// EventServiceProvider
protected $listen = [
'Acassan\WorkerBundle\Events\WorkerStarted' => [
\App\Listeners\LogWorkerStart::class,
],
];
Queue Table Customization Publish and modify migrations:
php artisan vendor:publish --tag=worker-migrations
How can I help you explore Laravel packages today?