|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bernard\Driver; |
|
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 FlatFileDriver implements \Bernard\Driver |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $baseDirectory; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var integer |
|
20
|
|
|
*/ |
|
21
|
|
|
private $permissions; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $baseDirectory The base directory |
|
25
|
|
|
* @param int $permissions Permissions to create the file with. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($baseDirectory, $permissions = 0740) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->baseDirectory = $baseDirectory; |
|
30
|
|
|
$this->permissions = $permissions; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function listQueues() |
|
37
|
|
|
{ |
|
38
|
|
|
$it = new \FilesystemIterator($this->baseDirectory, \FilesystemIterator::SKIP_DOTS); |
|
39
|
|
|
|
|
40
|
|
|
$queues = []; |
|
41
|
|
|
|
|
42
|
|
|
foreach ($it as $file) { |
|
43
|
|
|
if (!$file->isDir()) { |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
array_push($queues, $file->getBasename()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $queues; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function createQueue($queueName) |
|
57
|
|
|
{ |
|
58
|
|
|
$queueDir = $this->getQueueDirectory($queueName); |
|
59
|
|
|
|
|
60
|
|
|
if (is_dir($queueDir)) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
mkdir($queueDir, 0755, true); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function countMessages($queueName) |
|
71
|
|
|
{ |
|
72
|
|
|
return iterator_count($this->getJobIterator($queueName)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function pushMessage($queueName, $message) |
|
79
|
|
|
{ |
|
80
|
|
|
$queueDir = $this->getQueueDirectory($queueName); |
|
81
|
|
|
|
|
82
|
|
|
$filename = $this->getJobFilename($queueName); |
|
83
|
|
|
|
|
84
|
|
|
file_put_contents($queueDir.DIRECTORY_SEPARATOR.$filename, $message); |
|
85
|
|
|
chmod($queueDir.DIRECTORY_SEPARATOR.$filename, $this->permissions); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function popMessage($queueName, $duration = 5) |
|
92
|
|
|
{ |
|
93
|
|
|
$runtime = microtime(true) + $duration; |
|
94
|
|
|
$queueDir = $this->getQueueDirectory($queueName); |
|
95
|
|
|
$files = $this->getJobFiles($queueName); |
|
96
|
|
|
|
|
97
|
|
|
natsort($files); |
|
98
|
|
|
|
|
99
|
|
|
while (microtime(true) < $runtime) { |
|
100
|
|
|
if ($files) { |
|
|
|
|
|
|
101
|
|
|
$id = array_shift($files); |
|
102
|
|
|
$data = array(file_get_contents($queueDir.DIRECTORY_SEPARATOR.$id), $id); |
|
103
|
|
|
// Set file hidden (emulating message invisibility) |
|
104
|
|
|
rename($queueDir.DIRECTORY_SEPARATOR.$id, $queueDir.DIRECTORY_SEPARATOR.'.'.$id); |
|
105
|
|
|
return $data; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
usleep(1000); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return array(null, null); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function acknowledgeMessage($queueName, $receipt) |
|
118
|
|
|
{ |
|
119
|
|
|
$queueDir = $this->getQueueDirectory($queueName); |
|
120
|
|
|
// Set path to hidden filename |
|
121
|
|
|
$path = $queueDir.DIRECTORY_SEPARATOR.'.'.$receipt; |
|
122
|
|
|
|
|
123
|
|
|
if (!is_file($path)) { |
|
124
|
|
|
return; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
unlink($path); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function peekQueue($queueName, $index = 0, $limit = 20) |
|
134
|
|
|
{ |
|
135
|
|
|
$queueDir = $this->getQueueDirectory($queueName); |
|
136
|
|
|
|
|
137
|
|
|
$files = $this->getJobFiles($queueName); |
|
138
|
|
|
|
|
139
|
|
|
natsort($files); |
|
140
|
|
|
|
|
141
|
|
|
$files = array_slice($files, $index, $limit); |
|
142
|
|
|
|
|
143
|
|
|
$messages = []; |
|
144
|
|
|
|
|
145
|
|
|
foreach ($files as $file) { |
|
146
|
|
|
array_push($messages, file_get_contents($queueDir.DIRECTORY_SEPARATOR.$file)); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $messages; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* {@inheritdoc} |
|
154
|
|
|
*/ |
|
155
|
|
|
public function removeQueue($queueName) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->removeDirectoryRecursive($this->getQueueDirectory($queueName)); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* {@inheritdoc} |
|
162
|
|
|
*/ |
|
163
|
|
|
public function info() |
|
164
|
|
|
{ |
|
165
|
|
|
return []; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $queueName |
|
170
|
|
|
* |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
|
|
private function getQueueDirectory($queueName) |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->baseDirectory.DIRECTORY_SEPARATOR.str_replace(array('\\', '.'), '-', $queueName); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Generates a uuid. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $queueName |
|
182
|
|
|
* |
|
183
|
|
|
* @return string |
|
184
|
|
|
*/ |
|
185
|
|
|
private function getJobFilename($queueName) |
|
186
|
|
|
{ |
|
187
|
|
|
$path = $this->baseDirectory.'/bernard.meta'; |
|
188
|
|
|
|
|
189
|
|
|
if (!is_file($path)) { |
|
190
|
|
|
touch($path); |
|
191
|
|
|
chmod($path, $this->permissions); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$file = new \SplFileObject($path, 'r+'); |
|
195
|
|
|
$file->flock(LOCK_EX); |
|
196
|
|
|
|
|
197
|
|
|
$meta = unserialize($file->fgets()); |
|
198
|
|
|
|
|
199
|
|
|
$id = isset($meta[$queueName]) ? $meta[$queueName] : 0; |
|
200
|
|
|
$id++; |
|
201
|
|
|
|
|
202
|
|
|
$filename = sprintf('%d.job', $id); |
|
203
|
|
|
$meta[$queueName] = $id; |
|
204
|
|
|
|
|
205
|
|
|
$content = serialize($meta); |
|
206
|
|
|
|
|
207
|
|
|
$file->fseek(0); |
|
208
|
|
|
$file->fwrite($content, strlen($content)); |
|
209
|
|
|
$file->flock(LOCK_UN); |
|
210
|
|
|
|
|
211
|
|
|
return $filename; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Creates an iterator of all message files in the queue |
|
216
|
|
|
* @param string $queueName |
|
217
|
|
|
* @return \GlobIterator |
|
218
|
|
|
*/ |
|
219
|
|
|
private function getJobIterator($queueName) { |
|
220
|
|
|
$queueDir = $this->getQueueDirectory($queueName); |
|
221
|
|
|
$iterator = new \GlobIterator($queueDir.DIRECTORY_SEPARATOR.'*.job', \FilesystemIterator::KEY_AS_FILENAME); |
|
222
|
|
|
return $iterator; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Retrieves an array of all message files in the queue |
|
227
|
|
|
* @param string $queueName |
|
228
|
|
|
* @return array |
|
229
|
|
|
*/ |
|
230
|
|
|
private function getJobFiles($queueName) { |
|
231
|
|
|
$iterator = $this->getJobIterator($queueName); |
|
232
|
|
|
$files = array_keys(iterator_to_array($iterator)); |
|
233
|
|
|
return $files; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Removes a directory recursively |
|
238
|
|
|
* @param string $directory |
|
239
|
|
|
*/ |
|
240
|
|
|
private function removeDirectoryRecursive($directory) |
|
241
|
|
|
{ |
|
242
|
|
|
foreach (glob("{$directory}/{,.}[!.,!..]*", GLOB_MARK|GLOB_BRACE) as $file) |
|
243
|
|
|
{ |
|
244
|
|
|
if (is_dir($file)) { |
|
245
|
|
|
$this->removeDirectoryRecursive($file); |
|
246
|
|
|
} else { |
|
247
|
|
|
unlink($file); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
rmdir($directory); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.