-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkMan.php
119 lines (98 loc) · 2.36 KB
/
WorkMan.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Created by PhpStorm.
* User: qihuajun
* Date: 2017/5/18
* Time: 下午4:42
*/
namespace rossoneri\workman;
use rossoneri\workman\job\JobInterface;
use rossoneri\workman\queue\BeanstalkdQueue;
use yii\base\Component;
class WorkMan extends Component
{
/**
* queue config array
*
* please refer to Queue class to see details
*
* @var array
*/
public $queue;
/**
* worker options array
*
* please refer to Worker class to see details
*
* @var array
*/
public $workerOptions = [];
/**
* @var Registry Redis Id, If you want to enable registry function, this must be specified
*/
public $registeryRedisId;
/**
* @var BeanstalkdQueue
*/
private $beanstalkdQueue;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->beanstalkdQueue = new BeanstalkdQueue($this->queue);
}
/**
* Dispatch a job
*
* @param JobInterface $job
* @param $tube
* @param int $priority
* @param int $delay
* @param int $ttr
*/
public function dispatch(JobInterface $job,$tube=null,$priority=100,$delay=0,$ttr=10){
$id = $this->beanstalkdQueue->putInTube($job,$tube,$priority,$delay,$ttr);
$job->setId($id);
}
/**
* Start to work on a tube
*
* @param $watches
* @param array $options
*/
public function work($watches,$options = []){
$config = [
'queue' => $this->beanstalkdQueue,
'watches' => $watches
];
if($this->registeryRedisId){
$registry = new WorkerRegistry(['redisComponentId'=>$this->registeryRedisId]);
$config['registry'] = $registry;
}
$config = array_merge($config,$this->workerOptions,$options);
$worker = new Worker($config);
$worker->work();
}
/**
* return worker registry
*
* @return null|WorkerRegistry
*/
public function getRegistry(){
if($this->registeryRedisId){
$registry = new WorkerRegistry(['redisComponentId'=>$this->registeryRedisId]);
return $registry;
}
return null;
}
/**
* get the queue
*
* @return BeanstalkdQueue
*/
public function getQueue(){
return $this->beanstalkdQueue;
}
}