Symfony2 - check if command is running

Is there a way to check if the symfony command is running? I have a command that works without a timeout and consumes data, and I need to know if the command is running.

+4
source share
1 answer

You can use a lock to ensure that a command is run only once at a time. Symfony provides an assistant LockHandler, but you can also easily do this with simple 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();
    }
}
+15
source

Source: https://habr.com/ru/post/1621230/


All Articles