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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
<?php namespace Illuminate\Queue;
use IronMQ; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Queue\Jobs\IronJob; use Illuminate\Encryption\Encrypter;
class IronQueue extends Queue implements QueueInterface {
/** * The IronMQ instance. * * @var IronMQ */ protected $iron;
/** * The encrypter instance. * * @var \Illuminate\Encryption\Encrypter */ protected $crypt;
/** * The current request instance. * * @var \Illuminate\Http\Request */ protected $request;
/** * The name of the default tube. * * @var string */ protected $default;
/** * Create a new IronMQ queue instance. * * @param \IronMQ $iron * @param \Illuminate\Encryption\Encrypter $crypt * @param \Illuminate\Http\Request $request * @param string $default * @return void */ public function __construct(IronMQ $iron, Encrypter $crypt, Request $request, $default) { $this->iron = $iron; $this->crypt = $crypt; $this->request = $request; $this->default = $default; }
/** * Push a new job onto the queue. * * @param string $job * @param mixed $data * @param string $queue * @return mixed */ public function push($job, $data = '', $queue = null) { return $this->pushRaw($this->createPayload($job, $data, $queue), $queue); }
/** * Push a raw payload onto the queue. * * @param string $payload * @param string $queue * @param array $options * @return mixed */ public function pushRaw($payload, $queue = null, array $options = array()) { $payload = $this->crypt->encrypt($payload);
return $this->iron->postMessage($this->getQueue($queue), $payload, $options)->id; }
/** * Push a raw payload onto the queue after encrypting the payload. * * @param string $payload * @param string $queue * @param int $delay * @return mixed */ public function recreate($payload, $queue = null, $delay) { $options = array('delay' => $this->getSeconds($delay));
return $this->pushRaw($payload, $queue, $options); }
/** * Push a new job onto the queue after a delay. * * @param \DateTime|int $delay * @param string $job * @param mixed $data * @param string $queue * @return mixed */ public function later($delay, $job, $data = '', $queue = null) { $delay = $this->getSeconds($delay);
$payload = $this->createPayload($job, $data, $queue);
return $this->pushRaw($payload, $this->getQueue($queue), compact('delay')); }
/** * Pop the next job off of the queue. * * @param string $queue * @return \Illuminate\Queue\Jobs\Job|null */ public function pop($queue = null) { $queue = $this->getQueue($queue);
$job = $this->iron->getMessage($queue);
// If we were able to pop a message off of the queue, we will need to decrypt // the message body, as all Iron.io messages are encrypted, since the push // queues will be a security hazard to unsuspecting developers using it. if ( ! is_null($job)) { $job->body = $this->crypt->decrypt($job->body);
return new IronJob($this->container, $this, $job); } }
/** * Delete a message from the Iron queue. * * @param string $queue * @param string $id * @return void */ public function deleteMessage($queue, $id) { $this->iron->deleteMessage($queue, $id); }
/** * Marshal a push queue request and fire the job. * * @return \Illuminate\Http\Response */ public function marshal() { $this->createPushedIronJob($this->marshalPushedJob())->fire();
return new Response('OK'); }
/** * Marshal out the pushed job and payload. * * @return object */ protected function marshalPushedJob() { $r = $this->request;
$body = $this->crypt->decrypt($r->getContent());
return (object) array( 'id' => $r->header('iron-message-id'), 'body' => $body, 'pushed' => true, ); }
/** * Create a new IronJob for a pushed job. * * @param object $job * @return \Illuminate\Queue\Jobs\IronJob */ protected function createPushedIronJob($job) { return new IronJob($this->container, $this, $job, true); }
/** * Create a payload string from the given job and data. * * @param string $job * @param mixed $data * @param string $queue * @return string */ protected function createPayload($job, $data = '', $queue = null) { $payload = $this->setMeta(parent::createPayload($job, $data), 'attempts', 1);
return $this->setMeta($payload, 'queue', $this->getQueue($queue)); }
/** * Get the queue or return the default. * * @param string|null $queue * @return string */ public function getQueue($queue) { return $queue ?: $this->default; }
/** * Get the underlying IronMQ instance. * * @return IronMQ */ public function getIron() { return $this->iron; }
/** * Get the request instance. * * @return \Symfony\Component\HttpFoundation\Request */ public function getRequest() { return $this->request; }
/** * Set the request instance. * * @param \Symfony\Component\HttpFoundation\Request $request * @return void */ public function setRequest(Request $request) { $this->request = $request; }
}
|