-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCronTaskBot.php
95 lines (70 loc) · 2.1 KB
/
CronTaskBot.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
<?php
namespace Ling\CronTaskBot;
use Ling\CronTaskBot\CronTask\CronTaskInterface;
class CronTaskBot implements CronTaskBotInterface
{
public static function create()
{
return new static();
}
/**
* @param array $tasks , array of name => CronTaskInterface
* @return mixed
*/
public function executeTasks(array $tasks): array
{
$ret = [];
foreach ($tasks as $taskName => $task) {
if ($task instanceof CronTaskInterface) {
$ret[] = $this->executeTask($taskName, $task);
} else {
$ret[] = $this->executeNotATask($taskName, $task);
}
}
return $ret;
}
//--------------------------------------------
//
//--------------------------------------------
protected function executeTask($name, CronTaskInterface $task)
{
$ret = $this->getDefaultTaskReturn($name);
try {
$ret['task_label'] = $task->getLabel();
$task->execute();
$ret['is_successful'] = (int)$task->isSuccessful();
$ret['info_messages'] = $task->getInfoMessages();
$ret['error_messages'] = $task->getErrorMessages();
} catch (\Exception $e) {
$ret['is_successful'] = 0;
$ret['exception'] = "$e";
}
$ret['execution_end_date'] = $this->now();
return $ret;
}
protected function executeNotATask($name, $thing)
{
return $this->getDefaultTaskReturn($name);
}
//--------------------------------------------
//
//--------------------------------------------
private function now()
{
return date("Y-m-d H:i:s");
}
private function getDefaultTaskReturn($name)
{
$now = $this->now();
return [
'task_name' => $name,
'task_label' => "",
'execution_start_date' => $now,
'execution_end_date' => $now,
'is_successful' => 0,
'info_messages' => [],
'error_messages' => [],
'exception' => "",
];
}
}