|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is licensed under MIT License. |
| 4 | + * |
| 5 | + * Copyright (c) 2026-present WebFiori Framework |
| 6 | + * |
| 7 | + * For more information on the license, please visit: |
| 8 | + * https://github.com/WebFiori/.github/blob/main/LICENSE |
| 9 | + * |
| 10 | + */ |
| 11 | +namespace WebFiori\Framework\Cli\Commands; |
| 12 | + |
| 13 | +use WebFiori\Cli\Argument; |
| 14 | +use WebFiori\Cli\Command; |
| 15 | +use WebFiori\Framework\Cli\CLIUtils; |
| 16 | +use WebFiori\Framework\Scheduler\TasksManager; |
| 17 | + |
| 18 | +/** |
| 19 | + * A CLI command that runs the scheduler in a loop for a configurable duration. |
| 20 | + * |
| 21 | + * This command is intended for local development and testing. It calls |
| 22 | + * the scheduler check every 60 seconds and self-terminates after the |
| 23 | + * specified maximum number of minutes. |
| 24 | + * |
| 25 | + * Usage: |
| 26 | + * php webfiori scheduler:daemon # runs for 60 minutes (default) |
| 27 | + * php webfiori scheduler:daemon --max-minutes=5 # runs for 5 minutes |
| 28 | + * |
| 29 | + * For production environments, use `scheduler:run` with OS-level cron instead. |
| 30 | + * |
| 31 | + * @author Ibrahim |
| 32 | + */ |
| 33 | +class SchedulerDaemonCommand extends Command { |
| 34 | + /** |
| 35 | + * Creates a new instance of the command. |
| 36 | + * |
| 37 | + * Registers the `--max-minutes` argument with a default of 60 and |
| 38 | + * optionally the scheduler password argument if a password is configured. |
| 39 | + */ |
| 40 | + public function __construct() { |
| 41 | + parent::__construct('scheduler:daemon', [ |
| 42 | + new Argument('--max-minutes', 'Maximum number of minutes to keep the daemon running. Default: 60.', true), |
| 43 | + new Argument('--show-log', 'If set, execution log will be shown after each run.', true), |
| 44 | + ], 'Run the scheduler in a loop for a limited duration.'); |
| 45 | + |
| 46 | + if (TasksManager::getPassword() != 'NO_PASSWORD') { |
| 47 | + $this->addArg('p', [ |
| 48 | + 'optional' => false, |
| 49 | + 'description' => 'Scheduler password.' |
| 50 | + ]); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Executes the daemon loop. |
| 56 | + * |
| 57 | + * The scheduler check is invoked every 60 seconds. The loop exits |
| 58 | + * when the elapsed time exceeds the value of `--max-minutes`. |
| 59 | + * |
| 60 | + * @return int 0 on success, -1 if the password is incorrect or no tasks exist. |
| 61 | + */ |
| 62 | + public function exec(): int { |
| 63 | + $count = count(TasksManager::getTasks()); |
| 64 | + |
| 65 | + if ($count == 0) { |
| 66 | + $this->info('There are no scheduled tasks.'); |
| 67 | + return -1; |
| 68 | + } |
| 69 | + |
| 70 | + $maxMinutes = $this->getArgValue('--max-minutes'); |
| 71 | + $maxMinutes = $maxMinutes !== null ? intval($maxMinutes) : 60; |
| 72 | + |
| 73 | + if ($maxMinutes <= 0) { |
| 74 | + $this->error('--max-minutes must be a positive integer.'); |
| 75 | + return -1; |
| 76 | + } |
| 77 | + |
| 78 | + $pass = CLIUtils::resolvePassword($this->getArgValue('p')) ?? ''; |
| 79 | + $maxSeconds = $maxMinutes * 60; |
| 80 | + $startTime = time(); |
| 81 | + |
| 82 | + $this->println("Scheduler daemon started. Will run for $maxMinutes minute(s)."); |
| 83 | + $this->println('Press Ctrl+C to stop.'); |
| 84 | + $this->println('---'); |
| 85 | + |
| 86 | + while ((time() - $startTime) < $maxSeconds) { |
| 87 | + $this->println('[' . date('Y-m-d H:i:s') . '] Running scheduler check...'); |
| 88 | + $result = TasksManager::run($pass, null, false, $this); |
| 89 | + |
| 90 | + if ($result == 'INV_PASS') { |
| 91 | + $this->error('Provided password is incorrect.'); |
| 92 | + return -1; |
| 93 | + } |
| 94 | + |
| 95 | + $this->println('Executed: ' . $result['executed-count'] . '/' . $result['total-tasks'] . ' tasks.'); |
| 96 | + |
| 97 | + $remaining = $maxSeconds - (time() - $startTime); |
| 98 | + |
| 99 | + if ($remaining <= 0) { |
| 100 | + break; |
| 101 | + } |
| 102 | + |
| 103 | + $sleepTime = min(60, $remaining); |
| 104 | + $this->println("Next check in $sleepTime second(s)..."); |
| 105 | + $this->println('---'); |
| 106 | + sleep($sleepTime); |
| 107 | + } |
| 108 | + |
| 109 | + $this->println('---'); |
| 110 | + $this->success('Daemon stopped after ' . $maxMinutes . ' minute(s).'); |
| 111 | + return 0; |
| 112 | + } |
| 113 | +} |
0 commit comments