├── LICENSE ├── README.md ├── composer.json └── src ├── BusServiceProvider.php ├── Contracts └── RunAfterTransaction.php └── TransactionalDispatcher.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) REZOR 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel transactional jobs 2 | 3 | > [!NOTE] 4 | > Laravel 10 and newer versions natively support [transactional job handling](https://laravel.com/docs/events#dispatching-events-after-database-transactions) without the need for this package. 5 | 6 | #### Problem: 7 | ![](img/laravel_schema.jpeg) 8 | #### Solution: 9 | ![](img/package_schema.jpeg) 10 | 11 | 12 | By using this package you easily dispatch jobs inside transactions. Cancel job on transaction rollback. Add to queue on transaction committed. 13 | 14 | ## Installation 15 | _This package requires PHP 7.1 and Laravel 5.8 or higher._ 16 | _If you are on a PHP version below 7.1 or a Laravel version below 5.8 just use an older version of this package._ 17 | 18 | 1) Run ```composer require therezor/laravel-transactional-jobs``` in your laravel project root folder 19 | 20 | 2) Implement `TheRezor\TransactionalJobs\Contracts\RunAfterTransaction` to jobs that run in the middle of database transactions 21 | 22 | ```php 23 | app->register(LaravelProvider::class); 27 | 28 | $this->app->singleton(TransactionalDispatcher::class, function ($app) { 29 | return new TransactionalDispatcher($app, function ($connection = null) use ($app) { 30 | return $app[QueueFactoryContract::class]->connection($connection); 31 | }); 32 | }); 33 | 34 | $this->app->alias( 35 | TransactionalDispatcher::class, Dispatcher::class 36 | ); 37 | 38 | $this->app->alias( 39 | TransactionalDispatcher::class, DispatcherContract::class 40 | ); 41 | 42 | $this->app->alias( 43 | TransactionalDispatcher::class, QueueingDispatcherContract::class 44 | ); 45 | } 46 | 47 | public function boot() 48 | { 49 | Event::listen(TransactionBeginning::class, function () { 50 | $this->app->make(TransactionalDispatcher::class)->beginTransaction(); 51 | }); 52 | 53 | Event::listen(TransactionCommitted::class, function () { 54 | $this->app->make(TransactionalDispatcher::class)->commitTransaction(); 55 | }); 56 | 57 | Event::listen(TransactionRolledBack::class, function () { 58 | $this->app->make(TransactionalDispatcher::class)->rollbackTransaction(); 59 | }); 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/Contracts/RunAfterTransaction.php: -------------------------------------------------------------------------------- 1 | transactionLevel++; 17 | $this->pendingCommands[$this->transactionLevel] = []; 18 | } 19 | 20 | public function commitTransaction(): void 21 | { 22 | $pendingCommands = $this->finishCurrentTransaction(); 23 | if ($this->transactionLevel <= 0) { 24 | foreach ($pendingCommands as $command) { 25 | parent::dispatchToQueue($command); 26 | } 27 | 28 | return; 29 | } 30 | 31 | $this->pendingCommands[$this->transactionLevel] = array_merge( 32 | $this->pendingCommands[$this->transactionLevel], 33 | $pendingCommands 34 | ); 35 | } 36 | 37 | public function rollbackTransaction(): void 38 | { 39 | $this->finishCurrentTransaction(); 40 | } 41 | 42 | protected function finishCurrentTransaction(): array 43 | { 44 | $pendingCommands = $this->pendingCommands[$this->transactionLevel] ?? []; 45 | 46 | unset($this->pendingCommands[$this->transactionLevel]); 47 | 48 | $this->transactionLevel--; 49 | 50 | return $pendingCommands; 51 | } 52 | 53 | public function dispatchToQueue($command) 54 | { 55 | if ($this->isTransactionalJob($command)) { 56 | $this->addPendingCommand($command); 57 | 58 | return null; 59 | } 60 | 61 | return parent::dispatchToQueue($command); 62 | } 63 | 64 | protected function isTransactionalJob($command): bool 65 | { 66 | return $this->transactionLevel && ($command instanceof RunAfterTransaction || !empty($command->afterTransactions)); 67 | } 68 | 69 | protected function addPendingCommand($command): void 70 | { 71 | $this->pendingCommands[$this->transactionLevel][] = $command; 72 | } 73 | } 74 | --------------------------------------------------------------------------------