illuminated/console-mutex

用于 Laravel 控制台命令的互斥锁。


README

Mutex for Laravel Console Commands

Laravel Console Mutex

Buy me a coffee

StyleCI Build Status Coverage Status

Packagist Version Packagist Stars Packagist Downloads Packagist License

用于 Laravel 控制台命令的互斥锁。

Laravel Console Mutex - Demo

目录

用法

  1. 使用 Composer 安装包

    composer require illuminated/console-mutex
  2. 使用 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 Idea
Material Theme UI Plugin

许可

Laravel Console Mutex是开源软件,许可协议为MIT许可

Buy me a coffee