Skip to content

Latest commit

 

History

History
executable file
·
106 lines (75 loc) · 1.76 KB

README.md

File metadata and controls

executable file
·
106 lines (75 loc) · 1.76 KB

Queueable: Framework Agnostic Queue/Worker System

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