|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
6
|
|
|
use Zenstruck\ScheduleBundle\Event\ScheduleBuildEvent; |
|
7
|
|
|
use Zenstruck\ScheduleBundle\Schedule; |
|
8
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask; |
|
9
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\ProcessTask; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Kevin Bond <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
final class ConfigureTasksSubscriber implements EventSubscriberInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private $config; |
|
17
|
|
|
|
|
18
|
3 |
|
public function __construct(array $config) |
|
19
|
|
|
{ |
|
20
|
3 |
|
$this->config = $config; |
|
21
|
3 |
|
} |
|
22
|
|
|
|
|
23
|
3 |
|
public static function getSubscribedEvents(): array |
|
24
|
|
|
{ |
|
25
|
3 |
|
return [ScheduleBuildEvent::class => 'configureTasks']; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
3 |
|
public function configureTasks(ScheduleBuildEvent $event): void |
|
29
|
|
|
{ |
|
30
|
3 |
|
foreach ($this->config as $taskConfig) { |
|
31
|
3 |
|
$this->addTask($event->getSchedule(), $taskConfig); |
|
32
|
|
|
} |
|
33
|
3 |
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
private function addTask(Schedule $schedule, array $config): void |
|
36
|
|
|
{ |
|
37
|
3 |
|
$task = 'command' === $config['type'] ? new CommandTask($config['command']) : new ProcessTask($config['command']); |
|
38
|
|
|
|
|
39
|
3 |
|
$task->cron($config['frequency']); |
|
40
|
|
|
|
|
41
|
3 |
|
if ($config['description']) { |
|
42
|
1 |
|
$task->description($config['description']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
if ($config['without_overlapping']['enabled']) { |
|
46
|
1 |
|
$task->withoutOverlapping($config['without_overlapping']['ttl']); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
if ($config['between']['enabled']) { |
|
50
|
1 |
|
$task->between($config['between']['start'], $config['between']['end']); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
if ($config['unless_between']['enabled']) { |
|
54
|
1 |
|
$task->unlessBetween($config['unless_between']['start'], $config['unless_between']['end']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
if ($config['ping_before']['enabled']) { |
|
58
|
1 |
|
$task->pingBefore($config['ping_before']['url'], $config['ping_before']['method'], $config['ping_before']['options']); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
if ($config['ping_after']['enabled']) { |
|
62
|
1 |
|
$task->pingAfter($config['ping_after']['url'], $config['ping_after']['method'], $config['ping_after']['options']); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
if ($config['ping_on_success']['enabled']) { |
|
66
|
1 |
|
$task->pingOnSuccess($config['ping_on_success']['url'], $config['ping_on_success']['method'], $config['ping_on_success']['options']); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
if ($config['ping_on_failure']['enabled']) { |
|
70
|
1 |
|
$task->pingOnFailure($config['ping_on_failure']['url'], $config['ping_on_failure']['method'], $config['ping_on_failure']['options']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
if ($config['email_after']['enabled']) { |
|
74
|
1 |
|
$task->emailAfter($config['email_after']['to'], $config['email_after']['subject']); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
if ($config['email_on_failure']['enabled']) { |
|
78
|
1 |
|
$task->emailOnFailure($config['email_on_failure']['to'], $config['email_on_failure']['subject']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
$schedule->add($task); |
|
82
|
3 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|