1 | <?php |
||
11 | class FlatFileDriver 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 | public function __construct($baseDirectory, $permissions = 0740) |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function listQueues() |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function createQueue($queueName) |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function countMessages($queueName) |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function pushMessage($queueName, $message) |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function popMessage($queueName, $duration = 5) |
||
93 | { |
||
94 | $runtime = microtime(true) + $duration; |
||
95 | $queueDir = $this->getQueueDirectory($queueName); |
||
96 | |||
97 | $files = $this->getJobFiles($queueName); |
||
98 | |||
99 | while (microtime(true) < $runtime) { |
||
100 | if ($files) { |
||
101 | $id = array_pop($files); |
||
102 | if (@rename($queueDir.DIRECTORY_SEPARATOR.$id, $queueDir.DIRECTORY_SEPARATOR.$id.'.proceed')) { |
||
103 | return array(file_get_contents($queueDir.DIRECTORY_SEPARATOR.$id.'.proceed'), $id); |
||
104 | } |
||
105 | } else { |
||
106 | // In order to notice that a new message received, update the list. |
||
107 | $files = $this->getJobFiles($queueName); |
||
108 | } |
||
109 | |||
110 | usleep(1000); |
||
111 | } |
||
112 | |||
113 | return array(null, null); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function acknowledgeMessage($queueName, $receipt) |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function peekQueue($queueName, $index = 0, $limit = 20) |
||
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | public function removeQueue($queueName) |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function info() |
||
182 | |||
183 | /** |
||
184 | * @param string $queueName |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | private function getQueueDirectory($queueName) |
||
192 | |||
193 | /** |
||
194 | * Generates a uuid. |
||
195 | * |
||
196 | * @param string $queueName |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | private function getJobFilename($queueName) |
||
228 | |||
229 | /** |
||
230 | * @param string $queueName |
||
231 | * |
||
232 | * @return string[] |
||
233 | */ |
||
234 | private function getJobFiles($queueName) |
||
245 | } |
||
246 |