laravel-queue-rabbitmq icon indicating copy to clipboard operation
laravel-queue-rabbitmq copied to clipboard

Dispatch a job after DB transaction commit

Open shahramfdl opened this issue 3 years ago • 2 comments

Support Laravel 8 and later

Example: Using when you want to dispatch TestJob::dispatch()->afterCommit();

or you can use it on your job

class TestQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct()
    {
        $this->onConnection('rabbitmq');
        $this->onQueue('test');
        $this->afterCommit();
    }

    public function handle()
    {
        //...
    }
}

also it simply use on event listener queueable

class TestListener implements ShouldQueue
{
    use InteractsWithQueue;

    public $afterCommit = true;

    public function viaConnection()
    {
        return 'rabbitmq';
    }

    public function handle($event)
    {
        //...
    }
}

shahramfdl avatar Feb 28 '22 16:02 shahramfdl

@shahramfdl will this also support after_commit from config/queue.php? Is that coming in a later PR?

jehoshua02 avatar Mar 03 '22 00:03 jehoshua02

@shahramfdl will this also support after_commit from config/queue.php? Is that coming in a later PR?

@jehoshua02 Enable "after_commit" wherever Laravel allows you, yes its work now.

Example of specific RabbitMQ connection:

'rabbitmq' => [

            'driver' => 'rabbitmq',
            'queue' => env('RABBITMQ_QUEUE', 'default'),
            'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,
            'after_commit' => true, //Applies to all jobs in this connection 
            ....
],

It works like a charm.

shahramfdl avatar Mar 05 '22 22:03 shahramfdl

Running Laravel 9 and using this driver neither the ->afterCommit() method nor the 'after_commit' => true config option were working. That left if scratching our heads for some time - stuff would work using redis queues then fail with models not found using this rabbitmq driver.

This PR fixed it immediately. There are a number of other PRs and fixes on branches not submitted as PRs, that seem to be fixing things such as the ->delay() option, which is out-the-box queue functionality for Laravel and a little concerning. Without them, this is a broken driver. What needs to happen to get these PRs looked at?

judgej avatar Sep 15 '22 19:09 judgej

Sorry for the long delay. I appreciate the work. Since this PR introduces breaking changes to the API, I release it under v13.0.0

vyuldashev avatar Sep 15 '22 21:09 vyuldashev

Cool :-) We are going to be running some high load tests tomorrow with this PR included, so can let you know how it goes, if that helps at all. Much appreciated.

Update: so far, on smaller loads, no problems with release 13.0 on Laravel 9. All looks good.

judgej avatar Sep 15 '22 21:09 judgej