mbm-rafal / laravel-single-dispatch by mbm-rafal

Laravel Dispatcher extension for maintaining duplicate jobs
5,738
4
1
Package Data
Maintainer Username: mbm-rafal
Package Create Date: 2017-05-28
Package Last Update: 2017-05-28
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-06-09 15:07:48
Package Statistics
Total Downloads: 5,738
Monthly Downloads: 32
Daily Downloads: 0
Total Stars: 4
Total Watchers: 1
Total Forks: 3
Total Open Issues: 0

Single Dispatch

Extension for laravel dispatcher that do not allow to proceed same jobs on queues. Motivation for this feature is to not overuse resources while specific job is still waiting to be processed another one should not be queued.

Features

  • Allows to catch and ignore duplicated jobs

Requirements

Installed https://laravel.com/docs/5.4/queues

Installation

Require the mbm-rafal/laravel-single-dispatch package in your composer.json and update your dependencies.

$ composer require mbm-rafal/laravel-single-dispatch
$ composer update

Publish migration files

$ php artisan vendor:publish --provider="MBM\Bus\BusServiceProvider" --force

Run migrations

$ php artisan migrate

Usage

To use this extension you have to update resource resolved by provided by Dispatcher::class to do so, simply switch BusServiceProvider in app/config.php

// Illuminate\Bus\BusServiceProvider::class,
\MBM\Bus\BusServiceProvider::class,

You have to apply code to Queue events in AppServiceProvider

# Manage processed jobs
Queue::after(function (JobProcessed $event) {
    app(\MBM\Bus\Dispatcher::class)->unregister($event->job);
});

Queue::failing(function (JobFailed $event) {
    app(\MBM\Bus\Dispatcher::class)->unregister($event->job);
});

If you want to allow job to be duplicated add interface to Job class definition

use Illuminate\Contracts\Queue\ShouldQueue;
use \MBM\Bus\AllowDuplicates
 
class CustomJob implements ShouldQueue, AllowDuplicates
{
    // Code
}

License

Released under the MIT License, see LICENSE.