-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplequeue.php
More file actions
68 lines (68 loc) · 1.78 KB
/
simplequeue.php
File metadata and controls
68 lines (68 loc) · 1.78 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Simple queue class
*
* USAGE:
* $jobData = array(
* 'content' => $text_for_yandex,
* 'site_name' => 'www.yoursite.ru'
* );
* $qs = new SimpleQueue($datasource, 'QueueTableName');
* $qs->putInQueue($jobData);
*/
class SimpleQueue {
private $datasource;
private $queue_table;
/**
*
* @param object $datasource MySQLi object or other, implementing "escape" and "query" methods
* @param type $queue_table Table name for save queue to
*/
public function __construct($datasource, $queue_table) {
$this->datasource = $datasource;
$this->queue_table = $queue_table;
}
/**
*
* @param string[] $jobData Data array 'tbl_column' => 'value'
* @return integer Inserted row id
*/
public function putInQueue($jobData) {
$insertsql = array(
'`created` = ' . date("'YmdHis'", time())
);
foreach ($jobData as $column => $value) {
$insertsql[] = '`' . $column . "` = '" . $this->datasource->escape($value) . "'";
}
$sql = 'INSERT INTO `' . $this->queue_table . '` set ' . implode(', ', $insertsql);
if (($result = $this->datasource->query($sql)) == false) {
return false;
}
return $this->datasource->insert_id;
}
/**
*
* @return string[][] Array of active jobs 'jobid' => jobdata[]
*/
public function getActiveJobs() {
$sql = 'SELECT * FROM `' . $this->queue_table . '` where `status` = "new"';
if (!($result = $this->datasource->query($sql))) {
return false;
}
$list = array();
while ($row = $result->fetch_assoc()) {
$list[$row['id']] = $row;
}
return $list;
}
/**
* Mark job as done by id
*
* @param integer $jobId
* @return boolean
*/
public function markJobAsDone($jobId) {
$sql = 'UPDATE `' . $this->queue_table . '` set `status` = "done" where id = ' . (int) $jobId;
return $this->datasource->query($sql);
}
}