NEW FEATURE: running from console
This commit is contained in:
parent
5901073ca1
commit
6333f20a5b
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
2
webcron
2
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();
|
||||
|
|
Loading…
Reference in New Issue