-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScheduleRunCommand.php
More file actions
155 lines (118 loc) · 3.95 KB
/
Copy pathScheduleRunCommand.php
File metadata and controls
155 lines (118 loc) · 3.95 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace Zero\Lib\Console\Commands;
use Throwable;
use Zero\Lib\Console\Command\CommandInterface;
use Zero\Lib\Console\Input;
use Zero\Lib\Console\Scheduling\ExecutionReport;
use Zero\Lib\Console\Scheduling\Schedule;
use Zero\Lib\Console\Scheduling\Scheduler;
use Zero\Lib\Log;
final class ScheduleRunCommand implements CommandInterface
{
public function getName(): string
{
return 'schedule:run';
}
public function getDescription(): string
{
return 'Execute the scheduled tasks that are due.';
}
public function getUsage(): string
{
return 'php zero schedule:run [--path=routes/cron.php]';
}
public function execute(array $argv, ?Input $input = null): int
{
$input ??= new Input([], []);
$pathOption = $input->option('path');
if ($pathOption === null) {
$pathOption = $this->valueFromArgv($argv, '--path');
}
$schedulePath = $this->normalizePath($pathOption ?? 'routes/cron.php');
if (! file_exists($schedulePath)) {
Log::channel('internal')->info('Scheduler definition not found; skipping run.', [
'path' => $schedulePath,
]);
return 0;
}
try {
$schedule = $this->loadSchedule($schedulePath);
} catch (Throwable $exception) {
Log::channel('internal')->error('Failed to load the scheduler definition.', [
'path' => $schedulePath,
'exception' => $exception::class,
'message' => $exception->getMessage(),
]);
return 1;
}
if ($schedule === null || $schedule->isEmpty()) {
Log::channel('internal')->info('No scheduled tasks registered; nothing to run.', [
'path' => $schedulePath,
]);
return 0;
}
$scheduler = new Scheduler($schedule);
$report = $scheduler->run();
$this->logSummary($report);
if ($report->failed() > 0) {
foreach ($report->failures() as $failure) {
$exception = $failure['exception'];
Log::channel('internal')->error('Scheduled task failed during execution.', [
'task' => $failure['task'],
'exception' => $exception?->getMessage(),
]);
}
return 1;
}
return 0;
}
private function loadSchedule(string $path): ?Schedule
{
Schedule::reset();
$definition = require $path;
if ($definition instanceof Schedule) {
return $definition;
}
$schedule = Schedule::instance();
if (is_callable($definition)) {
$definition($schedule);
return $schedule;
}
return $schedule->isEmpty() ? null : $schedule;
}
private function normalizePath(string $path): string
{
$path = trim($path);
if ($path === '') {
$path = 'routes/cron.php';
}
if ($this->isAbsolutePath($path)) {
return $path;
}
return base($path);
}
private function isAbsolutePath(string $path): bool
{
return str_starts_with($path, DIRECTORY_SEPARATOR)
|| (strlen($path) > 1 && ctype_alpha($path[0]) && $path[1] === ':');
}
private function valueFromArgv(array $argv, string $option): ?string
{
foreach ($argv as $argument) {
if (! str_starts_with($argument, $option . '=')) {
continue;
}
return substr($argument, strlen($option) + 1) ?: null;
}
return null;
}
private function logSummary(ExecutionReport $report): void
{
Log::channel('internal')->info('Scheduler run completed.', [
'executed' => $report->succeeded(),
'failed' => $report->failed(),
'skipped' => $report->skipped(),
]);
}
}