mirror of
https://github.com/shlinkio/shlink.git
synced 2026-02-28 12:13:13 +08:00
45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Shlinkio\Shlink\CLI\Command\Util;
|
|
|
|
use Shlinkio\Shlink\CLI\Util\ExitCode;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Lock\LockFactory;
|
|
|
|
use function sprintf;
|
|
|
|
abstract class AbstractLockedCommand extends Command
|
|
{
|
|
public function __construct(private readonly LockFactory $locker)
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
final protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$lockConfig = $this->getLockConfig();
|
|
$lock = $this->locker->createLock($lockConfig->lockName, $lockConfig->ttl, $lockConfig->isBlocking);
|
|
|
|
if (! $lock->acquire($lockConfig->isBlocking)) {
|
|
$output->writeln(
|
|
sprintf('<comment>Command "%s" is already in progress. Skipping.</comment>', $lockConfig->lockName),
|
|
);
|
|
return ExitCode::EXIT_WARNING;
|
|
}
|
|
|
|
try {
|
|
return $this->lockedExecute($input, $output);
|
|
} finally {
|
|
$lock->release();
|
|
}
|
|
}
|
|
|
|
abstract protected function lockedExecute(InputInterface $input, OutputInterface $output): int;
|
|
|
|
abstract protected function getLockConfig(): LockedCommandConfig;
|
|
}
|