composer require dilab/queueable
- Create a Job
class SendEmailJob implements JobContract
{
public function handle(Payload $payload)
{
return 'Sending an email to user ' . $payload->get('name');
}
}
- Create a Queue
$driver = new InMemoryDriver();
$queue = new Queue('email', $driver);
- Enqueue a job
$queue->push(
new SendEmailJob(),
new Payload(['name' => 'Xu'])
);
- Create a Worker instance
$worker = new Worker($queue);
- Put worker to work
$worker->work($maxTries = 5, $sleepSecs = 5);
- You can set a PSR-3 logger if you want
$worker->setLogger($psr3Logger);
- beforeFetchJob: It is called before trying to fetch a job from the queue
$worker->attach('heartbeat', function () use ($queueName) {
// do something useful
});
- beforeCompleteJob: It is called before a job is completed
$worker->attach('beforeCompleteJob', function () {
// do something useful
});
- afterCompleteJob: It is called after a job is completed
$worker->attach('afterCompleteJob', function () {
// do something useful
});
- onError: It is called whenever it is failed to process a job
$worker->attach('onError', function ($failedJob, $message, $trace) {
// send an email
});
- AWS SQS
Some general notes when developing this package
- Driver works with message(raw data, mostly in array format)
- Queue translates message to Job
- Job works with message
- Worker works with Job & Queue objects