Я обновился до Symfony 3.4.*, чтобы воспользоваться преимуществами нового компонента блокировки. Однако кажется, что это работает в dev, однако в производстве блокировка всегда приобретается. Вот мой код:
Базовая командная оболочка:
<?php
namespace CoreBundle\Command;
ini_set('max_execution_time', 3600);
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Lock\Factory;
use Symfony\Component\Lock\Store\SemaphoreStore;
use Symfony\Component\Console\Command\LockableTrait;
class BaseCommandWrapper extends ContainerAwareCommand {
use LockableTrait;
function start($commandName) {
$this->commandName = $commandName;
$store = new SemaphoreStore();
$factory = new Factory($store);
$this->lock = $factory->createLock($this->commandName);
if (!$this->lock->acquire()) {
echo 'This command is already running in another process.' . PHP_EOL;
return false;
}
echo "Lock aquired" . PHP_EOL;
return $this->lock;
}
function stop() {
echo "Releasing lock" . PHP_EOL;
$this->lock->release();
}
}
?>
Сама команда:
class SomeCommand extends BaseCommandWrapper
{
protected function configure()
{
$this
->setName('processSomeCommand')
->setDescription('Process Some Command')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($this->start($this->getName()) === false) {
return 0;
}
sleep(60);
$this->stop();
}
Все команды запускаются cron, который в этом случае запускается каждую секунду.