From 6333f20a5b8619990b4d6e42f8e0479a36840c8a Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Thu, 15 Jul 2021 12:41:34 +0200 Subject: [PATCH] NEW FEATURE: running from console --- src/Command/RunCommand.php | 36 +++++++++++++++++++++++++++++++++++- src/Repository/Job.php | 12 ++++++------ webcron | 2 +- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 9d77929..6d1e46d 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -2,14 +2,48 @@ namespace JeroenED\Webcron\Command; +use JeroenED\Framework\Kernel; +use JeroenED\Framework\Repository; +use JeroenED\Webcron\Repository\Job; +use JeroenED\Webcron\Repository\Run; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; class RunCommand extends Command { protected static $defaultName = 'run'; + protected $kernel; + + public function __construct(Kernel $kernel) + { + $this->kernel = $kernel; + parent::__construct(); + } protected function configure() { - + $this + ->setDescription('Run a single cronjob') + ->setHelp('This command runs a single command') + ->addArgument('jobid', InputArgument::REQUIRED, 'The id of the job to be run'); + } + protected function execute(InputInterface $input, OutputInterface $output) + { + $jobRepo = new Job($this->kernel->getDbCon()); + $jobId = (int)$input->getArgument('jobid'); + $jobRepo->setJobRunning($jobId, true); + $result = $jobRepo->runNow($jobId, true); + $jobRepo->setJobRunning($jobId, false); + $output->write($result['output']); + if($result['success']) { + $output->writeln('Job succeeded with in ' . number_format($result['runtime'], 3) . 'secs exitcode ' . $result['exitcode']); + return Command::SUCCESS; + } else { + $output->writeln('Job failed in ' . number_format($result['runtime'], 3) . 'secs with exitcode ' . $result['exitcode']); + return Command::FAILURE; + } } } \ No newline at end of file diff --git a/src/Repository/Job.php b/src/Repository/Job.php index 44df682..19bf49e 100644 --- a/src/Repository/Job.php +++ b/src/Repository/Job.php @@ -234,20 +234,19 @@ class Job extends Repository } } - public function runNow($job) { + public function runNow($job, $console = false) { $job = $this->getJob($job, true); $runRepo = new Run($this->dbcon); - if($runRepo->isSlowJob($job['id']) || $job['data']['crontype'] === 'reboot') { + if($console == false && ($runRepo->isSlowJob($job['id']) || $job['data']['crontype'] === 'reboot')) { $jobsSql = "UPDATE job SET running = :status WHERE id = :id AND running IN (0,1,2)"; $jobsStmt = $this->dbcon->prepare($jobsSql); $jobsStmt->executeQuery([':id' => $job['id'], ':status' => 2]); return ['success' => true, 'status' => 'deferred', 'title' => 'Cronjob has been scheduled', 'message' => 'Job was scheduled to be run. You will find the output soon in the job details']; } else { - $this->runJob($job['id'], true); - $output = $runRepo->getLastRun($job['id']); + $output = $this->runJob($job['id'], true); return [ 'status' => 'ran', - 'output' => $output['output'], + 'output' => ($console) ? $output['output'] : htmlentities($output['output']), 'exitcode' => $output['exitcode'], 'runtime' => (float)$output['runtime'], 'title' => !str_contains($output['flags'], Run::FAILED) ? 'Cronjob successfully ran' : 'Cronjob failed. Please check output below', @@ -265,7 +264,7 @@ class Job extends Repository return $prepend . $command; } - public function runJob(int $job, bool $manual): void + public function runJob(int $job, bool $manual): array { $starttime = microtime(true); $job = $this->getJob($job, true); @@ -305,6 +304,7 @@ class Job extends Repository $addRunStmt = $this->dbcon->prepare($addRunSql); $addRunStmt->executeQuery([':id' => $job['id'], ':nextrun' => $nextrun]); } + return ['job_id' => $job['id'], 'exitcode' => $result['exitcode'], 'timestamp' =>floor($starttime), 'runtime' => $runtime, 'output' => (string)$result['output'], 'flags' => implode("", $flags)]; } public function unlockJob(int $id = 0): void diff --git a/webcron b/webcron index 398d55f..17e6620 100644 --- a/webcron +++ b/webcron @@ -16,7 +16,7 @@ $kernel->setConfigDir(getcwd() . '/config/'); $kernel->setTemplateDir(getcwd() . '/templates/'); $kernel->parseDotEnv($kernel->getProjectDir() . '/.env'); -$application->add(new RunCommand()); +$application->add(new RunCommand($kernel)); $application->add(new DaemonCommand($kernel)); $application->run();