1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bernard\Driver\FlatFile; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Flat file driver to provide a simple job queue without any |
7
|
|
|
* database. |
8
|
|
|
* |
9
|
|
|
* @author Markus Bachmann <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Driver implements \Bernard\Driver |
12
|
|
|
{ |
13
|
|
|
private $baseDirectory; |
14
|
|
|
|
15
|
|
|
private $permissions; |
16
|
|
|
|
17
|
|
|
private $queueType; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $baseDirectory The base directory |
21
|
|
|
* @param int $permissions permissions to create the file with |
22
|
|
|
*/ |
23
|
18 |
|
public function __construct($baseDirectory, $permissions = 0740, $options = null) |
24
|
|
|
{ |
25
|
18 |
|
$this->baseDirectory = $baseDirectory; |
26
|
18 |
|
$this->permissions = $permissions; |
27
|
18 |
|
$this->queueType = isset($options['queueType']) && in_array($options['queueType'], ['lifo', 'fifo']) ? $options['queueType'] : 'lifo'; |
28
|
18 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
2 |
|
public function listQueues() |
34
|
|
|
{ |
35
|
2 |
|
$it = new \FilesystemIterator($this->baseDirectory, \FilesystemIterator::SKIP_DOTS); |
36
|
|
|
|
37
|
2 |
|
$queues = []; |
38
|
|
|
|
39
|
2 |
|
foreach ($it as $file) { |
40
|
2 |
|
if (!$file->isDir()) { |
41
|
2 |
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
array_push($queues, $file->getBasename()); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
2 |
|
return $queues; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
18 |
|
public function createQueue($queueName) |
54
|
|
|
{ |
55
|
18 |
|
$queueDir = $this->getQueueDirectory($queueName); |
56
|
|
|
|
57
|
18 |
|
if (is_dir($queueDir)) { |
58
|
2 |
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
18 |
|
mkdir($queueDir, 0755, true); |
62
|
18 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function countMessages($queueName) |
68
|
|
|
{ |
69
|
|
|
$iterator = new \RecursiveDirectoryIterator( |
70
|
|
|
$this->getQueueDirectory($queueName), |
71
|
|
|
\FilesystemIterator::SKIP_DOTS |
72
|
|
|
); |
73
|
|
|
$iterator = new \RecursiveIteratorIterator($iterator); |
74
|
|
|
$iterator = new \RegexIterator($iterator, '#\.job$#'); |
75
|
|
|
|
76
|
|
|
return iterator_count($iterator); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
16 |
|
public function pushMessage($queueName, $message) |
83
|
|
|
{ |
84
|
16 |
|
$queueDir = $this->getQueueDirectory($queueName); |
85
|
|
|
|
86
|
16 |
|
$filename = $this->getJobFilename($queueName); |
87
|
|
|
|
88
|
16 |
|
file_put_contents($queueDir.DIRECTORY_SEPARATOR.$filename, $message); |
89
|
16 |
|
chmod($queueDir.DIRECTORY_SEPARATOR.$filename, $this->permissions); |
90
|
16 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
6 |
|
public function popMessage($queueName, $duration = 5) |
96
|
|
|
{ |
97
|
6 |
|
$runtime = microtime(true) + $duration; |
98
|
6 |
|
$queueDir = $this->getQueueDirectory($queueName); |
99
|
|
|
|
100
|
6 |
|
$it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME); |
101
|
6 |
|
$files = array_keys(iterator_to_array($it)); |
102
|
|
|
|
103
|
6 |
|
natsort($files); |
104
|
|
|
|
105
|
6 |
|
while (microtime(true) < $runtime) { |
106
|
6 |
|
if ($files) { |
107
|
6 |
|
$id = $this->queueType === 'fifo' ? array_shift($files) : array_pop($files); |
108
|
6 |
|
if (@rename($queueDir.DIRECTORY_SEPARATOR.$id, $queueDir.DIRECTORY_SEPARATOR.$id.'.proceed')) { |
109
|
6 |
|
return [file_get_contents($queueDir.DIRECTORY_SEPARATOR.$id.'.proceed'), $id]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->processFileOrFail($queueDir, $id); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
usleep(1000); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return [null, null]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $queueDir |
123
|
|
|
* @param string $id |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
private function processFileOrFail($queueDir, $id) { |
128
|
|
|
$name = $queueDir.DIRECTORY_SEPARATOR.$id; |
129
|
|
|
$newName = $name.'.proceed'; |
130
|
|
|
|
131
|
|
|
if (!@rename($name, $newName)) { |
132
|
|
|
throw new InsufficientPermissionsException('Unable to process file: '.$name); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return [file_get_contents($newName), $id]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
2 |
|
public function acknowledgeMessage($queueName, $receipt) |
142
|
|
|
{ |
143
|
2 |
|
$queueDir = $this->getQueueDirectory($queueName); |
144
|
2 |
|
$path = $queueDir.DIRECTORY_SEPARATOR.$receipt.'.proceed'; |
145
|
|
|
|
146
|
2 |
|
if (!is_file($path)) { |
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
unlink($path); |
151
|
2 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
2 |
|
public function peekQueue($queueName, $index = 0, $limit = 20) |
157
|
|
|
{ |
158
|
2 |
|
$queueDir = $this->getQueueDirectory($queueName); |
159
|
|
|
|
160
|
2 |
|
$it = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME); |
161
|
2 |
|
$files = array_keys(iterator_to_array($it)); |
162
|
|
|
|
163
|
2 |
|
natsort($files); |
164
|
2 |
|
if ($this->queueType === 'life') { |
165
|
|
|
$files = array_reverse($files); |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
$files = array_slice($files, $index, $limit); |
169
|
|
|
|
170
|
2 |
|
$messages = []; |
171
|
|
|
|
172
|
2 |
|
foreach ($files as $file) { |
173
|
2 |
|
array_push($messages, file_get_contents($queueDir.DIRECTORY_SEPARATOR.$file)); |
174
|
2 |
|
} |
175
|
|
|
|
176
|
2 |
|
return $messages; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
4 |
|
public function removeQueue($queueName) |
183
|
|
|
{ |
184
|
4 |
|
$iterator = new \RecursiveDirectoryIterator( |
185
|
4 |
|
$this->getQueueDirectory($queueName), |
186
|
|
|
\FilesystemIterator::SKIP_DOTS |
187
|
4 |
|
); |
188
|
4 |
|
$iterator = new \RecursiveIteratorIterator($iterator); |
189
|
4 |
|
$iterator = new \RegexIterator($iterator, '#\.job(.proceed)?$#'); |
190
|
|
|
|
191
|
4 |
|
foreach ($iterator as $file) { |
192
|
|
|
/* @var $file \DirectoryIterator */ |
193
|
4 |
|
unlink($file->getRealPath()); |
194
|
4 |
|
} |
195
|
|
|
|
196
|
4 |
|
rmdir($this->getQueueDirectory($queueName)); |
197
|
4 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
|
|
public function info() |
203
|
|
|
{ |
204
|
|
|
return []; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $queueName |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
18 |
|
private function getQueueDirectory($queueName) |
213
|
|
|
{ |
214
|
18 |
|
return $this->baseDirectory.DIRECTORY_SEPARATOR.str_replace(['\\', '.'], '-', $queueName); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Generates a uuid. |
219
|
|
|
* |
220
|
|
|
* @param string $queueName |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
16 |
|
private function getJobFilename($queueName) |
225
|
|
|
{ |
226
|
16 |
|
$path = $this->baseDirectory.'/bernard.meta'; |
227
|
|
|
|
228
|
16 |
|
if (!is_file($path)) { |
229
|
16 |
|
touch($path); |
230
|
16 |
|
chmod($path, $this->permissions); |
231
|
16 |
|
} |
232
|
|
|
|
233
|
16 |
|
$file = new \SplFileObject($path, 'r+'); |
234
|
16 |
|
$file->flock(LOCK_EX); |
235
|
|
|
|
236
|
16 |
|
$meta = unserialize($file->fgets()); |
237
|
|
|
|
238
|
16 |
|
$id = isset($meta[$queueName]) ? $meta[$queueName] : 0; |
239
|
16 |
|
++$id; |
240
|
|
|
|
241
|
16 |
|
$filename = sprintf('%d.job', $id); |
242
|
16 |
|
$meta[$queueName] = $id; |
243
|
|
|
|
244
|
16 |
|
$content = serialize($meta); |
245
|
|
|
|
246
|
16 |
|
$file->fseek(0); |
247
|
16 |
|
$file->fwrite($content, strlen($content)); |
248
|
16 |
|
$file->flock(LOCK_UN); |
249
|
|
|
|
250
|
16 |
|
return $filename; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|