1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Style\OutputStyle; |
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
7
|
|
|
use Zenstruck\ScheduleBundle\Event\AfterScheduleEvent; |
8
|
|
|
use Zenstruck\ScheduleBundle\Event\AfterTaskEvent; |
9
|
|
|
use Zenstruck\ScheduleBundle\Event\BeforeScheduleEvent; |
10
|
|
|
use Zenstruck\ScheduleBundle\Event\BeforeTaskEvent; |
11
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\Result; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Kevin Bond <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
final class ScheduleConsoleOutputSubscriber implements EventSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
private $io; |
19
|
|
|
|
20
|
9 |
|
public function __construct(OutputStyle $io) |
21
|
|
|
{ |
22
|
9 |
|
$this->io = $io; |
23
|
9 |
|
} |
24
|
|
|
|
25
|
9 |
|
public static function getSubscribedEvents(): array |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
9 |
|
BeforeScheduleEvent::class => 'beforeSchedule', |
29
|
|
|
AfterScheduleEvent::class => 'afterSchedule', |
30
|
|
|
BeforeTaskEvent::class => 'beforeTask', |
31
|
|
|
AfterTaskEvent::class => 'afterTask', |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
9 |
|
public function afterSchedule(AfterScheduleEvent $event): void |
36
|
|
|
{ |
37
|
9 |
|
if ($event->isSkipped()) { |
38
|
1 |
|
$this->io->note($event->getSkipReason()); |
39
|
|
|
|
40
|
1 |
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
8 |
|
$total = \count($event->getResults()); |
44
|
8 |
|
$successful = \count($event->getSuccessful()); |
45
|
8 |
|
$failures = \count($event->getFailures()); |
46
|
8 |
|
$skipped = \count($event->getSkipped()); |
47
|
8 |
|
$run = \count($event->getRun()); |
48
|
8 |
|
$messages = ["{$run}/{$total} tasks ran"]; |
49
|
|
|
|
50
|
8 |
|
if (0 === $total) { |
51
|
1 |
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
7 |
|
if ($successful > 0) { |
55
|
7 |
|
$messages[] = "{$successful} succeeded"; |
56
|
|
|
} |
57
|
|
|
|
58
|
7 |
|
if ($skipped > 0) { |
59
|
1 |
|
$messages[] = "{$skipped} skipped"; |
60
|
|
|
} |
61
|
|
|
|
62
|
7 |
|
if ($failures > 0) { |
63
|
4 |
|
$messages[] = "{$failures} failed"; |
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
$messages = \implode(', ', $messages).'.'; |
67
|
7 |
|
$messages .= " (Duration: {$event->getFormattedDuration()}, Memory: {$event->getFormattedMemory()})"; |
68
|
|
|
|
69
|
7 |
|
$this->io->{$event->isSuccessful() ? 'success' : 'error'}($messages); |
70
|
7 |
|
} |
71
|
|
|
|
72
|
9 |
|
public function beforeSchedule(BeforeScheduleEvent $event): void |
73
|
|
|
{ |
74
|
9 |
|
$dueTaskCount = \count($event->getSchedule()->due()); |
75
|
|
|
|
76
|
9 |
|
if (0 === $dueTaskCount) { |
77
|
1 |
|
$this->io->note(\sprintf('No tasks due to run. (%s total tasks)', \count($event->getSchedule()->all()))); |
78
|
|
|
|
79
|
1 |
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
8 |
|
$this->io->comment(\sprintf( |
83
|
8 |
|
'Running <info>%s</info> due task%s. (%s total tasks)', |
84
|
8 |
|
$dueTaskCount, |
85
|
8 |
|
$dueTaskCount > 1 ? 's' : '', |
86
|
8 |
|
\count($event->getSchedule()->all()) |
87
|
|
|
)); |
88
|
8 |
|
} |
89
|
|
|
|
90
|
7 |
|
public function beforeTask(BeforeTaskEvent $event): void |
91
|
|
|
{ |
92
|
7 |
|
$this->io->text("<comment>Running {$event->getTask()->getType()}:</comment> {$event->getTask()}"); |
93
|
7 |
|
} |
94
|
|
|
|
95
|
7 |
|
public function afterTask(AfterTaskEvent $event): void |
96
|
|
|
{ |
97
|
7 |
|
if ($this->io->isVerbose() && $output = $event->getResult()->getOutput()) { |
98
|
3 |
|
$this->io->text('---begin output---'); |
99
|
3 |
|
$this->io->write($output); |
100
|
3 |
|
$this->io->text('---end output---'); |
101
|
|
|
} |
102
|
|
|
|
103
|
7 |
|
$this->io->text(\sprintf('%s (<comment>Duration:</comment> %s, <comment>Memory:</comment> %s)', |
104
|
7 |
|
$this->afterTaskMessage($event->getResult()), |
105
|
7 |
|
$event->getFormattedDuration(), |
106
|
7 |
|
$event->getFormattedMemory() |
107
|
|
|
)); |
108
|
7 |
|
$this->io->newLine(); |
109
|
7 |
|
} |
110
|
|
|
|
111
|
7 |
|
private function afterTaskMessage(Result $result): string |
112
|
|
|
{ |
113
|
7 |
|
if ($result->isException()) { |
114
|
2 |
|
return "<error>Exception:</error> {$result->getDescription()}"; |
115
|
|
|
} |
116
|
|
|
|
117
|
7 |
|
if ($result->isFailure()) { |
118
|
2 |
|
return "<error>Failure:</error> {$result->getDescription()}"; |
119
|
|
|
} |
120
|
|
|
|
121
|
7 |
|
if ($result->isSkipped()) { |
122
|
1 |
|
return "<question>Skipped:</question> {$result->getDescription()}"; |
123
|
|
|
} |
124
|
|
|
|
125
|
7 |
|
return '<info>Success.</info>'; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|