Format queue.php

This commit is contained in:
Zankaria 2024-02-16 14:48:42 +01:00
parent 760431606d
commit 55034762b0

View File

@ -1,49 +1,49 @@
<?php <?php
class Queue { class Queue {
function __construct($key) { global $config; function __construct($key) { global $config;
if ($config['queue']['enabled'] == 'fs') { if ($config['queue']['enabled'] == 'fs') {
$this->lock = Locks::get_lock($config, $key); $this->lock = Locks::get_lock($config, $key);
$key = str_replace('/', '::', $key); $key = str_replace('/', '::', $key);
$key = str_replace("\0", '', $key); $key = str_replace("\0", '', $key);
$this->key = "tmp/queue/$key/"; $this->key = "tmp/queue/$key/";
} }
} }
function push($str) { global $config; function push($str) { global $config;
if ($config['queue']['enabled'] == 'fs') { if ($config['queue']['enabled'] == 'fs') {
$this->lock->get_ex(); $this->lock->get_ex();
file_put_contents($this->key.microtime(true), $str); file_put_contents($this->key.microtime(true), $str);
$this->lock->free(); $this->lock->free();
} }
return $this; return $this;
} }
function pop($n = 1) { global $config; function pop($n = 1) { global $config;
if ($config['queue']['enabled'] == 'fs') { if ($config['queue']['enabled'] == 'fs') {
$this->lock->get_ex(); $this->lock->get_ex();
$dir = opendir($this->key); $dir = opendir($this->key);
$paths = array(); $paths = array();
while ($n > 0) { while ($n > 0) {
$path = readdir($dir); $path = readdir($dir);
if ($path === FALSE) break; if ($path === FALSE) break;
elseif ($path == '.' || $path == '..') continue; elseif ($path == '.' || $path == '..') continue;
else { $paths[] = $path; $n--; } else { $paths[] = $path; $n--; }
} }
$out = array(); $out = array();
foreach ($paths as $v) { foreach ($paths as $v) {
$out []= file_get_contents($this->key.$v); $out []= file_get_contents($this->key.$v);
unlink($this->key.$v); unlink($this->key.$v);
} }
$this->lock->free(); $this->lock->free();
return $out; return $out;
} }
} }
} }
// Don't use the constructor. Use the get_queue function. // Don't use the constructor. Use the get_queue function.
$queues = array(); $queues = array();
function get_queue($name) { global $queues; function get_queue($name) { global $queues;
return $queues[$name] = isset ($queues[$name]) ? $queues[$name] : new Queue($name); return $queues[$name] = isset ($queues[$name]) ? $queues[$name] : new Queue($name);
} }