|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Resta\Worker; |
|
4
|
|
|
|
|
5
|
|
|
use Resta\Support\Utils; |
|
6
|
|
|
use Resta\Foundation\ApplicationProvider; |
|
7
|
|
|
use Resta\Contracts\ApplicationContracts; |
|
8
|
|
|
|
|
9
|
|
|
abstract class WorkerManagerAbstract extends ApplicationProvider |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $args = array(); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var null|object |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $worker; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* WorkerManager constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param ApplicationContracts $app |
|
25
|
|
|
* @param array $args |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(ApplicationContracts $app, $args=array()) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($app); |
|
30
|
|
|
|
|
31
|
|
|
if($this->app->runningInConsole()===false){ |
|
32
|
|
|
exception()->runtime('The worker can only be run as cli..'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->args = $args; |
|
36
|
|
|
|
|
37
|
|
|
$this->worker = $this->app->get('worker'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* get apply from cli for worker |
|
42
|
|
|
* |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getApply() |
|
46
|
|
|
{ |
|
47
|
|
|
if(isset($this->args['apply'])){ |
|
48
|
|
|
return $this->args['apply']; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return 'default'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* get data from cli for worker |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getData() |
|
60
|
|
|
{ |
|
61
|
|
|
if(isset($this->args['data'])){ |
|
62
|
|
|
return $this->args['data']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* get pause value |
|
70
|
|
|
* |
|
71
|
|
|
* @return int|mixed |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getPauseValue() |
|
74
|
|
|
{ |
|
75
|
|
|
if(isset($this->args['pause'])){ |
|
76
|
|
|
return $this->args['pause']; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return 0; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* get worker name |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed|void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getWorker() |
|
88
|
|
|
{ |
|
89
|
|
|
if(isset($this->args['worker'],$this->worker[$worker = strtolower($this->args['worker'])])){ |
|
90
|
|
|
return $worker; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
exception()->runtime('Any worker is not available in the container'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* check if worker is available |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function isWorkerAvailable() |
|
102
|
|
|
{ |
|
103
|
|
|
return isset($this->worker[$this->getWorker()]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* get pause for worker |
|
108
|
|
|
* |
|
109
|
|
|
* @param null|\int $pause |
|
110
|
|
|
*/ |
|
111
|
|
|
public function pause($pause=null) |
|
112
|
|
|
{ |
|
113
|
|
|
if(is_null($pause)){ |
|
114
|
|
|
sleep($this->getPauseValue()); |
|
115
|
|
|
} |
|
116
|
|
|
else{ |
|
117
|
|
|
sleep($pause); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* run job class |
|
123
|
|
|
* |
|
124
|
|
|
* @param $name |
|
125
|
|
|
* @param $arguments |
|
126
|
|
|
* @return mixed|void |
|
127
|
|
|
*/ |
|
128
|
|
|
public function __call($name, $arguments) |
|
129
|
|
|
{ |
|
130
|
|
|
$job = __NAMESPACE__.'\\'.ucfirst($name).'Job'; |
|
131
|
|
|
if(Utils::isNamespaceExists($job)){ |
|
132
|
|
|
return $this->app->resolve($job,['worker'=>$this])->execute(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
exception()->runtime('Job Class not found'); |
|
136
|
|
|
} |
|
137
|
|
|
} |