1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dmank\gearman; |
4
|
|
|
|
5
|
|
|
class Client |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var \GearmanClient |
9
|
|
|
*/ |
10
|
|
|
private $realClient; |
11
|
|
|
/** |
12
|
|
|
* @var ServerCollection |
13
|
|
|
*/ |
14
|
|
|
private $serverCollection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
private $serverAddedToClient = false; |
20
|
|
|
|
21
|
|
|
const PRIORITY_LOW = 0; |
22
|
|
|
const PRIORITY_NORMAL = 1; |
23
|
|
|
const PRIORITY_HIGH = 2; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param ServerCollection $servers |
27
|
|
|
*/ |
28
|
10 |
|
public function __construct(ServerCollection $servers) |
29
|
|
|
{ |
30
|
10 |
|
$this->serverCollection = $servers; |
31
|
10 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \GearmanClient $client |
35
|
|
|
*/ |
36
|
9 |
|
public function setImplementation(\GearmanClient $client) |
37
|
|
|
{ |
38
|
9 |
|
$this->realClient = $client; |
39
|
9 |
|
} |
40
|
|
|
|
41
|
1 |
|
public function getJobStatus($jobHandle) |
42
|
|
|
{ |
43
|
1 |
|
$status = $this->getClient()->jobStatus($jobHandle); |
44
|
|
|
|
45
|
1 |
|
return new JobStatus($status); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $method |
50
|
|
|
* @param $workLoad |
51
|
|
|
* @param int $priority |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
3 |
|
public function executeInBackground($method, $workLoad, $priority = self::PRIORITY_LOW) |
55
|
|
|
{ |
56
|
3 |
|
if (!StringHelper::isSerialized($workLoad)) { |
57
|
3 |
|
$workLoad = serialize($workLoad); |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
switch ($priority) { |
61
|
3 |
|
case self::PRIORITY_HIGH: |
62
|
1 |
|
$result = $this->getClient()->doHighBackground($method, $workLoad); |
63
|
1 |
|
break; |
64
|
2 |
|
case self::PRIORITY_NORMAL: |
65
|
1 |
|
$result = $this->getClient()->doBackground($method, $workLoad); |
66
|
1 |
|
break; |
67
|
1 |
|
case self::PRIORITY_LOW: |
68
|
1 |
|
default: |
69
|
1 |
|
$result = $this->getClient()->doLowBackground($method, $workLoad); |
70
|
1 |
|
break; |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $method |
79
|
|
|
* @param $workLoad |
80
|
|
|
* @param int $priority |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
4 |
|
public function executeJob($method, $workLoad, $priority = self::PRIORITY_LOW) |
84
|
|
|
{ |
85
|
4 |
|
if (!StringHelper::isSerialized($workLoad)) { |
86
|
4 |
|
$workLoad = serialize($workLoad); |
87
|
4 |
|
} |
88
|
|
|
|
89
|
4 |
|
$client = $this->getClient(); |
90
|
|
|
|
91
|
|
|
switch ($priority) { |
92
|
4 |
|
case self::PRIORITY_HIGH: |
93
|
1 |
|
$result = $client->doHigh($method, $workLoad); |
94
|
1 |
|
break; |
95
|
3 |
|
case self::PRIORITY_NORMAL: |
96
|
1 |
|
$gearmanMethod = $this->getPriorityNormalMethod(); |
97
|
1 |
|
$result = $client->$gearmanMethod($method, $workLoad); |
98
|
1 |
|
break; |
99
|
2 |
|
case self::PRIORITY_LOW: |
100
|
2 |
|
default: |
101
|
2 |
|
$result = $client->doLow($method, $workLoad); |
102
|
2 |
|
break; |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
return $result; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $jobs Array of dmank\gearman\Job, jobs to do |
110
|
|
|
* @param int $priority |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
1 |
|
public function executeJobs(array $jobs, $priority = self::PRIORITY_LOW) |
|
|
|
|
114
|
|
|
{ |
115
|
1 |
|
$client = $this->getClient(); |
116
|
|
|
|
117
|
1 |
|
$results = []; |
118
|
1 |
|
$client->setCompleteCallback(function ($task) use (&$results) { |
119
|
|
|
$results[] = $task->data(); |
120
|
1 |
|
}); |
121
|
|
|
|
122
|
1 |
|
foreach ($jobs as $job) { |
123
|
1 |
|
if (!is_a($job, Job::class)) { |
124
|
1 |
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
$client->addTask($job->getJobName(), $job->getWorkLoad()); |
128
|
1 |
|
} |
129
|
|
|
|
130
|
1 |
|
$client->runTasks(); |
131
|
|
|
|
132
|
1 |
|
return $results; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return \GearmanClient |
137
|
|
|
*/ |
138
|
9 |
|
private function getClient() |
139
|
|
|
{ |
140
|
9 |
|
if (!$this->realClient) { |
141
|
|
|
$this->realClient = $this->createClient(); |
142
|
|
|
} |
143
|
|
|
|
144
|
9 |
|
if (!$this->serverAddedToClient) { |
145
|
|
|
/* @var Server $server */ |
146
|
9 |
|
foreach ($this->serverCollection->getServers() as $server) { |
147
|
2 |
|
$this->realClient->addServer($server->getHost(), $server->getPort()); |
148
|
9 |
|
} |
149
|
|
|
|
150
|
9 |
|
$this->serverAddedToClient = true; |
151
|
9 |
|
} |
152
|
|
|
|
153
|
9 |
|
return $this->realClient; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return \GearmanClient |
158
|
|
|
*/ |
159
|
|
|
private function createClient() |
160
|
|
|
{ |
161
|
|
|
return new \GearmanClient(); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
private function getPriorityNormalMethod() |
165
|
|
|
{ |
166
|
1 |
|
$normalPriorityMethod = 'doLow'; |
167
|
|
|
|
168
|
1 |
|
if (version_compare(phpversion('gearman'), '1.0.0') >= 0) { |
169
|
1 |
|
$normalPriorityMethod = 'doNormal'; |
170
|
1 |
|
} |
171
|
|
|
|
172
|
1 |
|
return $normalPriorityMethod; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.