setName('schedule') ->setDescription('Run the schedule') ->setHelp('This command runs the schedule, checking if any tasks are due and executing them.'); #->addOption('quiet', null, InputOption::VALUE_NONE, 'Do not output any status messages.'); } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->io->title("UserFrosting's Scheduler"); // Prepare task locator $scheduler = $this->ci->scheduler; $this->io->writeln('Checking for tasks to run...'); $tasks = []; // Find which tasks are due foreach ($scheduler->getTasks() as $task) { if ($task['instance']->isDue()) { $tasks[] = $task; } } if (count($tasks) == 0) { $this->io->success('Nothing to do'); return self::SUCCESS; } $this->io->writeln('Found ' . count($tasks) . ' task(s) to run'); $this->io->writeln(''); $this->io->writeln('Running tasks...'); $hasFailure = false; // Run tasks foreach ($tasks as $task) { // Display the class we are going to use as info $this->io->write('Running task `' . $task['class'] . '`...'); $start = hrtime(true); try { if (!$task['instance']->run()) { throw new Exception('Task returned failure'); } $end = hrtime(true); $tdiff = round(($end-$start) / 1e+6); $tunit = 'ms'; if ($tdiff > 1000) { $tdiff = round($tdiff / 1000); $tunit = 's'; } $this->io->writeln($tdiff . $tunit); } catch (\Exception $e) { $hasFailure = true; $this->io->writeln(' [ERROR] ' . $e->getMessage() . ' '); } } // Success if (!$hasFailure) { $this->io->success('Schedule success !'); return self::SUCCESS; } else { $this->io->error('Schedule failed !'); return self::FAILURE; } } }