neurohotep / console-mutex
Mutex for Laravel console commands.
Requires
- php: ^7.2
- ext-json: *
- arvenil/ninja-mutex: ^0.6
- illuminate/console: ^6.0
- illuminate/support: ^6.0
Requires (Dev)
- ext-pdo_mysql: *
- ext-redis: *
- illuminated/testing-tools: ^6.0
- mockery/mockery: ^1.2
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.3
- predis/predis: ^1.1.1
- dev-master
- 6.x-dev
- 6.0.0
- 5.8.x-dev
- 5.8.1
- 5.8.0
- 5.7.x-dev
- 5.7.4
- 5.7.3
- 5.7.2
- 5.7.1
- 5.7.0
- 5.6.x-dev
- 5.6.3
- 5.6.2
- 5.6.1
- 5.6.0
- 5.5.x-dev
- 5.5.5
- 5.5.4
- 5.5.3
- 5.5.2
- 5.5.1
- 5.5.0
- 5.4.x-dev
- 5.4.2
- 5.4.1
- 5.4.0
- 5.3.x-dev
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.x-dev
- 5.2.2
- 5.2.1
- 5.2.0
- 5.1.x-dev
- 5.1.2
- 5.1.1
- 5.1.0
- 1.5.0
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
This package is not auto-updated.
Last update: 2024-11-17 09:33:34 UTC
README
Mutex for Laravel console commands.
Table of contents
Usage
-
Install the package via Composer:
composer require illuminated/console-mutex
-
Use
Illuminated\Console\WithoutOverlapping
trait:use Illuminated\Console\WithoutOverlapping; class ExampleCommand extends Command { use WithoutOverlapping; // ... }
Strategies
Overlapping can be prevented by various strategies:
file
(default)mysql
redis
memcached
Default file
strategy is fine for small applications, which are deployed on a single server.
If your application is more complex and deployed on several nodes, then you probably would like to use another mutex strategy.
You can change the mutex strategy by specifying $mutexStrategy
field:
class ExampleCommand extends Command { use WithoutOverlapping; protected $mutexStrategy = 'mysql'; // ... }
Or by using setMutexStrategy
method:
class ExampleCommand extends Command { use WithoutOverlapping; public function __construct() { parent::__construct(); $this->setMutexStrategy('mysql'); } // ... }
Advanced
Set custom timeout
By default, the mutex is checking for a running command, and if it finds such, it just exits. However, you can manually set the timeout for a mutex, so it can wait for another command to finish its execution, instead of just quitting immediately.
You can change the mutex timeout by specifying $mutexTimeout
field:
class ExampleCommand extends Command { use WithoutOverlapping; protected $mutexTimeout = 3000; // milliseconds // ... }
Or by using setMutexTimeout
method:
class ExampleCommand extends Command { use WithoutOverlapping; public function __construct() { parent::__construct(); $this->setMutexTimeout(3000); // milliseconds } // ... }
There are three possible options for $mutexTimeout
field:
0
- check without waiting (default);{milliseconds}
- check, and wait for a maximum of milliseconds specified;null
- wait, till running command finish its execution;
Handle several commands
Sometimes it is useful to set common mutex for several commands. You can easily achieve this by setting them the same mutex name. By default, mutex name is generated based on a command's name and arguments.
To change this, override getMutexName
method in your command:
class ExampleCommand extends Command { use WithoutOverlapping; public function getMutexName() { return "icmutex-for-command1-and-command2"; } // ... }
Custom mutex file storage
If you're using file
strategy, mutex files will be stored in the storage/app
folder, by default.
You can change the storage folder by overriding getMutexFileStorage
method in your command:
class ExampleCommand extends Command { use WithoutOverlapping; public function getMutexFileStorage() { return storage_path('my/custom/path'); } // ... }
Troubleshooting
Trait included, but nothing happens?
Note, that WithoutOverlapping
trait is overriding initialize
method:
trait WithoutOverlapping { protected function initialize(InputInterface $input, OutputInterface $output) { $this->initializeMutex(); } // ... }
If your command is overriding initialize
method too, then you should call initializeMutex
method by yourself:
class ExampleCommand extends Command { use WithoutOverlapping; protected function initialize(InputInterface $input, OutputInterface $output) { $this->initializeMutex(); $this->foo = $this->argument('foo'); $this->bar = $this->argument('bar'); $this->baz = $this->argument('baz'); } // ... }
Several traits conflict?
If you're using another illuminated/console-%
package, then you can find yourself getting into the "traits conflict".
For example, if you're trying to build loggable command, which is protected against overlapping:
class ExampleCommand extends Command { use Loggable; use WithoutOverlapping; // ... }
You'll get the fatal error - the traits conflict, because of both of these traits are overriding initialize
method:
If two traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.
Override initialize
method by yourself, and initialize traits in required order:
class ExampleCommand extends Command { use Loggable; use WithoutOverlapping; protected function initialize(InputInterface $input, OutputInterface $output) { $this->initializeMutex(); $this->initializeLogging(); } // ... }
License
The MIT License. Please see License File for more information.