-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResqueEnqueue.php
More file actions
74 lines (62 loc) · 1.88 KB
/
ResqueEnqueue.php
File metadata and controls
74 lines (62 loc) · 1.88 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
69
70
71
72
73
74
<?php
namespace janakawicks\resque;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
class ResqueEnqueue extends Component
{
public $namespace = 'resque:';
public $queue;
public function enqueue()
{
$enqueue = [];
//arg[0] => class
//arg[1...] => arguments
$args = func_get_args();
$enqueue['class'] = $args[0];
foreach($args as $k => $v){
if ($k < 1)
continue;
$enqueue['args'][] = $v;
}
if(!isset($enqueue['args']))
$enqueue['args'] = [];
return Yii::$app->redis->executeCommand(
'lpush', [
$this->namespace . 'queue:' . $this->queue,
json_encode($enqueue)
]
);
}
public function listWorkers($queues=[])
{
if(empty($queues))
return $this->_listWorkers($this->queue);
else {
$workers = [];
foreach($queues as $q){
$workers = $workers + $this->_listWorkers($q);
}
return $workers;
}
}
public function _listWorkers($queue)
{
//returns {host, pid, status, started_at || running details}
$workers = [];
$wrks = Yii::$app->redis->executeCommand('keys', ['resque:worker:*:'.$queue.'*']);
foreach($wrks as $w){
$arr = preg_split("/:/", $w);
if(!isset($arr[5])){
$workers[$arr[3]] = [ "host" => $arr[2], "pid" => $arr[3], "queue" => $arr[4], "status" => "running"];
$details = json_decode(Yii::$app->redis->executeCommand('get', [$w]), true);
$workers[$arr[3]]['started_at'] = date('Y-m-d h:i:s', strtotime($details["run_at"]));
}else if (!isset($workers[$arr[3]])){
$workers[$arr[3]] = [ "host" => $arr[2], "pid" => $arr[3], "queue" => $arr[4], "status" => "waiting" ];
$details = Yii::$app->redis->executeCommand('get', [$w]);
$workers[$arr[3]]['started_at'] = date('Y-m-d h:i:s', strtotime($details));
}
}
return $workers;
}
}