-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskProcess.php
More file actions
52 lines (46 loc) · 1.26 KB
/
TaskProcess.php
File metadata and controls
52 lines (46 loc) · 1.26 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
<?php
namespace WebmanTech\CrontabTask;
use InvalidArgumentException;
use Workerman\Crontab\Crontab;
use Workerman\Crontab\Parser;
final readonly class TaskProcess
{
public function __construct(
/**
* @var array<int, array<string, class-string<BaseTask>>>
*/
private array $tasks
)
{
}
/**
* 检查 tasks 参数是否正确
* @param array $tasks
* @return void
*/
public static function checkTasks(array $tasks): void
{
$parser = new Parser();
foreach ($tasks as [$cron, $task]) {
if (!$parser->isValid($cron)) {
throw new InvalidArgumentException('cron invalid: ' . $cron);
}
if (!is_string($task)) {
throw new InvalidArgumentException('task must be string');
}
if (!is_a($task, BaseTask::class, true)) {
throw new InvalidArgumentException('task must be instance of ' . BaseTask::class);
}
}
}
public function getTasks(): array
{
return $this->tasks;
}
public function onWorkerStart(): void
{
foreach ($this->tasks as [$cron, $task]) {
new Crontab($cron, fn() => $task::taskExec());
}
}
}