|
1
|
|
|
<?php |
|
2
|
|
|
namespace dmank\gearman\event\subscriber; |
|
3
|
|
|
|
|
4
|
|
|
use dmank\gearman\event\FunctionEvent; |
|
5
|
|
|
use dmank\gearman\event\FunctionFailureEvent; |
|
6
|
|
|
use dmank\gearman\event\RegisterFunctionEvent; |
|
7
|
|
|
use dmank\gearman\event\WorkerEvent; |
|
8
|
|
|
use dmank\gearman\event\WorkerExceptionEvent; |
|
9
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
12
|
|
|
|
|
13
|
|
|
class Monolog implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var LoggerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $logger; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(LoggerInterface $logger) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->logger = $logger; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function getSubscribedEvents() |
|
26
|
|
|
{ |
|
27
|
|
|
return array( |
|
28
|
|
|
WorkerEvent::EVENT_BEFORE_RUN => array('onBeforeRun', 10), |
|
29
|
|
|
WorkerEvent::EVENT_ON_BEFORE_DESTROY => array('onBeforeDestroy', 10), |
|
30
|
|
|
WorkerEvent::EVENT_AFTER_RUN => array('onAfterRun', 10), |
|
31
|
|
|
WorkerEvent::EVENT_ON_IO_WAIT => array('onIOWait', 10), |
|
32
|
|
|
RegisterFunctionEvent::EVENT_ON_AFTER_REGISTER_FUNCTIONS => array('onAfterRegisterFunctions', 10), |
|
33
|
|
|
RegisterFunctionEvent::EVENT_ON_BEFORE_REGISTER_FUNCTIONS => array('onBeforeRegisterFunctions', 10), |
|
34
|
|
|
WorkerExceptionEvent::EVENT_ON_FAILURE => array('onFailure', 10), |
|
35
|
|
|
WorkerEvent::EVENT_ON_NO_JOBS => array('onNoJobs', 10), |
|
36
|
|
|
FunctionEvent::FUNCTION_BEFORE_EXECUTE => array('onFunctionBeforeExecute', 10), |
|
37
|
|
|
FunctionEvent::FUNCTION_AFTER_EXECUTE => array('onFunctionAfterExecute', 10), |
|
38
|
|
|
FunctionFailureEvent::FUNCTION_ON_FAILURE => array('onFunctionFailure', 10) |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function onBeforeRun(WorkerEvent $event) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$this->logger->info('Starting Worker'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function onAfterRun(WorkerEvent $event) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$this->logger->info('Finished work.'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function onBeforeDestroy(WorkerEvent $event) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$this->logger->notice('Destroying worker.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function onIOWait(WorkerEvent $event) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$this->logger->debug('I`m idling.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function onFailure(WorkerExceptionEvent $event) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->logger->critical( |
|
65
|
|
|
sprintf('Something unexpected happened. Message was "%s"', $event->getException()->getMessage()) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function onNoJobs(WorkerEvent $event) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$this->logger->debug( |
|
72
|
|
|
sprintf('Got no jobs to do.') |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function onFunctionBeforeExecute(FunctionEvent $functionEvent) |
|
77
|
|
|
{ |
|
78
|
|
|
$jobHandler = $functionEvent->getJobHandler(); |
|
79
|
|
|
$job = $functionEvent->getJob(); |
|
80
|
|
|
|
|
81
|
|
|
$this->logger->info( |
|
82
|
|
|
sprintf('Im starting working on job %s', $job->getJobName()) |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
if ($jobHandler instanceof LoggerAwareInterface) { |
|
86
|
|
|
$this->logger->debug('Setting Logger to Job'); |
|
87
|
|
|
$jobHandler->setLogger($this->logger); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function onFunctionAfterExecute(FunctionEvent $functionEvent) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->logger->info( |
|
94
|
|
|
sprintf('I successfully finished my work on job %s', $functionEvent->getJob()->getJobName()) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function onFunctionFailure(FunctionFailureEvent $event) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->logger->critical( |
|
101
|
|
|
sprintf( |
|
102
|
|
|
'Something unexpected happened in job %s' . |
|
103
|
|
|
'... Message %s , Trace %s.', |
|
104
|
|
|
$event->getJob()->getJobName(), |
|
105
|
|
|
$event->getException()->getMessage(), |
|
106
|
|
|
$event->getException()->getTraceAsString() |
|
107
|
|
|
) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function onBeforeRegisterFunctions(RegisterFunctionEvent $registerFunctionEvent) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->logger->debug( |
|
114
|
|
|
sprintf( |
|
115
|
|
|
'I got %s jobs', |
|
116
|
|
|
$registerFunctionEvent->getJobs()->count() |
|
117
|
|
|
) |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function onAfterRegisterFunctions(RegisterFunctionEvent $registerFunctionEvent) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->logger->debug( |
|
124
|
|
|
sprintf( |
|
125
|
|
|
'Added %s jobs to the Worker', |
|
126
|
|
|
$registerFunctionEvent->getJobs()->count() |
|
127
|
|
|
) |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.