-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue.php
More file actions
48 lines (44 loc) · 1.25 KB
/
queue.php
File metadata and controls
48 lines (44 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
class Queue
{
var $conn = null;
function __construct($servername, $username, $password, $dbname){
$this->conn = new mysqli($servername, $username, $password, $dbname);
if ($this->conn->connect_error) {
throw(new Exception("Connection failed: " . $this->conn->connect_error));
}
}
function push($payload){
$sql = 'SELECT pushJob(\'' . $this->conn->escape_string($payload) . '\') as pushResult;';
$result = $this->conn->query($sql);
if($result && $row = $result->fetch_assoc()) {
if($row['pushResult'] > 0){
return $row['pushResult'];
}
throw(new Exception('Could not push job, could not create!'));
}
throw(new Exception('Could not push job, query failed: ' . $sql));
}
function pop($clientId){
$sql = 'CALL popJob(' . $this->conn->escape_string($clientId) . ');';
$result = $this->conn->query($sql);
$out = array();
while($result && $row = $result->fetch_assoc()) {
$out[] = $row;
if(!empty($row['error'])){
throw(new Exception('popJob failed: ' . $row['error']));
}
}
return $out;
}
function show(){
$sql = 'SELECT * FROM job;';
$result = $this->conn->query($sql);
$out = array();
while($result && $row = $result->fetch_assoc()) {
$out[] = $row;
}
return $out;
}
}
?>