1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Resta\Console\Source\Schedule;
|
4
|
|
|
|
5
|
|
|
use Resta\Schedule\ScheduleManager;
|
6
|
|
|
use Resta\Support\ClosureDispatcher;
|
7
|
|
|
use Resta\Support\JsonHandler;
|
8
|
|
|
use Resta\Support\Utils;
|
9
|
|
|
use Resta\Console\ConsoleOutputter;
|
10
|
|
|
use Resta\Console\ConsoleListAccessor;
|
11
|
|
|
|
12
|
|
|
class Schedule extends ConsoleOutputter {
|
13
|
|
|
|
14
|
|
|
use ConsoleListAccessor;
|
15
|
|
|
|
16
|
|
|
/**
|
17
|
|
|
* @var $type
|
|
|
|
|
18
|
|
|
*/
|
19
|
|
|
public $type = 'schedule';
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* @var $define
|
|
|
|
|
23
|
|
|
*/
|
24
|
|
|
public $define = 'creates schedule for application';
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* @var $commandRule
|
|
|
|
|
28
|
|
|
*/
|
29
|
|
|
public $commandRule = [
|
30
|
|
|
'create' => ['schedule'],
|
31
|
|
|
'register' => ['schedule'],
|
32
|
|
|
'run' => ['schedule'],
|
33
|
|
|
'delete' => ['schedule']
|
34
|
|
|
];
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* schedule clear
|
38
|
|
|
*
|
39
|
|
|
* @return void
|
40
|
|
|
*/
|
41
|
|
|
public function clear()
|
42
|
|
|
{
|
43
|
|
|
shell_exec('crontab -r');
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* @return void
|
48
|
|
|
*/
|
49
|
|
|
public function create(){
|
50
|
|
|
|
51
|
|
|
$schedulePath = app()->path()->schedule();
|
52
|
|
|
|
53
|
|
|
if(!file_exists($schedulePath)){
|
54
|
|
|
$this->directory['schedule'] = $schedulePath;
|
55
|
|
|
$this->file->makeDirectory($this);
|
56
|
|
|
}
|
57
|
|
|
|
58
|
|
|
$this->argument['scheduleNamespace'] = app()->namespace()->schedule();
|
59
|
|
|
$this->argument['scheduleClass'] = ucfirst($this->argument['schedule']).'';
|
60
|
|
|
$this->argument['projectName'] = strtolower($this->projectName());
|
61
|
|
|
|
62
|
|
|
$this->touch['schedule/schedule']= $schedulePath.'/'.$this->argument['schedule'].'.php';
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->file->touch($this);
|
66
|
|
|
|
67
|
|
|
echo $this->classical(' > Schedule file called as "'.$this->argument['schedule'].'" has been successfully created in the '.$schedulePath.'');
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
/**
|
71
|
|
|
* schedule delete
|
72
|
|
|
*
|
73
|
|
|
* @throws \Resta\Exception\FileNotFoundException
|
74
|
|
|
*/
|
75
|
|
|
public function delete()
|
76
|
|
|
{
|
77
|
|
|
$this->scheduleJsonFile();
|
78
|
|
|
$this->clear();
|
79
|
|
|
foreach (JsonHandler::get() as $schedule=>$items){
|
80
|
|
|
if($schedule!==$this->argument['schedule']){
|
81
|
|
|
app()->command('schedule register','schedule:'.$schedule);
|
|
|
|
|
82
|
|
|
}
|
83
|
|
|
}
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
public function list()
|
87
|
|
|
{
|
88
|
|
|
exec('crontab -l',$list);
|
89
|
|
|
|
90
|
|
|
$this->table->setHeaders(['no','minute','hour','day','month','week','schedule','description']);
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
foreach ($list as $key=>$item){
|
94
|
|
|
|
95
|
|
|
if(preg_match('@.*php api schedule run '.strtolower($this->projectName()).'.*@is',$item,$result)){
|
96
|
|
|
if(isset($result[0])){
|
97
|
|
|
|
98
|
|
|
$cron = [];
|
99
|
|
|
|
100
|
|
|
if(preg_match('@(.*)\scd@',$result[0],$cron)){
|
101
|
|
|
$cron = (isset($cron[1])) ? explode(' ',$cron[1]) : '';
|
102
|
|
|
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
$scheduleName = '';
|
106
|
|
|
|
107
|
|
|
if(preg_match('@schedule\:(.*?)\s@',$result[0],$scheduler)){
|
108
|
|
|
$scheduleName = (isset($scheduler[1])) ? $scheduler[1] : '';
|
109
|
|
|
|
110
|
|
|
$schedulerInstance = $this->scheduleInstance(ucfirst($scheduleName));
|
111
|
|
|
$description = ClosureDispatcher::bind($schedulerInstance)->call(function(){
|
112
|
|
|
return $this->description;
|
|
|
|
|
113
|
|
|
});
|
114
|
|
|
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
$this->table->addRow([
|
118
|
|
|
$key+1,
|
119
|
|
|
isset($cron[0]) ? $cron[0] : '',
|
120
|
|
|
isset($cron[1]) ? $cron[1] : '',
|
121
|
|
|
isset($cron[2]) ? $cron[2] : '',
|
122
|
|
|
isset($cron[3]) ? $cron[3] : '',
|
123
|
|
|
isset($cron[4]) ? $cron[4] : '',
|
124
|
|
|
$scheduleName,
|
125
|
|
|
isset($description) ? $description : '',
|
126
|
|
|
]);
|
127
|
|
|
}
|
128
|
|
|
}
|
129
|
|
|
}
|
130
|
|
|
|
131
|
|
|
echo $this->table->getTable();
|
132
|
|
|
}
|
133
|
|
|
|
134
|
|
|
/**
|
135
|
|
|
* @return void
|
136
|
|
|
*/
|
137
|
|
|
public function register()
|
138
|
|
|
{
|
139
|
|
|
$schedules = Utils::glob(app()->path()->schedule());
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
if(isset($schedules[$this->argument['schedule']])){
|
143
|
|
|
|
144
|
|
|
$scheduleNamespace = Utils::getNamespace($schedules[$this->argument['schedule']]);
|
145
|
|
|
$scheduleInstance = app()->resolve($scheduleNamespace);
|
146
|
|
|
|
147
|
|
|
$scheduleManager = new ScheduleManager();
|
148
|
|
|
$scheduleInstance->when($scheduleManager);
|
149
|
|
|
|
150
|
|
|
$cronScheduler = implode(' ',$scheduleManager->getCronScheduler());
|
151
|
|
|
|
152
|
|
|
$command = $cronScheduler.' cd '.root.' && php api schedule run munch schedule:'.lcfirst($this->argument['schedule']).' >> /dev/null 2>&1';
|
153
|
|
|
|
154
|
|
|
if($this->cronjob_exists($command)===false){
|
155
|
|
|
|
156
|
|
|
$output = shell_exec('crontab -l');
|
157
|
|
|
file_put_contents('/tmp/crontab.txt', $output.''.$command.''.PHP_EOL);
|
158
|
|
|
exec('crontab /tmp/crontab.txt');
|
159
|
|
|
|
160
|
|
|
$this->scheduleJsonFile();
|
161
|
|
|
JsonHandler::set($this->argument['schedule'].'.namespace',$scheduleNamespace);
|
162
|
|
|
JsonHandler::set($this->argument['schedule'].'.date',date('Y-m-d H:i:s'));
|
163
|
|
|
JsonHandler::set($this->argument['schedule'].'.command',$cronScheduler);
|
164
|
|
|
|
165
|
|
|
echo $this->info('Cron has been added');
|
166
|
|
|
}
|
167
|
|
|
|
168
|
|
|
}
|
169
|
|
|
|
170
|
|
|
|
171
|
|
|
}
|
172
|
|
|
|
173
|
|
|
public function run()
|
174
|
|
|
{
|
175
|
|
|
$schedules = Utils::glob(app()->path()->schedule());
|
176
|
|
|
|
177
|
|
|
if(isset($schedules[$this->argument['schedule']])){
|
178
|
|
|
$scheduleNamespace = Utils::getNamespace($schedules[$this->argument['schedule']]);
|
179
|
|
|
$scheduleInstance = app()->resolve($scheduleNamespace);
|
180
|
|
|
|
181
|
|
|
$scheduleInstance->command();
|
182
|
|
|
}
|
183
|
|
|
}
|
184
|
|
|
|
185
|
|
|
public function update()
|
186
|
|
|
{
|
187
|
|
|
$this->clear();
|
188
|
|
|
$this->scheduleJsonFile();
|
189
|
|
|
foreach (JsonHandler::get() as $schedule=>$items){
|
190
|
|
|
app()->command('schedule register','schedule:'.lcfirst($schedule));
|
|
|
|
|
191
|
|
|
}
|
192
|
|
|
}
|
193
|
|
|
|
194
|
|
|
private function cronjob_exists($command){
|
195
|
|
|
|
196
|
|
|
$cronjob_exists=false;
|
197
|
|
|
|
198
|
|
|
exec('crontab -l', $crontab);
|
199
|
|
|
|
200
|
|
|
|
201
|
|
|
if(isset($crontab)&&is_array($crontab)){
|
202
|
|
|
|
203
|
|
|
$crontab = array_flip($crontab);
|
204
|
|
|
|
205
|
|
|
if(isset($crontab[$command])){
|
206
|
|
|
|
207
|
|
|
$cronjob_exists=true;
|
208
|
|
|
|
209
|
|
|
}
|
210
|
|
|
|
211
|
|
|
}
|
212
|
|
|
return $cronjob_exists;
|
213
|
|
|
}
|
214
|
|
|
|
215
|
|
|
/**
|
216
|
|
|
* @param $schedule
|
217
|
|
|
* @return mixed|null
|
218
|
|
|
*/
|
219
|
|
|
private function scheduleInstance($schedule)
|
220
|
|
|
{
|
221
|
|
|
$schedules = Utils::glob(app()->path()->schedule());
|
222
|
|
|
|
223
|
|
|
if(isset($schedules[$schedule])){
|
224
|
|
|
$scheduleNamespace = Utils::getNamespace($schedules[$schedule]);
|
225
|
|
|
return $scheduleInstance = app()->resolve($scheduleNamespace);
|
|
|
|
|
226
|
|
|
}
|
227
|
|
|
|
228
|
|
|
return null;
|
229
|
|
|
|
230
|
|
|
}
|
231
|
|
|
|
232
|
|
|
private function scheduleJsonFile()
|
233
|
|
|
{
|
234
|
|
|
JsonHandler::$file = path()->kernel().''.DIRECTORY_SEPARATOR.'Schedule.json';
|
235
|
|
|
}
|
236
|
|
|
|
237
|
|
|
}
|
238
|
|
|
|