dilab / queueable
Framework Agnostic Queue System
Installs: 5 776
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 0
Forks: 1
Open Issues: 3
Requires
- php: >=5.6.0
- aws/aws-sdk-php: ^3.19
- mockery/mockery: ^0.9.6
- psr/log: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5.6
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);
- You can set a PSR-3 logger if you want
$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