Download the PHP package mehr-it/lara-transaction-waiting-events without Composer

On this page you can find all versions of the php package mehr-it/lara-transaction-waiting-events. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package lara-transaction-waiting-events

Laravel events waiting for transactions to complete

This package implements queued events for Laravel which are guaranteed not be handled before any pending database transaction has been closed.

Why is it necessary?

Queued event listeners often rely on data that is written by the event emitter. Even the usage of database transactions is required or at least extremely recommendable, Laravel does not offer a mechanism to block event handling after any pending database transactions are done.

Many other packages dealing with events and transactions, simply collect events in memory until the database transactions have been committed. Then they send them to queue. However if the process runs into an error condition after committing and before having sent all events to the queue, you will lose some events. Imagine a customer order never being processed due to a lost event...

It would be much better to emit the event first and then commit the database transaction after the event has been sent (Note: the event handler must gracefully handle cases where the transaction is rolled back - which usually is simple).

How does it work?

Whenever an event is dispatched to a queued listener while a database transaction is open, the event dispatcher will write a lock entry to the database which is released as soon as the transaction is closed. Queued listeners wait for the "transaction lock" to be released before being invoked. That's all we need.

Important: Event listeners are invoked even after the transaction has been rolled back! It's up to the listener to deal with cases like this. Since it is always a very good idea that the listener checks any preconditions when invoked (instead of assuming a specific state of the system), their should be no extra work here.

Implementation details

This packages extends Laravel's event dispatcher to set "transaction locks" whenever a queued listener (marked with the WaitsForTransactions interface) is invoked. It uses MySQL locking functions to set the "transaction locks". Therefore, this package only works for MySQL connections. The lock implementation assures that locks are always removed when a transaction ends. No matter if committed, rolled back or the process died unexpectedly.

Before calling the queued listener, any transaction locks are checked for existence. If a transaction lock still exists, event handling job is released to the queue again and is retried later.

Requirements

This package only works with MySQL database connections!

Installation

composer require mehr-it/lara-transaction-waiting-events

This package uses Laravel's package auto-discovery, so the service provider and aliases will be loaded automatically.

Usage

There is only one thing to do, to make queued event handlers wait for transactions to be complete. The WaitsForTransactions marker interface has to be added to the listener class:

class Listener implements ShouldQueue, WaitsForTransactions
{
    public function handle($event) {

    }
}

Specify transaction lock details

The listener may specify some options for the transaction locks:

Custom connection

By default listeners only wait for transactions using the default DB connection. You may pass in another connection name or specify multiple connections using the waitForTransactions property:

class Listener implements ShouldQueue, WaitsForTransactions
{
    public $waitForTransactions = ['default-connection', 'secondary-connection'];

    public function handle($event) {

    }
}

Timeout and retry delay

By default, the listener waits up to 1s for transactions to complete. If yet not complete, the handler job is released back to queue to be retried in 5s. If you are using long-running transactions, you might want to adapt these values:

class Listener implements ShouldQueue, WaitsForTransactions
{
    // the time to wait for transactions to complete
    public $waitForTransactionsTimeout = 7;

    // the retry delay
    public $waitForTransactionsRetryAfter = 30;

    public function handle($event) {

    }
}

Disable transaction waiting events

Sometimes - especially when running tests or using the 'sync' queue driver - you might want to disable transaction waiting events and dispatch them even if the transaction has not been committed yet. You can do so, by setting EVENTS_WAIT_FOR_TRANSACTIONS to false in your .env file:

EVENTS_WAIT_FOR_TRANSACTIONS=false

All versions of lara-transaction-waiting-events with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
laravel/framework Version ^5.8|^6.0|^7.0|^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mehr-it/lara-transaction-waiting-events contains the following files

Loading the files please wait ....