1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation\Queue; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Jitamin\Bus\Job\BaseJob; |
16
|
|
|
use Jitamin\Foundation\Base; |
17
|
|
|
use SimpleQueue\Job; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class JobHandler. |
21
|
|
|
*/ |
22
|
|
|
class JobHandler extends Base |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Serialize a job. |
26
|
|
|
* |
27
|
|
|
* @param BaseJob $job |
28
|
|
|
* |
29
|
|
|
* @return Job |
30
|
|
|
*/ |
31
|
|
|
public function serializeJob(BaseJob $job) |
32
|
|
|
{ |
33
|
|
|
return new Job([ |
34
|
|
|
'class' => get_class($job), |
35
|
|
|
'params' => $job->getJobParams(), |
36
|
|
|
'user_id' => $this->userSession->getId(), |
|
|
|
|
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Execute a job. |
42
|
|
|
* |
43
|
|
|
* @param Job $job |
44
|
|
|
*/ |
45
|
|
|
public function executeJob(Job $job) |
46
|
|
|
{ |
47
|
|
|
$payload = $job->getBody(); |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$className = $payload['class']; |
51
|
|
|
$this->prepareJobSession($payload['user_id']); |
52
|
|
|
$this->prepareJobEnvironment(); |
53
|
|
|
|
54
|
|
|
if (DEBUG) { |
55
|
|
|
$this->logger->debug(__METHOD__.' Received job => '.$className.' ('.getmypid().')'); |
|
|
|
|
56
|
|
|
$this->logger->debug(__METHOD__.' => '.json_encode($payload)); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$worker = new $className($this->container); |
60
|
|
|
call_user_func_array([$worker, 'execute'], $payload['params']); |
61
|
|
|
} catch (Exception $e) { |
62
|
|
|
$this->logger->error(__METHOD__.': Error during job execution: '.$e->getMessage()); |
|
|
|
|
63
|
|
|
$this->logger->error(__METHOD__.' => '.json_encode($payload)); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create the session for the job. |
69
|
|
|
* |
70
|
|
|
* @param int $user_id |
71
|
|
|
*/ |
72
|
|
|
protected function prepareJobSession($user_id) |
73
|
|
|
{ |
74
|
|
|
$session = []; |
75
|
|
|
$this->sessionStorage->setStorage($session); |
|
|
|
|
76
|
|
|
|
77
|
|
|
if ($user_id > 0) { |
78
|
|
|
$user = $this->userModel->getById($user_id); |
|
|
|
|
79
|
|
|
$this->userSession->initialize($user); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Flush in-memory caching and specific events. |
85
|
|
|
*/ |
86
|
|
|
protected function prepareJobEnvironment() |
87
|
|
|
{ |
88
|
|
|
$this->memoryCache->flush(); |
|
|
|
|
89
|
|
|
$this->actionManager->removeEvents(); |
|
|
|
|
90
|
|
|
$this->dispatcher->dispatch('app.bootstrap'); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.