ScheduleLoggerSubscriber::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
    /** @var LoggerInterface */
19
    private $logger;
20 7
21
    public function __construct(LoggerInterface $logger)
22 7
    {
23 7
        $this->logger = $logger;
24
    }
25 7
26
    public static function getSubscribedEvents(): array
27
    {
28 7
        return [
29
            BeforeScheduleEvent::class => 'beforeSchedule',
30
            AfterScheduleEvent::class => 'afterSchedule',
31
            BeforeTaskEvent::class => 'beforeTask',
32
            AfterTaskEvent::class => 'afterTask',
33
        ];
34
    }
35 7
36
    public function beforeSchedule(BeforeScheduleEvent $event): void
37 7
    {
38
        $context = $event->runContext();
39 7
40 7
        $allTaskCount = \count($context->getSchedule()->all());
41
        $dueTaskCount = \count($context->dueTasks());
42 7
43 1
        if (0 === $dueTaskCount) {
44
            $this->logger->debug('No tasks due to run.', ['total' => $allTaskCount]);
45 1
46
            return;
47
        }
48 6
49 6
        $message = \sprintf('%s %d %stask%s.',
50 6
            $context->isForceRun() ? 'Force running' : 'Running',
51 6
            $dueTaskCount,
52 6
            $context->isForceRun() ? '' : 'due ',
53
            $dueTaskCount > 1 ? 's' : ''
54
        );
55 6
56 6
        $this->logger->info($message, [
57 6
            'total' => $allTaskCount,
58
            'due' => $dueTaskCount,
59 6
        ]);
60
    }
61 7
62
    public function afterSchedule(AfterScheduleEvent $event): void
63 7
    {
64
        $context = $event->runContext();
65 7
66 1
        if ($context->isSkipped()) {
67
            $this->logger->info($context->getSkipReason());
68 1
69
            return;
70
        }
71 6
72 6
        $total = \count($context->getResults());
73 6
        $successful = \count($context->getSuccessful());
74 6
        $failures = \count($context->getFailures());
75 6
        $skipped = \count($context->getSkipped());
76 6
        $run = \count($context->getRun());
77
        $level = $context->isSuccessful() ? LogLevel::INFO : LogLevel::ERROR;
78 6
79 1
        if (0 === $total) {
80
            return;
81
        }
82 5
83 5
        $this->logger->log($level, "{$run}/{$total} tasks ran", [
84 5
            'total' => $total,
85 5
            'successful' => $successful,
86 5
            'skipped' => $skipped,
87 5
            'failures' => $failures,
88 5
            'duration' => $context->getFormattedDuration(),
89 5
            'memory' => $context->getFormattedMemory(),
90
            'forced' => $context->isForceRun(),
91 5
        ]);
92
    }
93 5
94
    public function beforeTask(BeforeTaskEvent $event): void
95 5
    {
96 5
        $context = $event->runContext();
97
        $task = $context->getTask();
98 5
99 5
        $this->logger->info(\sprintf('%s "%s"',
100 5
            $context->getScheduleRunContext()->isForceRun() ? 'Force running' : 'Running',
101 5
            $task
102 5
        ), ['id' => $task->getId()]);
103
    }
104 5
105
    public function afterTask(AfterTaskEvent $event): void
106 5
    {
107
        $context = $event->runContext();
108 5
109 5
        $result = $context->getResult();
110 5
        $task = $result->getTask();
111
        $logContext = ['id' => $task->getId()];
112 5
113 1
        if ($result->isSkipped()) {
114
            $this->logger->info("Skipped \"{$task}\" ({$result->getDescription()})", $logContext);
115 1
116
            return;
117
        }
118 4
119 4
        $logContext['result'] = $result->getDescription();
120 4
        $logContext['duration'] = $context->getFormattedDuration();
121 4
        $logContext['memory'] = $context->getFormattedMemory();
122
        $logContext['forced'] = $context->getScheduleRunContext()->isForceRun();
123 4
124 2
        if ($result->isSuccessful()) {
125
            $this->logger->info("Successfully ran \"{$task}\"", $logContext);
126 2
127
            return;
128
        }
129 2
130 1
        if ($result->getOutput()) {
131
            $logContext['output'] = $result->getOutput();
132
        }
133 2
134 1
        if (!$result->isException()) {
135
            $this->logger->error("Failure when running \"{$task}\"", $logContext);
136 1
137
            return;
138
        }
139 1
140
        $logContext['exception'] = $result->getException();
141 1
142 1
        $this->logger->critical("Exception thrown when running \"{$task}\"", $logContext);
143
    }
144
}
145