forked from wayhood/yii2-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Job.php
59 lines (49 loc) · 1.14 KB
/
Job.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
<?php
/**
* Created by PhpStorm.
* User: yiistudio
* Date: 11/30/14
* Time: 10:23 PM
*/
namespace wh\queue;
use Yii;
class Job
{
protected $queueObject;
protected $payload;
protected $queueName;
/**
* 一个Job实例
*
* @param $queueObject
* @param $payload
* @param $queueName
*/
public function __construct($queueObject, $payload, $queueName)
{
$this->queueObject = $queueObject;
$this->payload = $payload;
$this->queueName = $queueName;
}
public function run()
{
$this->resolveAndRun(json_decode($this->payload, true));
}
public function getQueueObject()
{
return $this->queueObject;
}
protected function resolveAndRun(array $payload)
{
list($class, $method) = $this->resolveJob($payload['job']);
$instance = Yii::createObject([
'class' => $class
]);
$instance->{$method}($this, $payload['data']);
}
protected function resolveJob($job)
{
$segments = explode('@', $job);
return count($segments) > 1 ? $segments : array($segments[0], 'run');
}
}