Basic scheduler completed
This commit is contained in:
139
src/Bakery/ScheduleCommand.php
Normal file
139
src/Bakery/ScheduleCommand.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?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)
|
||||
{
|
||||
// Get options
|
||||
$quiet = $input->getOption('quiet');
|
||||
|
||||
if (!$quiet) {
|
||||
$this->io->title("UserFrosting's Scheduler");
|
||||
}
|
||||
|
||||
// Prepare task locator
|
||||
$scheduler = $this->ci->scheduler;
|
||||
|
||||
|
||||
if (!$quiet) {
|
||||
$this->io->writeln('<comment>Checking for tasks to run...</comment>');
|
||||
}
|
||||
|
||||
$tasks = [];
|
||||
|
||||
// Start by getting tasks
|
||||
foreach ($scheduler->getTasks() as $task) {
|
||||
if ($task['instance']->isDue()) {
|
||||
// Add task class to list
|
||||
$tasks[] = $task;
|
||||
}
|
||||
}
|
||||
|
||||
if (count($tasks) == 0) {
|
||||
if (!$quiet) {
|
||||
$this->io->success('Nothing to do');
|
||||
}
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
if (!$quiet) {
|
||||
$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 (!($taskSuccess = $task['instance']->run())) {
|
||||
throw new Exception('Task returned failure');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$taskSuccess = false;
|
||||
$hasFailure = true;
|
||||
if ($quiet) {
|
||||
$this->io->write('<info>Running task `' . $task['class'] . '`...</>');
|
||||
}
|
||||
$this->io->error('<error> [ERROR] ' . $e->getMessage() . ' </>');
|
||||
}
|
||||
|
||||
if (!$quiet && $taskSuccess) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
// Success
|
||||
if (!$hasFailure) {
|
||||
if (!$quiet) {
|
||||
$this->io->success('Schedule success !');
|
||||
}
|
||||
return self::SUCCESS;
|
||||
} else {
|
||||
if (!$quiet) {
|
||||
$this->io->error('Schedule failed !');
|
||||
}
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user