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