dilab/queueable

Framework Agnostic Queue System

0.2.7 2018-12-04 09:49 UTC

This package is auto-updated.

Last update: 2024-04-14 18:51:28 UTC


README

Install

composer require dilab/queueable

Usage

Job & Queue

  • 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'])
);

Worker

  • Create a Worker instance
$worker = new Worker($queue);
  • Put worker to work
$worker->work($maxTries = 5, $sleepSecs = 5);
$worker->setLogger($psr3Logger);

Callbacks

  • 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
});

Current Drivers:

  • AWS SQS

Notes

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