1 | <?php |
||
14 | class WorkerManager |
||
15 | { |
||
16 | protected $workers; |
||
17 | protected $jobManager; |
||
18 | |||
19 | /** @var LoggerInterface */ |
||
20 | protected $logger; |
||
21 | protected $eventDispatcher; |
||
22 | protected $logFunc; |
||
23 | |||
24 | 18 | public function __construct(JobManagerInterface $jobManager, EventDispatcher $eventDispatcher) |
|
30 | |||
31 | public function setLogger(LoggerInterface $logger) |
||
35 | |||
36 | /** |
||
37 | * @param Worker $worker |
||
38 | * |
||
39 | * @throws DuplicateWorkerException |
||
40 | */ |
||
41 | 9 | public function addWorker(Worker $worker) |
|
42 | { |
||
43 | 9 | if ($this->logger) { |
|
44 | $this->logger->debug(__METHOD__." - Added worker: {$worker->getName()}"); |
||
45 | } |
||
46 | |||
47 | 9 | if (isset($this->workers[$worker->getName()])) { |
|
48 | 1 | throw new DuplicateWorkerException("{$worker->getName()} already exists in worker manager"); |
|
49 | } |
||
50 | |||
51 | 9 | $this->workers[$worker->getName()] = $worker; |
|
52 | 9 | } |
|
53 | |||
54 | 8 | public function getWorker($name) |
|
62 | |||
63 | 1 | public function getWorkers() |
|
67 | |||
68 | 3 | public function setLoggingFunc(callable $callable) |
|
72 | |||
73 | 6 | public function log($level, $msg, array $context = []) |
|
87 | |||
88 | /** |
||
89 | * @param null $workerName |
||
90 | * @param null $methodName |
||
91 | * @param bool $prioritize |
||
92 | * |
||
93 | * @return null|Job |
||
94 | */ |
||
95 | 5 | public function run($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
104 | |||
105 | /** |
||
106 | * @param array $payload |
||
107 | * @param Job $job |
||
108 | */ |
||
109 | 1 | protected function handleException(array $payload, Job $job) |
|
110 | { |
||
111 | 1 | $exception = $payload[0]; |
|
112 | 1 | $exceptionMessage = get_class($exception)."\n".$exception->getCode().' - '.$exception->getMessage()."\n".$exception->getTraceAsString(); |
|
113 | 1 | $this->log('debug', "Failed: {$job->getClassName()}->{$job->getMethod()}"); |
|
114 | 1 | $job->setStatus(BaseJob::STATUS_EXCEPTION); |
|
115 | 1 | $message = $job->getMessage(); |
|
116 | 1 | if ($message) { |
|
|
|||
117 | $message .= "\n\n"; |
||
118 | } else { |
||
119 | 1 | $message = $exceptionMessage; |
|
120 | } |
||
121 | |||
122 | 1 | $job->setMessage($message); |
|
123 | 1 | $this->jobManager->getJobTimingManager()->recordTiming(JobTiming::STATUS_FINISHED_EXCEPTION); |
|
124 | 1 | } |
|
125 | |||
126 | 5 | public function processStatus(Job $job, $result) |
|
137 | |||
138 | 6 | public function runJob(Job $job) |
|
139 | { |
||
172 | } |
||
173 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: