-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathStopWorkerCommand.php
More file actions
74 lines (60 loc) · 2.12 KB
/
StopWorkerCommand.php
File metadata and controls
74 lines (60 loc) · 2.12 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
70
71
72
73
74
<?php
declare(strict_types=1);
namespace SchedulerBundle\Command;
use Psr\Cache\CacheItemPoolInterface;
use SchedulerBundle\EventListener\StopWorkerOnNextTaskSubscriber;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use function microtime;
/**
* @author Guillaume Loulier <[email protected]>
*/
#[AsCommand(
name: 'scheduler:stop-worker',
description: 'Stops the worker after the current task'
)]
final class StopWorkerCommand extends Command
{
public function __construct(private CacheItemPoolInterface $stopWorkerCacheItemPool)
{
parent::__construct();
}
protected function configure(): void
{
$this
->setHelp(
help:
<<<'EOF'
The <info>%command.name%</info> command stop the worker after the current task.
<info>php %command.full_name%</info>
The worker will *not* be restarted once stopped.
EOF
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$style = new SymfonyStyle(input: $input, output: $output);
try {
$item = $this->stopWorkerCacheItemPool->getItem(key: StopWorkerOnNextTaskSubscriber::STOP_NEXT_TASK_TIMESTAMP_KEY);
$item->set(value: microtime(as_float: true));
$this->stopWorkerCacheItemPool->save(item: $item);
} catch (Throwable $e) {
$style->error(message: [
'An error occurred while trying to stop the worker:',
$e->getMessage(),
]);
return Command::FAILURE;
}
$style->success(message: [
'The worker will be stopped according the following conditions:',
' - The current task has been executed',
' - The sleep phase is over',
]);
return Command::SUCCESS;
}
}