|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Services\Core\Commands; |
| 4 | + |
| 5 | +use Services\Core\Hm_Container; |
| 6 | +use Symfony\Component\Console\Command\Command; |
| 7 | +use Symfony\Component\Console\Input\InputInterface; |
| 8 | +use Symfony\Component\Console\Output\OutputInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class Hm_SchedulerWorkCommand |
| 12 | + * @package Services\Core\Commands |
| 13 | + */ |
| 14 | +class Hm_SchedulerWorkCommand extends Hm_BaseCommand |
| 15 | +{ |
| 16 | + /** |
| 17 | + * The name of the command. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected static $defaultName = 'schedule:work'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Flag to indicate if the scheduler should stop running. |
| 25 | + * @var bool |
| 26 | + */ |
| 27 | + private $shouldStop = false; |
| 28 | + |
| 29 | + /** |
| 30 | + * Store the last run time for each task to prevent overlapping runs. |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + private $lastRunTimes = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * Configure the command. |
| 37 | + */ |
| 38 | + protected function configure() |
| 39 | + { |
| 40 | + $this |
| 41 | + ->setDescription('Continuously run the scheduler to execute due tasks') |
| 42 | + ->setHelp('This command runs the scheduler in a loop to continuously check and execute scheduled tasks.'); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Execute the console command. |
| 47 | + * |
| 48 | + * @param InputInterface $input |
| 49 | + * @param OutputInterface $output |
| 50 | + * @return int Command exit code. |
| 51 | + */ |
| 52 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 53 | + { |
| 54 | + $scheduler = Hm_Container::getContainer()->get('scheduler'); |
| 55 | + $output->writeln("Scheduler started. Press Ctrl+C to stop."); |
| 56 | + |
| 57 | + if (function_exists('pcntl_signal')) { |
| 58 | + pcntl_signal(SIGINT, function () { |
| 59 | + $this->shouldStop = true; |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + while (!$this->shouldStop) { |
| 64 | + foreach ($scheduler->getTasks() as $task) { |
| 65 | + $taskId = spl_object_hash($task); |
| 66 | + $currentTime = new \DateTime('now', new \DateTimeZone($task->getTimezone())); |
| 67 | + |
| 68 | + $lastRunTime = isset($this->lastRunTimes[$taskId]) ? $this->lastRunTimes[$taskId] : $currentTime; |
| 69 | + |
| 70 | + $this->lastRunTimes[$taskId] = $currentTime; |
| 71 | + |
| 72 | + if ($task->isDue() && $currentTime > $lastRunTime) { |
| 73 | + $output->writeln("Running task: {$task->getName()} at " . $currentTime->format('Y-m-d H:i:s')); |
| 74 | + $task->run(); |
| 75 | + $output->writeln("Task: {$task->getName()} added to queue"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Wait one minute before the next loop iteration |
| 80 | + sleep(60); |
| 81 | + |
| 82 | + // Dispatch any pending signals |
| 83 | + if (function_exists('pcntl_signal_dispatch')) { |
| 84 | + pcntl_signal_dispatch(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + $output->writeln("Scheduler stopped gracefully."); |
| 89 | + |
| 90 | + return Command::SUCCESS; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Stops the scheduler loop gracefully. |
| 95 | + */ |
| 96 | + public function stop() |
| 97 | + { |
| 98 | + $this->shouldStop = true; |
| 99 | + } |
| 100 | +} |
0 commit comments