Passed
Push — master ( b05304...84ccec )
by Harry
02:04
created

PoolLogger::onRunFailed()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
ccs 13
cts 13
cp 1
cc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of graze/parallel-process.
4
 *
5
 * Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/parallel-process/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/parallel-process
12
 */
13
14
namespace Graze\ParallelProcess\Monitor;
15
16
use Exception;
17
use Graze\ParallelProcess\Event\PoolRunEvent;
18
use Graze\ParallelProcess\Event\RunEvent;
19
use Graze\ParallelProcess\PoolInterface;
20
use Graze\ParallelProcess\RunInterface;
21
use Psr\Log\LoggerInterface;
22
23
class PoolLogger
24
{
25
    /** @var LoggerInterface */
26
    private $logger;
27
28
    /**
29
     * LoggingMonitor constructor.
30
     *
31
     * @param LoggerInterface $logger
32
     */
33 6
    public function __construct(LoggerInterface $logger)
34
    {
35 6
        $this->logger = $logger;
36 6
    }
37
38
    /**
39
     * Monitor a Pool or Run, and log all activity
40
     *
41
     * @param PoolInterface|RunInterface $item
42
     */
43 6
    public function monitor($item)
44
    {
45 6
        if ($item instanceof PoolInterface) {
46 2
            $item->addListener(PoolRunEvent::POOL_RUN_ADDED, [$this, 'onPoolRunAdded']);
47 2
            $item->addListener(PoolRunEvent::UPDATED, [$this, 'onPoolUpdated']);
48 2
            array_map([$this, 'monitor'], $item->getAll());
49
        }
50 6
        if ($item instanceof RunInterface) {
51 6
            $item->addListener(RunEvent::STARTED, [$this, 'onRunStarted']);
52 6
            $item->addListener(RunEvent::SUCCESSFUL, [$this, 'onRunSuccessful']);
53 6
            $item->addListener(RunEvent::FAILED, [$this, 'onRunFailed']);
54 6
            $item->addListener(RunEvent::COMPLETED, [$this, 'onRunCompleted']);
55
        }
56 6
    }
57
58
    /**
59
     * @param PoolRunEvent $event
60
     */
61 1
    public function onPoolRunAdded(PoolRunEvent $event)
62
    {
63 1
        $this->logger->debug(
64 1
            sprintf(
65 1
                'pool [%s:%s]: Run [%s:%s] has been added',
66 1
                get_class($event->getPool()),
67 1
                spl_object_hash($event->getPool()),
68 1
                get_class($event->getRun()),
69 1
                spl_object_hash($event->getRun())
70
            ),
71 1
            array_merge($this->getTags($event->getPool()), $this->getTags($event->getRun()))
72
        );
73 1
        $this->monitor($event->getRun());
74 1
    }
75
76
    /**
77
     * @param RunEvent $event
78
     */
79 1
    public function onPoolUpdated(RunEvent $event)
80
    {
81 1
        $pool = $event->getRun();
82 1
        if ($pool instanceof PoolInterface) {
83 1
            $this->logger->debug(
84 1
                sprintf('pool [%s:%s]: updated', get_class($event->getRun()), spl_object_hash($pool)),
85 1
                $this->getTags($pool)
86
            );
87
        }
88 1
    }
89
90
    /**
91
     * @param RunEvent $event
92
     */
93 5
    public function onRunStarted(RunEvent $event)
94
    {
95 5
        $this->logger->debug(
96 5
            sprintf('run [%s:%s]: has started', get_class($event->getRun()), spl_object_hash($event->getRun())),
97 5
            $this->getTags($event->getRun())
98
        );
99 5
    }
100
101
    /**
102
     * @param RunEvent $event
103
     */
104 4
    public function onRunSuccessful(RunEvent $event)
105
    {
106 4
        $this->logger->debug(
107 4
            sprintf(
108 4
                'run [%s:%s]: successfully finished',
109 4
                get_class($event->getRun()),
110 4
                spl_object_hash($event->getRun())
111
            ),
112 4
            $this->getTags($event->getRun())
113
        );
114 4
    }
115
116
    /**
117
     * @param RunEvent $event
118
     */
119 1
    public function onRunFailed(RunEvent $event)
120
    {
121 1
        $errors = array_map(
122 1
            function (Exception $e) {
123 1
                return $e->getMessage();
124 1
            },
125 1
            $event->getRun()->getExceptions()
126
        );
127 1
        $this->logger->debug(
128 1
            sprintf(
129 1
                'run [%s:%s]: failed - %s',
130 1
                get_class($event->getRun()),
131 1
                spl_object_hash($event->getRun()),
132 1
                count($errors) > 0 ? reset($errors) : ''
133
            ),
134 1
            array_merge(['errors' => $errors], $this->getTags($event->getRun()))
135
        );
136 1
    }
137
138
    /**
139
     * @param RunEvent $event
140
     */
141 5
    public function onRunCompleted(RunEvent $event)
142
    {
143 5
        $this->logger->debug(
144 5
            sprintf(
145 5
                'run [%s:%s]: has finished running',
146 5
                get_class($event->getRun()),
147 5
                spl_object_hash($event->getRun())
148
            ),
149 5
            $this->getTags($event->getRun())
150
        );
151 5
    }
152
153
    /**
154
     * @param PoolInterface|RunInterface $item
155
     *
156
     * @return array
157
     */
158 6
    private function getTags($item)
159
    {
160 6
        if ($item instanceof PoolInterface) {
161 2
            return $this->getPoolTags($item);
162
        }
163 6
        return $this->getRunTags($item);
164
    }
165
166
    /**
167
     * @param PoolInterface $pool
168
     *
169
     * @return array
170
     */
171 2
    private function getPoolTags(PoolInterface $pool)
172
    {
173 2
        $tags = [];
174 2
        if ($pool instanceof RunInterface) {
175 2
            $tags = $this->getRunTags($pool);
176
        }
177
        return [
178 2
            'pool' => array_merge(
179
                [
180 2
                    'type'         => get_class($pool),
181 2
                    'id'           => spl_object_hash($pool),
182 2
                    'num_waiting'  => count($pool->getWaiting()),
183 2
                    'num_running'  => count($pool->getRunning()),
184 2
                    'num_finished' => count($pool->getFinished()),
185
                ],
186 2
                (isset($tags['run']) ? $tags['run'] : [])
187
            ),
188
        ];
189
    }
190
191
    /**
192
     * @param RunInterface $run
193
     *
194
     * @return array
195
     */
196 6
    private function getRunTags(RunInterface $run)
197
    {
198
        return [
199 6
            'run' => [
200 6
                'type'         => get_class($run),
201 6
                'id'           => spl_object_hash($run),
202 6
                'tags'         => $run->getTags(),
203 6
                'hasStarted'   => $run->hasStarted(),
204 6
                'isRunning'    => $run->isRunning(),
205 6
                'isSuccessful' => $run->isSuccessful(),
206 6
                'duration'     => $run->getDuration(),
207 6
                'priority'     => $run->getPriority(),
208 6
                'progress'     => $run->getProgress() ? $run->getProgress()[0] : null,
209
            ],
210
        ];
211
    }
212
}
213