1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
7
|
|
|
use Zenstruck\ScheduleBundle\Event\BuildScheduleEvent; |
8
|
|
|
use Zenstruck\ScheduleBundle\Schedule; |
9
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task; |
10
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask; |
11
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\CompoundTask; |
12
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\NullTask; |
13
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\ProcessTask; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Kevin Bond <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class TaskConfigurationSubscriber implements EventSubscriberInterface |
19
|
|
|
{ |
20
|
|
|
private const PROCESS_TASK_PREFIX = 'bash:'; |
21
|
|
|
|
22
|
|
|
private $config; |
23
|
|
|
private $taskLocator; |
24
|
|
|
|
25
|
8 |
|
public function __construct(array $config, ContainerInterface $taskLocator) |
26
|
|
|
{ |
27
|
8 |
|
$this->config = $config; |
28
|
8 |
|
$this->taskLocator = $taskLocator; |
29
|
8 |
|
} |
30
|
|
|
|
31
|
8 |
|
public static function getSubscribedEvents(): array |
32
|
|
|
{ |
33
|
8 |
|
return [BuildScheduleEvent::class => 'configureTasks']; |
34
|
|
|
} |
35
|
|
|
|
36
|
8 |
|
public function configureTasks(BuildScheduleEvent $event): void |
37
|
|
|
{ |
38
|
8 |
|
foreach ($this->config as $taskConfig) { |
39
|
8 |
|
$this->addTask($event->getSchedule(), $taskConfig); |
40
|
|
|
} |
41
|
8 |
|
} |
42
|
|
|
|
43
|
8 |
|
private function addTask(Schedule $schedule, array $config): void |
44
|
|
|
{ |
45
|
8 |
|
$task = $this->getTask($config['command']); |
46
|
|
|
|
47
|
8 |
|
$task->cron($config['frequency']); |
48
|
|
|
|
49
|
8 |
|
if ($config['description']) { |
50
|
4 |
|
$task->description($config['description']); |
51
|
|
|
} |
52
|
|
|
|
53
|
8 |
|
if ($config['timezone']) { |
54
|
1 |
|
$task->timezone($config['timezone']); |
55
|
|
|
} |
56
|
|
|
|
57
|
8 |
|
if ($config['without_overlapping']['enabled']) { |
58
|
3 |
|
$task->withoutOverlapping($config['without_overlapping']['ttl']); |
59
|
|
|
} |
60
|
|
|
|
61
|
8 |
|
if ($config['only_between']['enabled']) { |
62
|
1 |
|
$task->onlyBetween($config['only_between']['start'], $config['only_between']['end']); |
63
|
|
|
} |
64
|
|
|
|
65
|
8 |
|
if ($config['unless_between']['enabled']) { |
66
|
1 |
|
$task->unlessBetween($config['unless_between']['start'], $config['unless_between']['end']); |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
if ($config['ping_before']['enabled']) { |
70
|
1 |
|
$task->pingBefore($config['ping_before']['url'], $config['ping_before']['method'], $config['ping_before']['options']); |
71
|
|
|
} |
72
|
|
|
|
73
|
8 |
|
if ($config['ping_after']['enabled']) { |
74
|
1 |
|
$task->pingAfter($config['ping_after']['url'], $config['ping_after']['method'], $config['ping_after']['options']); |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
if ($config['ping_on_success']['enabled']) { |
78
|
1 |
|
$task->pingOnSuccess($config['ping_on_success']['url'], $config['ping_on_success']['method'], $config['ping_on_success']['options']); |
79
|
|
|
} |
80
|
|
|
|
81
|
8 |
|
if ($config['ping_on_failure']['enabled']) { |
82
|
1 |
|
$task->pingOnFailure($config['ping_on_failure']['url'], $config['ping_on_failure']['method'], $config['ping_on_failure']['options']); |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
if ($config['email_after']['enabled']) { |
86
|
1 |
|
$task->emailAfter($config['email_after']['to'], $config['email_after']['subject']); |
87
|
|
|
} |
88
|
|
|
|
89
|
8 |
|
if ($config['email_on_failure']['enabled']) { |
90
|
1 |
|
$task->emailOnFailure($config['email_on_failure']['to'], $config['email_on_failure']['subject']); |
91
|
|
|
} |
92
|
|
|
|
93
|
8 |
|
$schedule->add($task); |
94
|
8 |
|
} |
95
|
|
|
|
96
|
8 |
|
private function getTask(array $commands): Task |
97
|
|
|
{ |
98
|
8 |
|
if (1 === \count($commands)) { |
99
|
6 |
|
return $this->getSingleTask(\array_values($commands)[0]); |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
$task = new CompoundTask(); |
103
|
|
|
|
104
|
3 |
|
foreach ($commands as $description => $command) { |
105
|
3 |
|
$subTask = $this->getSingleTask($command); |
106
|
|
|
|
107
|
3 |
|
if (!\is_numeric($description)) { |
108
|
2 |
|
$subTask->description($description); |
109
|
|
|
} |
110
|
|
|
|
111
|
3 |
|
$task->add($subTask); |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
return $task; |
115
|
|
|
} |
116
|
|
|
|
117
|
8 |
|
private function getSingleTask(?string $command): Task |
118
|
|
|
{ |
119
|
8 |
|
if (null === $command) { |
120
|
1 |
|
return new NullTask('to be overridden'); |
121
|
|
|
} |
122
|
|
|
|
123
|
7 |
|
if (0 === \mb_strpos($command, '@')) { |
124
|
1 |
|
return clone $this->taskLocator->get(\mb_substr($command, 1)); |
125
|
|
|
} |
126
|
|
|
|
127
|
7 |
|
if (0 !== \mb_strpos($command, self::PROCESS_TASK_PREFIX)) { |
128
|
6 |
|
return new CommandTask($command); |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
return new ProcessTask(\trim(\mb_substr($command, \mb_strlen(self::PROCESS_TASK_PREFIX)))); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|