Symfony2 - 检查命令是否正在运行

nik*_*oss 4 symfony

有没有办法检查symfony命令是否已经运行?我有一个没有超时运行并消耗数据的命令,我需要知道命令是否已经运行.

Ste*_*rig 19

您可以使用锁定来确保命令一次只运行一次.Symfony LockHandler为此提供了帮助,但您也可以使用普通PHP轻松完成此操作.

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\LockHandler;

class WhateverCommand extends Command
{
    protected function configure() { }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $lock = new LockHandler('a_unique_id_for_your_command');
        if (!$lock->lock()) {
            $output->writeln('This command is already running in another process.');

            return 0;
        }

        // ... do some task

        $lock->release();
    }
}
Run Code Online (Sandbox Code Playgroud)