webcron/src/Command/RunCommand.php

66 lines
2.4 KiB
PHP
Raw Normal View History

2021-05-24 18:36:16 +02:00
<?php
2022-04-27 14:24:48 +02:00
namespace App\Command;
2021-05-24 18:36:16 +02:00
2022-04-27 14:24:48 +02:00
use App\Repository\JobRepository;
use App\Repository\RunRepository;
2021-05-24 18:36:16 +02:00
use Symfony\Component\Console\Command\Command;
2021-07-15 12:41:34 +02:00
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
2022-04-27 14:24:48 +02:00
use Symfony\Component\HttpKernel\KernelInterface;
2021-05-24 18:36:16 +02:00
class RunCommand extends Command
{
protected static $defaultName = 'run';
2021-07-15 12:41:34 +02:00
protected $kernel;
2021-05-24 18:36:16 +02:00
2022-04-27 14:24:48 +02:00
public function __construct(KernelInterface $kernel)
2021-05-24 18:36:16 +02:00
{
2021-07-15 12:41:34 +02:00
$this->kernel = $kernel;
parent::__construct();
}
2021-05-24 18:36:16 +02:00
2021-07-15 12:41:34 +02:00
protected function configure()
{
$this
2022-04-27 14:24:48 +02:00
->setDescription('RunRepository a single cronjob')
2021-07-15 12:41:34 +02:00
->setHelp('This command runs a single command')
->addArgument('jobid', InputArgument::REQUIRED, 'The id of the job to be run');
}
2021-07-15 12:41:34 +02:00
protected function execute(InputInterface $input, OutputInterface $output)
{
2022-04-27 14:24:48 +02:00
$runRepo = $this->getEntityManager()->getRepository(Run::class);
2021-07-15 12:41:34 +02:00
$jobId = (int)$input->getArgument('jobid');
2021-07-15 13:22:52 +02:00
$jobRunning = $jobRepo->isLockedJob($jobId);
if($jobRunning) {
2022-04-27 14:24:48 +02:00
$output->writeln('JobRepository is already running');
2021-07-15 13:22:52 +02:00
return Command::FAILURE;
}
2021-07-15 12:41:34 +02:00
$jobRepo->setJobRunning($jobId, true);
$jobRepo->setTempVar($jobId, 'consolerun', true);
2021-07-15 12:41:34 +02:00
$result = $jobRepo->runNow($jobId, true);
$job = $jobRepo->getJob($jobId);
if($job['data']['crontype'] == 'reboot') {
$sleeping = true;
while($sleeping) {
$job = $jobRepo->getJob($jobId);
if(time() >= $job['running']) $sleeping = false;
sleep(1);
}
$result = $jobRepo->runNow($jobId, true);
}
2021-07-15 12:41:34 +02:00
$jobRepo->setJobRunning($jobId, false);
$jobRepo->setTempVar($jobId, 'consolerun', false);
2021-07-15 12:41:34 +02:00
$output->write($result['output']);
if($result['success']) {
2022-04-27 14:24:48 +02:00
$output->writeln('JobRepository succeeded with in ' . number_format($result['runtime'], 3) . 'secs with exitcode ' . $result['exitcode']);
2021-07-15 12:41:34 +02:00
return Command::SUCCESS;
} else {
2022-04-27 14:24:48 +02:00
$output->writeln('JobRepository failed in ' . number_format($result['runtime'], 3) . 'secs with exitcode ' . $result['exitcode']);
2021-07-15 12:41:34 +02:00
return Command::FAILURE;
}
2021-05-24 18:36:16 +02:00
}
}