6

我最近升级到 Symfony 3.4.x,由于弃用警告而重构 LockHandler 并陷入奇怪的行为。

重构前的命令代码:

class FooCommand
{
    protected function configure() { /* ... does not matter ... */ }
    protected function lock() : bool
    {
        $resource = $this->getName();
        $lock     = new \Symfony\Component\Filesystem\LockHandler($resource);

        return $lock->lock();
    }
    protected function execute()
    {
        if (!$this->lock()) return 0;

        // Execute some task
    }
}

它可以防止同时运行两个命令——第二个只是完成而不做工作。那很好。

但是在建议重构之后,它允许同时运行许多命令。这是失败的。如何防止执行?新代码:

class FooCommand
{
    protected function configure() { /* ... does not matter ... */ }
    protected function lock() : bool
    {
        $resource = $this->getName();
        $store    = new \Symfony\Component\Lock\FlockStore(sys_get_temp_dir());
        $factory  = new \Symfony\Component\Lock\Factory($store);
        $lock     = $factory->createLock($resource);

        return $lock->acquire();
    }
    protected function execute()
    {
        if (!$this->lock()) return 0;

        // Execute some task
    }
}

NB #1:我不关心很多服务器,只关心一个应用程序实例。

NB #2:如果进程被杀死,那么新命令必须解锁并运行。

4

2 回答 2

7

您必须使用 LockableTrait 特征

    use Symfony\Component\Console\Command\LockableTrait;
    use Symfony\Component\Console\Command\Command

    class FooCommand  extends Command
    {
        use LockableTrait;
.....
protected function execute(InputInterface $input, OutputInterface $output)
    {
        if (!$this->lock()) {
            $output->writeln('The command is already running in another process.');

            return 0;
        }
// If you prefer to wait until the lock is released, use this:
        // $this->lock(null, true);

        // ...

        // if not released explicitly, Symfony releases the lock
        // automatically when the execution of the command ends
        $this->release();

}
于 2017-12-12T16:24:07.263 回答
2

添加到 Mohamed 的答案中,为命令储物柜分配一个 ID 很重要。

否则锁会与其他命令有并发问题,特别是如果你有不同的环境(测试、生产、阶段......)。您会看到该命令没有按预期的周期执行。

可以在lock()语句本身上分配此 ID。

    use Symfony\Component\Console\Command\LockableTrait;
    use Symfony\Component\Console\Command\Command

    class FooCommand  extends Command
    {
       use LockableTrait;
       .....
       protected function execute(InputInterface $input, OutputInterface $output)
       {
         if (!$this->lock('FooCommand'.getenv('APP_ENV'))) {
            $output->writeln('The command is already running in another process.');
            return 0;
         }

         $this->release();
       }
    }
于 2020-03-27T15:11:59.290 回答