forked from tereshvti/gearman-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
192 lines (175 loc) · 5.93 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace Supertag\Bundle\GearmanBundle;
use GearmanClient;
/**
* A proxy for gearman client, we might need a namespace
* for every job name
*/
class Client
{
private $gmc;
private $servers;
private $namespace;
public function __construct($servers, $namespace)
{
$this->servers = $servers;
$this->namespace = $namespace;
}
private function connect()
{
if (null === $this->gmc) {
$this->gmc = new GearmanClient;
$this->gmc->addServers($this->servers);
}
}
/**
* Get the php extension gearman client
*
* @return GearmanClient
*/
public function getGearmanClient()
{
$this->connect();
return $this->gmc;
}
/**
* Get job $name prefixed with namespace
*
* @param string $name
* @return string
*/
public function getJobName($name)
{
return $this->namespace . $name;
}
/**
* Schedules a background job
*
* @param string $jobName - the name of job to do. Will append st_version to prevent conflict
* @param Workload $workload - command parameters for the job command
* @return Resource - job handle
*/
public function doBackground($jobName, Workload $workload)
{
$this->connect();
return $this->gmc->doBackground($this->getJobName($jobName), (string)$workload);
}
/**
* Schedules a high priority background job
*
* @param string $jobName - the name of job to do. Will append st_version to prevent conflict
* @param Workload $workload - command parameters for the job command
* @return Resource - job handle
*/
public function doHighBackground($jobName, Workload $workload)
{
$this->connect();
return $this->gmc->doHighBackground($this->getJobName($jobName), (string)$workload);
}
/**
* Schedules a low priority background job
*
* @param string $jobName - the name of job to do. Will append st_version to prevent conflict
* @param Workload $workload - command parameters for the job command
* @return Resource - job handle
*/
public function doLowBackground($jobName, Workload $workload)
{
$this->connect();
return $this->gmc->doLowBackground($this->getJobName($jobName), (string)$workload);
}
/**
* Schedules a normal priority job
*
* @param string $jobName - the name of job to do. Will append st_version to prevent conflict
* @param Workload $workload - command parameters for the job command
* @return string - status result
*/
public function doNormal($jobName, Workload $workload)
{
$this->connect();
return $this->gmc->doNormal($this->getJobName($jobName), (string)$workload);
}
/**
* Schedules a high priority job
*
* @param string $jobName - the name of job to do. Will append st_version to prevent conflict
* @param Workload $workload - command parameters for the job command
* @return string - status result
*/
public function doHigh($jobName, Workload $workload)
{
$this->connect();
return $this->gmc->doHigh($this->getJobName($jobName), (string)$workload);
}
/**
* Schedules a low priority job
*
* @param string $jobName - the name of job to do. Will append st_version to prevent conflict
* @param Workload $workload - command parameters for the job command
* @return string - status result
*/
public function doLow($jobName, Workload $workload)
{
$this->connect();
return $this->gmc->doLow($this->getJobName($jobName), (string)$workload);
}
/**
* Add a normal priority task
*
* @param string $jobName - the name of job to do. Will append st_version to prevent conflict
* @param Workload $workload - command parameters for the job command
* @param mixed $context
* @param string $unique - unique task identifier
* @param boolean $background - whether to run in background or not
* @return GearmanTask
*/
public function addTask($jobName, Workload $workload, &$context, $unique, $background = true)
{
$this->connect();
$method = 'addTask' . $background ? 'Background' : '';
return $this->gmc->{$method}($this->getJobName($jobName), (string)$workload, $context, $unique);
}
/**
* Add a high priority task
*
* @param string $jobName - the name of job to do. Will append st_version to prevent conflict
* @param Workload $workload - command parameters for the job command
* @param mixed $context
* @param string $unique - unique task identifier
* @param boolean $background - whether to run in background or not
* @return GearmanTask
*/
public function addTaskHigh($jobName, Workload $workload, &$context, $unique, $background = true)
{
$this->connect();
$method = 'addTaskHigh' . $background ? 'Background' : '';
return $this->gmc->{$method}($this->getJobName($jobName), (string)$workload, $context, $unique);
}
/**
* Add a low priority task
*
* @param string $jobName - the name of job to do. Will append st_version to prevent conflict
* @param Workload $workload - command parameters for the job command
* @param mixed $context
* @param string $unique - unique task identifier
* @param boolean $background - whether to run in background or not
* @return GearmanTask
*/
public function addTaskLow($jobName, Workload $workload, &$context, $unique, $background = true)
{
$this->connect();
$method = 'addTaskLow' . $background ? 'Background' : '';
return $this->gmc->{$method}($this->getJobName($jobName), (string)$workload, $context, $unique);
}
/**
* Run added tasks
*
* @return boolean
*/
public function runTasks()
{
$this->connect();
return $this->gmc->runTasks();
}
}