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