-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLockingCommand.php
More file actions
69 lines (57 loc) · 2.04 KB
/
Copy pathLockingCommand.php
File metadata and controls
69 lines (57 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
namespace MatchBot\Application\Commands;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\LockInterface;
/**
* Base class for Commands which should be protected against overlapping runs of the same Command
* name, for 30 minutes.
*/
abstract class LockingCommand extends Command
{
private LockFactory $lockFactory; // @phpstan-ignore property.uninitialized
private LockInterface $lock; // @phpstan-ignore property.uninitialized
private LoggerInterface $logger; // @phpstan-ignore property.uninitialized
public function setLockFactory(LockFactory $lockFactory): void
{
$this->lockFactory = $lockFactory;
}
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->start($input, $output);
if ($this->getLock()) {
$return = $this->doExecute($input, $output);
$this->releaseLock();
} else {
$message = $this->getName() ?? self::class . ' did nothing as another instance had the lock.';
$output->writeln($message);
$this->logger->warning($message);
return 0; // Log at warning level to help monitoring volume but don't fire error alarms.
}
$this->finish($input, $output);
return $return;
}
private function getLock(): bool
{
$name = $this->getName();
\assert(\is_string($name), 'Command name must be defined');
$this->lock = $this->lockFactory->createLock(
resource: $name,
ttl: 24 * 60 * 60, // 1 day
autoRelease: true // auto-release on process end
);
return $this->lock->acquire(false);
}
private function releaseLock(): void
{
$this->lock->release();
}
}