-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSchedule.php
More file actions
64 lines (55 loc) · 1.45 KB
/
Schedule.php
File metadata and controls
64 lines (55 loc) · 1.45 KB
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
<?php
namespace WebmanTech\CrontabTask;
use InvalidArgumentException;
class Schedule
{
private array $config = [
'process_name_prefix' => 'cron_task_',
];
private array $processes = [];
public function __construct(array $config = [])
{
$this->config = array_merge($this->config, $config);
}
/**
* 添加单个定时任务,独立进程
* @param string $name
* @param string $cron
* @param string $task
* @return $this
*/
public function addTask(string $name, string $cron, string $task): self
{
return $this->addTasks($name, [
[$cron, $task]
]);
}
/**
* 添加多个定时任务,在同个进程中(注意会存在阻塞)
* @param string $name
* @param array $tasks
* @return $this
*/
public function addTasks(string $name, array $tasks): self
{
$name = $this->config['process_name_prefix'] . $name;
if (isset($this->processes[$name])) {
throw new InvalidArgumentException('Task already exists: ' . $name);
}
TaskProcess::checkTasks($tasks);
$this->processes[$name] = [
'handler' => TaskProcess::class,
'constructor' => [
'tasks' => $tasks,
],
];
return $this;
}
/**
* @return array
*/
public function buildProcesses(): array
{
return $this->processes;
}
}