summaryrefslogtreecommitdiff
path: root/inc/queue.php
diff options
context:
space:
mode:
authorczaks <marcin@6irc.net>2016-05-08 15:37:49 +0200
committerczaks <marcin@6irc.net>2016-05-08 15:37:49 +0200
commitbb9aaad899c47e4eec49cbd444573e78d48a146a (patch)
tree69378054c4602cd681db0784f9be9f9bc5098c6e /inc/queue.php
parentf24e0f98143528c7b2cd5c504899c5492f5d75fd (diff)
i forgot about a queue and a lock implementation
Diffstat (limited to 'inc/queue.php')
-rw-r--r--inc/queue.php49
1 files changed, 49 insertions, 0 deletions
diff --git a/inc/queue.php b/inc/queue.php
new file mode 100644
index 00000000..66305b3b
--- /dev/null
+++ b/inc/queue.php
@@ -0,0 +1,49 @@
+<?php
+
+class Queue {
+ function __construct($key) { global $config;
+ if ($config['queue']['enabled'] == 'fs') {
+ $this->lock = new Lock($key);
+ $key = str_replace('/', '::', $key);
+ $key = str_replace("\0", '', $key);
+ $this->key = "tmp/queue/$key/";
+ }
+ }
+
+ function push($str) { global $config;
+ if ($config['queue']['enabled'] == 'fs') {
+ $this->lock->get_ex();
+ file_put_contents($this->key.microtime(true), $str);
+ $this->lock->free();
+ }
+ return $this;
+ }
+
+ function pop($n = 1) { global $config;
+ if ($config['queue']['enabled'] == 'fs') {
+ $this->lock->get_ex();
+ $dir = opendir($this->key);
+ $paths = array();
+ while ($n > 0) {
+ $path = readdir($dir);
+ if ($path === FALSE) break;
+ elseif ($path == '.' || $path == '..') continue;
+ else { $paths[] = $path; $n--; }
+ }
+ $out = array();
+ foreach ($paths as $v) {
+ $out []= file_get_contents($this->key.$v);
+ unlink($this->key.$v);
+ }
+ $this->lock->free();
+ return $out;
+ }
+ }
+}
+
+// Don't use the constructor. Use the get_queue function.
+$queues = array();
+
+function get_queue($name) { global $queues;
+ return $queues[$name] = isset ($queues[$name]) ? $queues[$name] : new Queue($name);
+}