oriceon/console-mutex
Composer 安装命令:
composer require oriceon/console-mutex
包简介
Mutex for Laravel Console Commands. Fork for Laravel 10.x
README 文档
README
Laravel Console Mutex
This is a fork from the original Repository to support Laravel v10.x
Mutex for Laravel Console Commands.
| Laravel | Console Mutex |
|---|---|
| 10.x | 10.x |
| 9.x | 9.x |
| 8.x | 8.x |
| 7.x | 7.x |
| 6.x | 6.x |
| 5.8.* | 5.8.* |
| 5.7.* | 5.7.* |
| 5.6.* | 5.6.* |
| 5.5.* | 5.5.* |
| 5.4.* | 5.4.* |
| 5.3.* | 5.3.* |
| 5.2.* | 5.2.* |
| 5.1.* | 5.1.* |
Table of contents
Usage
-
Install the package via Composer:
composer require srd2010/console-mutex
-
Use
Illuminated\Console\WithoutOverlappingtrait:use Illuminated\Console\WithoutOverlapping; class ExampleCommand extends Command { use WithoutOverlapping; // ... }
Strategies
Mutex can prevent overlapping by using various strategies:
file(default)mysqlredismemcached
The default file strategy is acceptable for small applications, which are deployed on a single server.
If your application is more complex and deployed on several nodes, you should consider using another mutex strategy.
You can change strategy by using the $mutexStrategy field:
class ExampleCommand extends Command { use WithoutOverlapping; protected string $mutexStrategy = 'mysql'; // ... }
Or by using the setMutexStrategy() method:
class ExampleCommand extends Command { use WithoutOverlapping; public function __construct() { parent::__construct(); $this->setMutexStrategy('mysql'); } // ... }
Advanced
Set custom timeout
By default, if mutex sees that the command is already running, it will immediately quit. You can change that behavior by setting a timeout in which mutex can wait for another running command to finish its execution.
You can set the timeout by specifying the $mutexTimeout field:
class ExampleCommand extends Command { use WithoutOverlapping; // In milliseconds protected ?int $mutexTimeout = 3000; // ... }
Or by using the setMutexTimeout() method:
class ExampleCommand extends Command { use WithoutOverlapping; public function __construct() { parent::__construct(); // In milliseconds $this->setMutexTimeout(3000); } // ... }
Here's how the $mutexTimeout field is treated:
0- no waiting (default);{int}- wait for the given number of milliseconds;null- wait for the running command to finish its execution;
Handle multiple commands
Sometimes it might be useful to have a shared mutex for multiple commands. You can easily achieve that by setting the same mutex name for all of those commands.
You should use the getMutexName() method for that:
class ExampleCommand extends Command { use WithoutOverlapping; public function getMutexName() { return 'shared-for-command1-and-command2'; } // ... }
Set custom storage folder
If you're using the file strategy, mutex files would be stored in the storage/app folder.
You can change that by overriding the getMutexFileStorage() method:
class ExampleCommand extends Command { use WithoutOverlapping; public function getMutexFileStorage() { return storage_path('my/custom/path'); } // ... }
Troubleshooting
Trait included, but nothing happens?
WithoutOverlapping trait overrides the initialize() method:
trait WithoutOverlapping { protected function initialize(InputInterface $input, OutputInterface $output) { $this->initializeMutex(); parent::initialize($input, $output); } // ... }
If your command overrides the initialize() method too, you have to call the initializeMutex() method by yourself:
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'); } // ... }
Several traits conflict?
If you're using another illuminated/console-% package, you'll get the "traits conflict" error.
For example, if you're building a loggable command, which doesn't allow overlapping:
class ExampleCommand extends Command { use Loggable; use WithoutOverlapping; // ... }
You'll get the traits conflict, because both of those traits are overriding the initialize() method:
If two traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.
To fix that - override the initialize() method and resolve the conflict:
class ExampleCommand extends Command { use Loggable; use WithoutOverlapping; protected function initialize(InputInterface $input, OutputInterface $output) { // Initialize conflicting traits $this->initializeMutex(); $this->initializeLogging(); } // ... }
Original Sponsors
License
Laravel Console Mutex is open-sourced software licensed under the MIT license.
oriceon/console-mutex 适用场景与选型建议
oriceon/console-mutex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 05 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「console」 「command」 「laravel」 「semaphore」 「mutex」 「locker」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 oriceon/console-mutex 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 oriceon/console-mutex 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 oriceon/console-mutex 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This class provides you with an easy-to-use interface to progress bars.
A Wrapper for exec and it's results
Symfony bundle to monitor and execute commands
A Laravel package to retrieve data from Google Search Console
Additional artisan commands for Laravel
Shell command module for PHP.
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-12



