tajmahal86 / console-mutex
Laravel控制台命令的互斥锁。
Requires
- php: ^8.1
- illuminate/console: ^10.0
- illuminate/support: ^10.0
- ramsey/collection: ^2.0
- tajmahal86/ninja-mutex: ^0.7.0
Requires (Dev)
- ext-pdo_mysql: *
- ext-redis: *
- illuminated/testing-tools: ^9.0
- mockery/mockery: ^1.4.4
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^9.5.10
- predis/predis: ^2.1
- dev-master
- 10.0.1
- 10.0.0
- 8.x-dev
- 8.1.0
- 8.0.0
- 7.x-dev
- 7.1.0
- 7.0.0
- 6.x-dev
- 6.2.0
- 6.1.0
- 6.0.0
- 5.8.x-dev
- 5.8.1
- 5.8.0
- 5.7.x-dev
- 5.7.5
- 5.7.4
- 5.7.3
- 5.7.2
- 5.7.1
- 5.7.0
- 5.6.x-dev
- 5.6.4
- 5.6.3
- 5.6.2
- 5.6.1
- 5.6.0
- 5.5.x-dev
- 5.5.6
- 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
- dev-l9-compatibility
- dev-main
This package is not auto-updated.
Last update: 2024-09-20 14:30:16 UTC
README
Laravel Console Mutex
Laravel控制台命令的互斥锁。
目录
用法
-
通过Composer安装包
composer require illuminated/console-mutex
-
使用
Illuminated\Console\WithoutOverlapping
特性use Illuminated\Console\WithoutOverlapping; class ExampleCommand extends Command { use WithoutOverlapping; // ... }
策略
互斥锁可以通过各种策略防止重叠
file
(默认)mysql
redis
memcached
默认的file
策略适用于小型应用,这些应用部署在单个服务器上。如果你的应用更复杂,并且部署在多个节点上,你应该考虑使用另一种互斥锁策略。
你可以通过使用$mutexStrategy
字段来更改策略
class ExampleCommand extends Command { use WithoutOverlapping; protected string $mutexStrategy = 'mysql'; // ... }
或使用setMutexStrategy()
方法
class ExampleCommand extends Command { use WithoutOverlapping; public function __construct() { parent::__construct(); $this->setMutexStrategy('mysql'); } // ... }
高级
设置自定义超时
默认情况下,如果互斥锁发现命令已经在运行,它将立即退出。你可以通过设置超时来更改此行为,在此期间互斥锁可以等待另一个正在运行的命令完成执行。
你可以通过指定$mutexTimeout
字段来设置超时
class ExampleCommand extends Command { use WithoutOverlapping; // In milliseconds protected ?int $mutexTimeout = 3000; // ... }
或使用setMutexTimeout()
方法
class ExampleCommand extends Command { use WithoutOverlapping; public function __construct() { parent::__construct(); // In milliseconds $this->setMutexTimeout(3000); } // ... }
以下是$mutexTimeout
字段的处理方式
0
- 不等待(默认);{int}
- 等待给定数量的毫秒;null
- 等待正在运行的命令完成其执行;
处理多个命令
有时可能需要为多个命令有一个共享的互斥锁。你可以通过为所有这些命令设置相同的互斥锁名称来轻松实现这一点。
你应该使用getMutexName()
方法来做这件事
class ExampleCommand extends Command { use WithoutOverlapping; public function getMutexName() { return 'shared-for-command1-and-command2'; } // ... }
设置自定义存储文件夹
如果你使用的是file
策略,互斥锁文件将存储在storage/app
文件夹中。
你可以通过重写getMutexFileStorage()
方法来更改这一点
class ExampleCommand extends Command { use WithoutOverlapping; public function getMutexFileStorage() { return storage_path('my/custom/path'); } // ... }
故障排除
包含特性,但没有发生任何事情?
WithoutOverlapping
特性重写了initialize()
方法
trait WithoutOverlapping { protected function initialize(InputInterface $input, OutputInterface $output) { $this->initializeMutex(); parent::initialize($input, $output); } // ... }
如果你的命令也重写了initialize()
方法,你必须自己调用initializeMutex()
方法
class ExampleCommand extends Command { use WithoutOverlapping; protected function initialize(InputInterface $input, OutputInterface $output) { // You have to call it first $this->initializeMutex(); // Then goes your custom code $this->foo = $this->argument('foo'); $this->bar = $this->argument('bar'); $this->baz = $this->argument('baz'); } // ... }
多个特性冲突?
如果你使用另一个illuminated/console-%
包,你会得到“特性冲突”错误。
例如,如果你正在构建一个可记录的命令,它不允许重叠
class ExampleCommand extends Command { use Loggable; use WithoutOverlapping; // ... }
你会得到特性冲突,因为这两个特性都重写了initialize()
方法
如果有两个特性插入了一个同名的方法,如果没有显式解决冲突,将产生一个致命错误。
要解决这个问题 - 重写initialize()
方法并解决冲突
class ExampleCommand extends Command { use Loggable; use WithoutOverlapping; protected function initialize(InputInterface $input, OutputInterface $output) { // Initialize conflicting traits $this->initializeMutex(); $this->initializeLogging(); } // ... }
赞助商
许可证
Laravel Console Mutex是开源软件,许可协议为MIT许可证。