1
|
|
|
<?php |
2
|
|
|
namespace Phloppy; |
3
|
|
|
|
4
|
|
|
class Job { |
5
|
|
|
|
6
|
|
|
const STATE_ACTIVE = 'active'; |
7
|
|
|
const STATE_WAIT_REPL = 'wait-repl'; |
8
|
|
|
const STATE_QUEUED = 'queued'; |
9
|
|
|
const STATE_ACKNOWLEDGED = 'acknowledged'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Job Id (generated by disque). |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $id = ''; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Queue name this job was fetched from. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $queue = ''; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Job body. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $body = ''; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Delay time (before putting the job into the queue, in seconds). |
37
|
|
|
* |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
private $delay = 0; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Retry time (seconds). |
45
|
|
|
* |
46
|
|
|
* How much time should elapse, since the last time the job was queued, and without an acknowledge about the job |
47
|
|
|
* delivery, before the job is re-queued again for delivery. |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
private $retry = 120; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The expire time (in seconds). |
56
|
|
|
* |
57
|
|
|
* How much time should elapse for the job to be deleted regardless of the fact it was successfully delivered, |
58
|
|
|
* i.e. acknowledged, or not. |
59
|
|
|
* |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
private $ttl = 3600; |
63
|
|
|
|
64
|
|
|
|
65
|
20 |
|
public function __construct($body) |
66
|
|
|
{ |
67
|
20 |
|
$this->body = $body; |
68
|
20 |
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the job id. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
16 |
|
public function getId() |
77
|
|
|
{ |
78
|
16 |
|
return $this->id; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $id |
83
|
|
|
*/ |
84
|
19 |
|
public function setId($id) |
85
|
|
|
{ |
86
|
19 |
|
$this->id = $id; |
87
|
19 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
18 |
|
public function getBody() |
93
|
|
|
{ |
94
|
18 |
|
return $this->body; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
17 |
|
public function getDelay() |
101
|
|
|
{ |
102
|
17 |
|
return $this->delay; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param int $delay |
107
|
|
|
*/ |
108
|
1 |
|
public function setDelay($delay) |
109
|
|
|
{ |
110
|
1 |
|
$this->delay = (int) $delay; |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
17 |
|
public function getRetry() |
117
|
|
|
{ |
118
|
17 |
|
return $this->retry; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param int $retry |
123
|
|
|
*/ |
124
|
4 |
|
public function setRetry($retry) |
125
|
|
|
{ |
126
|
4 |
|
$this->retry = $retry; |
127
|
4 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
17 |
|
public function getTtL() |
133
|
|
|
{ |
134
|
17 |
|
return $this->ttl; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param mixed $ttl |
139
|
|
|
*/ |
140
|
11 |
|
public function setTtl($ttl) |
141
|
|
|
{ |
142
|
11 |
|
$this->ttl = $ttl; |
143
|
11 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
2 |
|
public function getQueue() |
149
|
|
|
{ |
150
|
2 |
|
return $this->queue; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $queue |
155
|
|
|
*/ |
156
|
17 |
|
public function setQueue($queue) |
157
|
|
|
{ |
158
|
17 |
|
$this->queue = $queue; |
159
|
17 |
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Job Factory method. |
164
|
|
|
* |
165
|
|
|
* @param array $args |
166
|
|
|
* @return Job |
167
|
|
|
*/ |
168
|
19 |
|
public static function create(array $args) |
169
|
|
|
{ |
170
|
19 |
|
$job = new Job($args['body']); |
171
|
|
|
|
172
|
19 |
|
if (isset($args['id'])) { |
173
|
11 |
|
$job->setId($args['id']); |
174
|
11 |
|
$job->setTtl(hexdec(substr($job->getId(), -4))); |
175
|
11 |
|
} |
176
|
|
|
|
177
|
19 |
|
if (isset($args['queue'])) { |
178
|
9 |
|
$job->setQueue($args['queue']); |
179
|
9 |
|
} |
180
|
|
|
|
181
|
19 |
|
if (isset($args['ttl'])) { |
182
|
3 |
|
$job->setTtl($args['ttl']); |
183
|
3 |
|
} |
184
|
|
|
|
185
|
19 |
|
if (isset($args['retry'])) { |
186
|
2 |
|
$job->setRetry($args['retry']); |
187
|
2 |
|
} |
188
|
|
|
|
189
|
19 |
|
return $job; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Retrieve the originating node id. |
194
|
|
|
* |
195
|
|
|
* @return string|null The node id of the node where the job was published |
196
|
|
|
*/ |
197
|
1 |
|
public function getOriginNode() |
198
|
|
|
{ |
199
|
1 |
|
return $this->id ? substr($this->id, 2, 8) : null; |
200
|
|
|
} |
201
|
|
|
|
202
|
1 |
|
public function __toString() |
203
|
|
|
{ |
204
|
1 |
|
return $this->getId(); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|