115 lines
3.2 KiB
PHP
115 lines
3.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* AVSDev UF Scheduler (https://avsdev.uk)
|
|
*
|
|
* @link https://git.avsdev.uk/avsdev/sprinkle-scheduler
|
|
* @license https://git.avsdev.uk/avsdev/sprinkle-scheduler/blob/master/LICENSE.md (LGPL-3.0 License)
|
|
*/
|
|
|
|
namespace UserFrosting\Sprinkle\Scheduler\Bakery;
|
|
|
|
use Exception;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use UserFrosting\Sprinkle\Core\Bakery\Helper\ConfirmableTrait;
|
|
use UserFrosting\Sprinkle\Core\Database\Tasker\Tasker;
|
|
use UserFrosting\System\Bakery\BaseCommand;
|
|
|
|
/**
|
|
* scheduler Bakery Command
|
|
* Run a scheduled task.
|
|
*
|
|
* @author Craig Williams (https://avsdev.uk)
|
|
*/
|
|
class ScheduleCommand extends BaseCommand
|
|
{
|
|
use ConfirmableTrait;
|
|
|
|
/**
|
|
* @var Tasker
|
|
*/
|
|
protected $scheduler;
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
protected function configure()
|
|
{
|
|
$this->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('<comment>Checking for tasks to run...</comment>');
|
|
|
|
$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('<info>Found ' . count($tasks) . ' task(s) to run</>');
|
|
$this->io->writeln('');
|
|
$this->io->writeln('<comment>Running tasks...</comment>');
|
|
|
|
$hasFailure = false;
|
|
|
|
// Run tasks
|
|
foreach ($tasks as $task) {
|
|
// Display the class we are going to use as info
|
|
$this->io->write('<info>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> [ERROR] ' . $e->getMessage() . ' </>');
|
|
}
|
|
}
|
|
|
|
// Success
|
|
if (!$hasFailure) {
|
|
$this->io->success('Schedule success !');
|
|
return self::SUCCESS;
|
|
} else {
|
|
$this->io->error('Schedule failed !');
|
|
return self::FAILURE;
|
|
}
|
|
}
|
|
}
|