therezor / laravel-transactional-jobs

Run Laravel job inside transaction (after transaction is committed, or cancel if rolled back)
MIT License
45 stars 8 forks source link

Installation and running this? #1

Closed zilveer closed 5 years ago

zilveer commented 5 years ago

Hi I am using the following code

try { DB::beginTransaction(); // database queries here DB::commit(); } catch (\PDOException $e) { // Woopsy DB::rollBack(); }

In one of my controllers, how do I use this plugin so it can add the commits to the queue ? Also I suppose that I need to enable latavel queue jobs , right ?

Can you please provide a step by step guide on how to make it work with laravel and what edits I need to make on laravel to make it work ?

Bear regards

therezor commented 5 years ago

Hi. This package if for running laravel job after transaction successfully commits and skip if transaction fails.

Usage if fairly simple

  1. Replace BusServiceProvider using one from package.
  2. Add porperty $afterTransactions = true to jobs that has to be run after transaction successfully commits. https://github.com/therezor/laravel-transactional-jobs/blob/master/README.md
zilveer commented 5 years ago

@therezor thx for your reply . What is the main difference between the built in queue in Laravel and your plugin ?

therezor commented 5 years ago

It uses the same queue class but extended to prevent adding jobs to queue before data was actually saved to DB. That make it inaccessible inside workers job

zilveer commented 5 years ago

@therezor thanks, but what if the adding the job to DB fails, then we have data which has been saved to the DB?

therezor commented 5 years ago

If adding job to queue fails you'll have all your data committed to db.

zilveer commented 5 years ago

@therezor then the saved data, which has relationships to the jobs is failed as well. how is it possible to reverse this or really make sure that the jobs is actually added upon DB commit ?

therezor commented 5 years ago

laravel way package way