1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Psr\Log\LogLevel; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
8
|
|
|
use Zenstruck\ScheduleBundle\Event\AfterScheduleEvent; |
9
|
|
|
use Zenstruck\ScheduleBundle\Event\AfterTaskEvent; |
10
|
|
|
use Zenstruck\ScheduleBundle\Event\BeforeScheduleEvent; |
11
|
|
|
use Zenstruck\ScheduleBundle\Event\BeforeTaskEvent; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Kevin Bond <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
final class ScheduleLoggerSubscriber implements EventSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
private $logger; |
19
|
|
|
|
20
|
6 |
|
public function __construct(LoggerInterface $logger) |
21
|
|
|
{ |
22
|
6 |
|
$this->logger = $logger; |
23
|
6 |
|
} |
24
|
|
|
|
25
|
6 |
|
public static function getSubscribedEvents(): array |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
6 |
|
BeforeScheduleEvent::class => 'beforeSchedule', |
29
|
|
|
AfterScheduleEvent::class => 'afterSchedule', |
30
|
|
|
BeforeTaskEvent::class => 'beforeTask', |
31
|
|
|
AfterTaskEvent::class => 'afterTask', |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
6 |
|
public function beforeSchedule(BeforeScheduleEvent $event): void |
36
|
|
|
{ |
37
|
6 |
|
$dueTaskCount = \count($event->getSchedule()->due()); |
38
|
|
|
|
39
|
6 |
|
if (0 === $dueTaskCount) { |
40
|
1 |
|
$this->logger->debug('No tasks due to run.', ['total' => \count($event->getSchedule()->all())]); |
41
|
|
|
|
42
|
1 |
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
5 |
|
$this->logger->info(\sprintf('Running %s due task%s.', $dueTaskCount, $dueTaskCount > 1 ? 's' : ''), [ |
46
|
5 |
|
'total' => \count($event->getSchedule()->all()), |
47
|
5 |
|
'due' => $dueTaskCount, |
48
|
|
|
]); |
49
|
5 |
|
} |
50
|
|
|
|
51
|
6 |
|
public function afterSchedule(AfterScheduleEvent $event): void |
52
|
|
|
{ |
53
|
6 |
|
if ($event->isSkipped()) { |
54
|
1 |
|
$this->logger->info($event->getSkipReason()); |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
$total = \count($event->getResults()); |
58
|
6 |
|
$successful = \count($event->getSuccessful()); |
59
|
6 |
|
$failures = \count($event->getFailures()); |
60
|
6 |
|
$skipped = \count($event->getSkipped()); |
61
|
6 |
|
$run = \count($event->getRun()); |
62
|
6 |
|
$level = $event->isSuccessful() ? LogLevel::INFO : LogLevel::ERROR; |
63
|
|
|
|
64
|
6 |
|
if (0 === $total) { |
65
|
2 |
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
$this->logger->log($level, "{$run}/{$total} tasks ran", [ |
69
|
4 |
|
'total' => $total, |
70
|
4 |
|
'successful' => $successful, |
71
|
4 |
|
'skipped' => $skipped, |
72
|
4 |
|
'failures' => $failures, |
73
|
4 |
|
'duration' => $event->getFormattedDuration(), |
74
|
4 |
|
'memory' => $event->getFormattedMemory(), |
75
|
|
|
]); |
76
|
4 |
|
} |
77
|
|
|
|
78
|
4 |
|
public function beforeTask(BeforeTaskEvent $event): void |
79
|
|
|
{ |
80
|
4 |
|
$this->logger->info("Running \"{$event->getTask()->getType()}\": {$event->getTask()}"); |
81
|
4 |
|
} |
82
|
|
|
|
83
|
4 |
|
public function afterTask(AfterTaskEvent $event): void |
84
|
|
|
{ |
85
|
4 |
|
$result = $event->getResult(); |
86
|
4 |
|
$task = $result->getTask(); |
87
|
|
|
$context = [ |
88
|
4 |
|
'duration' => $event->getFormattedDuration(), |
89
|
4 |
|
'memory' => $event->getFormattedMemory(), |
90
|
|
|
]; |
91
|
|
|
|
92
|
4 |
|
if ($result->isSuccessful()) { |
93
|
1 |
|
$this->logger->info("Successfully ran \"{$task->getType()}\": {$task}", $context); |
94
|
|
|
|
95
|
1 |
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
$context['task'] = $task; |
99
|
3 |
|
$context['result'] = $result; |
100
|
|
|
|
101
|
3 |
|
if ($result->isSkipped()) { |
102
|
1 |
|
$this->logger->info("Skipped \"{$task->getType()}\": {$task}", $context); |
103
|
|
|
|
104
|
1 |
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
$context['output'] = $result->getOutput(); |
108
|
|
|
|
109
|
2 |
|
if ($result->isException()) { |
110
|
1 |
|
$context['exception'] = $result->getException(); |
111
|
|
|
|
112
|
1 |
|
$this->logger->error("Exception thrown when running \"{$task->getType()}\": {$task}", $context); |
113
|
|
|
|
114
|
1 |
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$this->logger->error("Failure when running \"{$task->getType()}\": {$task}", $context); |
118
|
1 |
|
} |
119
|
|
|
} |
120
|
|
|
|