-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSchedulerCommand.php
More file actions
252 lines (204 loc) · 6.48 KB
/
SchedulerCommand.php
File metadata and controls
252 lines (204 loc) · 6.48 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
declare(strict_types=1);
namespace Bow\Console\Command;
use DateTime;
use Bow\Console\AbstractCommand;
use Bow\Console\Color;
use Bow\Scheduler\Scheduler;
use Bow\Configuration\Loader;
class SchedulerCommand extends AbstractCommand
{
/**
* Run the scheduler once (execute all due events)
*
* @return void
*/
public function run(): void
{
$scheduler = $this->getScheduler();
echo Color::green("Running scheduler...\n");
$results = $scheduler->run();
if (empty($results)) {
echo Color::yellow("No scheduled events are due.\n");
return;
}
foreach ($results as $result) {
$this->displayResult($result);
}
echo Color::green("\nScheduler run completed.\n");
}
/**
* Start the scheduler daemon (continuous loop)
*
* @return void
*/
public function work(): void
{
$scheduler = $this->getScheduler();
echo Color::green("Starting scheduler daemon...\n");
echo Color::yellow("Press Ctrl+C to stop.\n\n");
// Set up custom logger for console output
$scheduler->setLogger(function (string $message) {
echo $message . "\n";
});
$scheduler->start();
}
/**
* List all registered scheduled events
*
* @return void
*/
public function list(): void
{
$scheduler = $this->getScheduler();
$events = $scheduler->getEvents();
if (empty($events)) {
echo Color::yellow("No scheduled events registered.\n");
return;
}
echo Color::green("Registered Scheduled Events:\n");
echo str_repeat('-', 100) . "\n";
printf("%-45s | %-10s | %-15s | %s\n", "Description", "Type", "Expression", "Next Due");
echo str_repeat('-', 100) . "\n";
$now = new DateTime();
foreach ($events as $event) {
$description = $event->getDescription();
$type = $event->getType();
$expression = $event->getCronExpression();
$isDue = $event->isDue($now);
// Truncate long descriptions
if (strlen($description) > 43) {
$description = substr($description, 0, 40) . '...';
}
$dueStatus = $isDue ? Color::green("DUE NOW") : Color::yellow("waiting");
printf(
"%-45s | %-10s | %-15s | %s\n",
$description,
$type,
$expression,
$dueStatus
);
}
echo str_repeat('-', 100) . "\n";
echo Color::green("Total: " . count($events) . " event(s)\n");
}
/**
* Show the next run time for all events
*
* @return void
*/
public function next(): void
{
$scheduler = $this->getScheduler();
$events = $scheduler->getEvents();
if (empty($events)) {
echo Color::yellow("No scheduled events registered.\n");
return;
}
echo Color::green("Next Run Times:\n");
echo str_repeat('-', 80) . "\n";
$now = new DateTime();
foreach ($events as $event) {
$description = $event->getDescription();
$isDue = $event->isDue($now);
$status = $isDue
? Color::green("DUE NOW")
: Color::yellow("waiting");
echo sprintf(
"[%-8s] %-50s %s (%s)\n",
$event->getType(),
$description,
$status,
$event->getCronExpression()
);
}
echo str_repeat('-', 80) . "\n";
}
/**
* Test run a specific event by its index
*
* @param int $index The 0-based index of the event to run
* @return void
*/
public function test(int $index = 0): void
{
$scheduler = $this->getScheduler();
$events = $scheduler->getEvents();
if (empty($events)) {
echo Color::yellow("No scheduled events registered.\n");
return;
}
if ($index < 0 || $index >= count($events)) {
echo Color::red("Invalid event index: {$index}\n");
echo Color::yellow("Use 'php bow schedule:list' to see available events (0-indexed).\n");
return;
}
$event = $events[$index];
$description = $event->getDescription();
echo Color::green("Running event: {$description}\n");
try {
$startTime = microtime(true);
$event->run();
$endTime = microtime(true);
$duration = round(($endTime - $startTime) * 1000, 2);
echo Color::green("Event completed successfully in {$duration}ms\n");
$output = $event->getOutput();
if ($output) {
echo Color::yellow("Output:\n{$output}\n");
}
} catch (\Throwable $e) {
echo Color::red("Event failed: " . $e->getMessage() . "\n");
echo Color::yellow("Stack trace:\n" . $e->getTraceAsString() . "\n");
}
}
/**
* Get the scheduler instance
*
* @return Scheduler
*/
private function getScheduler(): Scheduler
{
$scheduler = Scheduler::getInstance();
$this->loadSchedulerFile($scheduler);
return $scheduler;
}
/**
* Load the scheduler from kernel
*
* @param Scheduler $scheduler
* @return void
*/
private function loadSchedulerFile(Scheduler $scheduler): void
{
$kernel = Loader::getInstance();
$kernel->schedules($scheduler);
}
/**
* Display an event result
*
* @param array $result
* @return void
*/
private function displayResult(array $result): void
{
$status = match ($result['status']) {
'success' => Color::green('[SUCCESS]'),
'failed' => Color::red('[FAILED]'),
'skipped' => Color::yellow('[SKIPPED]'),
default => Color::yellow('[UNKNOWN]'),
};
echo sprintf(
"%s [%s] %s\n",
$status,
$result['type'],
$result['description']
);
if ($result['error']) {
echo Color::red(" Error: {$result['error']}\n");
}
if ($result['started_at'] && $result['finished_at']) {
$duration = $result['finished_at']->getTimestamp() - $result['started_at']->getTimestamp();
echo Color::yellow(" Duration: {$duration}s\n");
}
}
}