#development #laravel #php #reading-list

🔗 The Laravel Isolatable interface
twitter.com

TIL #Laravel provides an Isolatable interface that commands can implement, the migrate command is one example of a command implementing this interface.

This allows you to make sure migrations only run once at the same time!

You can read more about this in the Laravel documentation.

Isolatable Commands

Sometimes you may wish to ensure that only one instance of a command can run at a time. To accomplish this, you may implement the Illuminate\Contracts\Console\Isolatable interface on your command class:

1namespace App\Console\Commands;
2
3use Illuminate\Console\Command;
4use Illuminate\Contracts\Console\Isolatable;
5
6class SendEmails extends Command implements Isolatable
7{
8    // ...
9}

When a command is marked as Isolatable, Laravel will automatically add an --isolated option to the command. When the command is invoked with that option, Laravel will ensure that no other instances of that command are already running. Laravel accomplishes this by attempting to acquire an atomic lock using your application's default cache driver. If other instances of the command are running, the command will not execute; however, the command will still exit with a successful exit status code:

php artisan mail:send 1 --isolated

If you would like to specify the exit status code that the command should return if it is not able to execute, you may provide the desired status code via the isolated option:

php artisan mail:send 1 --isolated=12
continue reading on twitter.com

⚠️ This post links to an external website. ⚠️