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