|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Job |
|
5
|
|
|
* |
|
6
|
|
|
* Simple, database based job queue. |
|
7
|
|
|
* |
|
8
|
|
|
* @package core |
|
9
|
|
|
* @author [email protected] |
|
10
|
|
|
* @copyright Caffeina srl - 2015-2016 - http://caffeina.com |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/* |
|
14
|
|
|
|
|
15
|
|
|
Here is the table needed : |
|
16
|
|
|
|
|
17
|
|
|
CREATE TABLE `queue` ( |
|
18
|
|
|
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, |
|
19
|
|
|
`type` varchar(128) DEFAULT NULL, |
|
20
|
|
|
`status` enum('PENDING','ACTIVE','DONE','ERROR') NOT NULL DEFAULT 'PENDING', |
|
21
|
|
|
`tries` int(5) unsigned DEFAULT '0', |
|
22
|
|
|
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
|
23
|
|
|
`scheduled_at` timestamp NULL, |
|
24
|
|
|
`activated_at` timestamp NULL DEFAULT NULL, |
|
25
|
|
|
`payload` text, |
|
26
|
|
|
`error` text, |
|
27
|
|
|
PRIMARY KEY (`id`), |
|
28
|
|
|
KEY `type` (`type`), |
|
29
|
|
|
KEY `status` (`status`), |
|
30
|
|
|
KEY `scheduled_at` (`scheduled_at`) |
|
31
|
|
|
) CHARSET=utf8; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
class Job extends Model { |
|
37
|
|
|
const _PRIMARY_KEY_ = "queue.id"; |
|
38
|
|
|
|
|
39
|
|
|
public $id, |
|
|
|
|
|
|
40
|
|
|
$type, |
|
41
|
|
|
$status = 'PENDING', |
|
42
|
|
|
$tries = 0, |
|
43
|
|
|
$created_at, |
|
44
|
|
|
$scheduled_at, |
|
45
|
|
|
$activated_at, |
|
46
|
|
|
$payload, |
|
47
|
|
|
$error; |
|
48
|
|
|
|
|
49
|
|
|
public static function queue($type, $payload=null, $when=null){ |
|
50
|
|
|
$now = gmdate("Y-m-d H:i:s"); |
|
51
|
|
|
$job = new static; |
|
52
|
|
|
$job->type = $type; |
|
53
|
|
|
$job->created_at = $now; |
|
54
|
|
|
$job->scheduled_at = $when ? gmdate("Y-m-d H:i:s",(is_int($when) ? $when : (strtotime($when)?:time()))) : $now; |
|
55
|
|
|
$job->payload = $payload !== null ? serialize($payload) : null; |
|
56
|
|
|
$job->save(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function register($type, $callback){ |
|
60
|
|
|
self::on("worker[{$type}]", $callback); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function cleanQueue($all=false){ |
|
64
|
|
|
$statuses = $all ? "'DONE','ERROR'" : "'DONE'"; |
|
65
|
|
|
return SQL::exec("DELETE FROM `".static::persistenceOptions('table')."` WHERE `status` IN ($statuses)"); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function execute(){ |
|
69
|
|
|
$condition = "status = 'PENDING' and `scheduled_at` <= NOW() ORDER BY `scheduled_at` ASC LIMIT 1"; |
|
70
|
|
|
if (($job = static::where($condition)) && ($job = current($job))){ |
|
|
|
|
|
|
71
|
|
|
// Lock chosen job rapidly |
|
72
|
|
|
SQL::update(static::persistenceOptions('table'),['id' => $job->id, 'status' => 'ACTIVE']); |
|
73
|
|
|
$job->run(); |
|
74
|
|
|
return $job; |
|
75
|
|
|
} else return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function run(){ |
|
79
|
|
|
$this->status = 'ACTIVE'; |
|
80
|
|
|
$this->activated_at = gmdate("Y-m-d H:i:s"); |
|
81
|
|
|
$this->tries++; |
|
82
|
|
|
$this->save(); |
|
83
|
|
|
$this->status = 'DONE'; |
|
84
|
|
|
self::trigger("worker[{$this->type}]", $this, $this->payload ? unserialize($this->payload) : null); |
|
85
|
|
|
$this->save(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function error($message=null){ |
|
89
|
|
|
$this->status = 'ERROR'; |
|
90
|
|
|
$this->error = $message; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function retry($message=null){ |
|
94
|
|
|
$this->status = 'PENDING'; |
|
95
|
|
|
$this->error = $message; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.