forked from doppar/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCronRunCommand.php
More file actions
474 lines (400 loc) · 14 KB
/
CronRunCommand.php
File metadata and controls
474 lines (400 loc) · 14 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?php
namespace Phaseolies\Console\Commands\Cron;
use App\Schedule\Schedule;
use Phaseolies\Console\Schedule\Command;
use Symfony\Component\Process\Process;
use React\EventLoop\Loop;
class CronRunCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'cron:run {--daemon : Run in daemon mode for second-based schedules}';
/**
* The description of the console command.
*
* @var string
*/
protected $description = 'Run the scheduled commands';
/**
* Track last execution times for second-based commands (timestamp in seconds)
*
* @var array
*/
protected $lastExecution = [];
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$isDaemon = $this->option('daemon');
if ($isDaemon) {
return $this->runDaemonMode();
}
return $this->runStandardMode();
}
/**
* Run in standard mode (called by system cron every minute)
*
* @return int
*/
protected function runStandardMode(): int
{
return $this->executeWithTiming(function () {
$schedule = new Schedule();
$schedule->schedule($schedule);
$allCommands = $schedule->getCommands();
$secondBasedCommands = [];
$regularCommands = [];
foreach ($allCommands as $command) {
if ($command->isSecondSchedule()) {
$secondBasedCommands[] = $command;
} else {
$regularCommands[] = $command;
}
}
// Run regular commands
$regularDueCommands = array_filter($regularCommands, fn($cmd) => $cmd->isDue());
foreach ($regularDueCommands as $command) {
$this->executeCommand($command);
}
if (!empty($secondBasedCommands)) {
$this->displayInfo('Found ' . count($secondBasedCommands) . ' second-based schedule(s)');
// Check if daemon is running
if (!$this->isDaemonRunning()) {
$this->displayWarning('Second-based schedules detected but daemon is not running!');
$this->displayInfo('Start the daemon with: php pool cron:run --daemon');
}
}
$totalExecuted = count($regularDueCommands);
if ($totalExecuted > 0) {
$this->newLine(1);
$this->displaySuccess('Executed ' . $totalExecuted . ' scheduled command(s)');
} else {
$this->displayInfo('No scheduled commands are ready to run.');
}
return Command::SUCCESS;
});
}
/**
* Run in daemon mode for second-based schedules using React PHP Event Loop
*
* @return int
*/
protected function runDaemonMode(): int
{
$this->displayInfo('Starting doppar cron daemon...');
$this->displayInfo('Monitoring for second-based schedules...');
$this->displayInfo('Press Ctrl+C to stop');
$this->newLine();
// Write PID file to track daemon
$this->writeDaemonPid();
// Handle SIGTERM and SIGINT for graceful shutdown
if (function_exists('pcntl_signal')) {
Loop::addSignal(SIGTERM, function () {
$this->displayInfo('Received SIGTERM, shutting down gracefully...');
$this->cleanupDaemonPid();
Loop::stop();
});
Loop::addSignal(SIGINT, function () {
$this->newLine();
$this->displayInfo('Received SIGINT, shutting down gracefully...');
$this->cleanupDaemonPid();
Loop::stop();
});
}
// Register shutdown handler to clean up
register_shutdown_function(function () {
$this->cleanupDaemonPid();
});
// Check every second for due commands
Loop::addPeriodicTimer(1.0, function () {
try {
$schedule = new Schedule();
$schedule->schedule($schedule);
$allCommands = $schedule->getCommands();
// Filter only second-based commands
$secondBasedCommands = array_filter(
$allCommands,
fn($cmd) => $cmd->isSecondSchedule()
);
if (empty($secondBasedCommands)) {
// Log once every minute if no commands found
static $lastWarning = 0;
if (time() - $lastWarning >= 60) {
$this->displayWarning('[' . date('H:i:s') . '] No second-based schedules found. Daemon continues monitoring...');
$lastWarning = time();
}
return;
}
$currentTimestamp = time();
// Check and run due commands
foreach ($secondBasedCommands as $command) {
$this->processSecondBasedCommand($command, $currentTimestamp);
}
} catch (\Exception $e) {
$this->displayError('[' . date('H:i:s') . '] Daemon error: ' . $e->getMessage());
// Continue running even if there's an error
}
});
Loop::run();
return Command::SUCCESS;
}
/**
* Process a second-based command
*
* @param mixed $command
* @param int $currentTimestamp
* @return void
*/
protected function processSecondBasedCommand($command, int $currentTimestamp): void
{
$commandKey = $this->getCommandKey($command);
// Get the interval in seconds from the command
$interval = $this->getCommandInterval($command);
if (!$interval) {
// If we can't determine interval, fall back to isDue() check with throttling
if ($command->isDue()) {
if (
!isset($this->lastExecution[$commandKey]) ||
($currentTimestamp - $this->lastExecution[$commandKey]) >= 1
) {
$this->displayInfo('[' . date('H:i:s') . '] Executing: ' . $command->getCommand());
$this->executeCommand($command, true);
$this->lastExecution[$commandKey] = $currentTimestamp;
}
}
return;
}
// Check if it's time to run based on the interval
if (!isset($this->lastExecution[$commandKey])) {
// First run
$this->displayInfo('[' . date('H:i:s') . '] Executing: ' . $command->getCommand() . ' (first run)');
$this->executeCommand($command, true);
$this->lastExecution[$commandKey] = $currentTimestamp;
} else {
// Check if enough time has passed
$timeSinceLastExecution = $currentTimestamp - $this->lastExecution[$commandKey];
if ($timeSinceLastExecution >= $interval) {
$this->displayInfo('[' . date('H:i:s') . '] Executing: ' . $command->getCommand() . ' (interval: ' . $interval . 's)');
$this->executeCommand($command, true);
$this->lastExecution[$commandKey] = $currentTimestamp;
}
}
}
/**
* Get the interval in seconds for a command
*
* @param mixed $command
* @return int|null
*/
protected function getCommandInterval($command): ?int
{
return $command->getSecondInterval();
}
/**
* Get a unique key for a command
*
* @param mixed $command
* @return string
*/
protected function getCommandKey($command): string
{
return md5($command->getCommand());
}
/**
* Execute a command with proper handling
*
* @param mixed $command
* @param bool $isSecondBased
* @return void
*/
protected function executeCommand($command, bool $isSecondBased = false): void
{
try {
$env = array_merge(getenv(), [
'APP_RUNNING_IN_CONSOLE' => true,
'APP_SCHEDULE_RUNNING' => true,
'APP_SECOND_SCHEDULE' => $isSecondBased ? 'true' : 'false'
]);
if (!$isSecondBased) {
$this->line('<comment>Running:</comment> ' . $command->getCommand());
}
if ($command->shouldRunInBackground()) {
$this->runInBackground($command, $env);
} else {
$this->runInForeground($command, $env);
}
} catch (\Exception $e) {
$this->displayError('Error executing command: ' . $e->getMessage());
}
}
/**
* Check if daemon is currently running
*
* @return bool
*/
protected function isDaemonRunning(): bool
{
$pidFile = $this->getDaemonPidFile();
if (!file_exists($pidFile)) {
return false;
}
$data = @json_decode(file_get_contents($pidFile), true);
$pid = $data['pid'] ?? 0;
if ($pid <= 0) {
return false;
}
// Check if process is actually running
if (function_exists('posix_kill')) {
return posix_kill($pid, 0);
}
// Fallback for systems without posix_kill
$output = shell_exec(sprintf("ps -p %d -o pid=", $pid));
return !empty(trim($output));
}
/**
* Write daemon PID file
*
* @return void
*/
protected function writeDaemonPid(): void
{
$pidFile = $this->getDaemonPidFile();
$dir = dirname($pidFile);
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
$data = [
'pid' => getmypid(),
'started_at' => date('Y-m-d H:i:s'),
'php_version' => PHP_VERSION,
'os' => PHP_OS
];
file_put_contents($pidFile, json_encode($data, JSON_PRETTY_PRINT), LOCK_EX);
}
/**
* Clean up daemon PID file
*
* @return void
*/
protected function cleanupDaemonPid(): void
{
$pidFile = $this->getDaemonPidFile();
if (file_exists($pidFile)) {
unlink($pidFile);
}
}
/**
* Get the daemon PID file path
*
* @return string
*/
protected function getDaemonPidFile(): string
{
return storage_path('schedule/cron_daemon.pid');
}
protected function runInBackground($command, $env): void
{
$phpBinary = PHP_BINARY;
$poolScript = 'pool';
$finishId = uniqid('cron_finish_', true);
if ($command->withoutOverlapping) {
$command->lock();
}
$logDir = storage_path('schedule');
if (!file_exists($logDir)) {
mkdir($logDir, 0755, true);
}
$logFile = $logDir . '/cron_' . md5($command->getCommand()) . '.log';
$lockFile = $command->getLockFile() . '.pid';
$lockDir = dirname($lockFile);
if (!file_exists($lockDir)) {
mkdir($lockDir, 0755, true);
}
$commandString = sprintf(
'(%s %s %s >> %s 2>&1 ; %s %s cron:finish %s %d $? >> %s 2>&1) & echo $!',
escapeshellarg($phpBinary),
escapeshellarg($poolScript),
escapeshellarg($command->getCommand()),
escapeshellarg($logFile),
escapeshellarg($phpBinary),
escapeshellarg($poolScript),
escapeshellarg($finishId),
$command->withoutOverlapping ? 1 : 0,
escapeshellarg($logFile)
);
$pid = (int) shell_exec($commandString);
if (empty($pid)) {
throw new \RuntimeException('Failed to start background process');
}
$processInfo = [
'pid' => $pid,
'finish_id' => $finishId,
'command' => $command->getCommand(),
'started_at' => date('Y-m-d H:i:s'),
'log_file' => $logFile,
'command_string' => $commandString,
'os' => PHP_OS
];
$maxRetries = 3;
$retryDelay = 100000;
for ($attempt = 1; $attempt <= $maxRetries; $attempt++) {
try {
$written = file_put_contents(
$lockFile,
json_encode($processInfo, JSON_PRETTY_PRINT),
LOCK_EX
);
if ($written !== false) {
break;
}
if ($attempt < $maxRetries) {
usleep($retryDelay);
}
} catch (\Exception $e) {
if ($attempt === $maxRetries) {
$this->displayError('Failed to write process info after ' . $maxRetries . ' attempts: ' . $e->getMessage());
return;
}
usleep($retryDelay);
}
}
file_put_contents(
$logFile,
sprintf(
"[%s] Process started (PID: %d)\nCommand: %s\nProcess Info: %s\n\n",
date('Y-m-d H:i:s'),
$pid,
$command->getCommand(),
json_encode($processInfo, JSON_PRETTY_PRINT)
),
FILE_APPEND
);
}
protected function runInForeground($command, $env): void
{
if ($command->withoutOverlapping) {
$command->lock();
}
$process = new Process(['php', 'pool', $command->getCommand()], base_path(), $env);
$process->setTimeout(null);
$process->run();
if ($process->isSuccessful()) {
// Only show success for non-second-based
if (!$command->isSecondSchedule()) {
$this->info('Success: ' . $command->getCommand());
}
} else {
$this->displayError('Error: ' . $command->getCommand());
$this->displayError('Output: ' . $process->getErrorOutput());
}
if ($command->withoutOverlapping) {
$command->releaseLock();
}
}
}