Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispatch a job after DB transaction commit #468

Merged
merged 1 commit into from
Sep 15, 2022

Conversation

shahramfdl
Copy link
Contributor

@shahramfdl shahramfdl commented Feb 28, 2022

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)
    {
        //...
    }
}

@jehoshua02
Copy link

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

@shahramfdl
Copy link
Contributor Author

shahramfdl commented Mar 5, 2022

@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.

@judgej
Copy link

judgej commented Sep 15, 2022

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?

@vyuldashev
Copy link
Owner

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 vyuldashev merged commit 842ce70 into vyuldashev:master Sep 15, 2022
@judgej
Copy link

judgej commented Sep 15, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants