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-28 16:25:34 +02:00
|
|
|
use App\Entity\Job;
|
|
|
|
use Doctrine\Persistence\ManagerRegistry;
|
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\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;
|
2022-04-28 16:25:34 +02:00
|
|
|
protected $doctrine;
|
2021-05-24 18:36:16 +02:00
|
|
|
|
2022-04-28 16:25:34 +02:00
|
|
|
public function __construct(KernelInterface $kernel, ManagerRegistry $doctrine)
|
2021-05-24 18:36:16 +02:00
|
|
|
{
|
2021-07-15 12:41:34 +02:00
|
|
|
$this->kernel = $kernel;
|
2022-04-28 16:25:34 +02:00
|
|
|
$this->doctrine = $doctrine;
|
2021-07-15 12:41:34 +02:00
|
|
|
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-10-18 15:17:36 +02:00
|
|
|
|
2021-07-15 12:41:34 +02:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2022-04-28 16:25:34 +02:00
|
|
|
$jobRepo = $this->doctrine->getRepository(Job::class);
|
2021-07-15 12:41:34 +02:00
|
|
|
$jobId = (int)$input->getArgument('jobid');
|
2022-05-19 11:56:06 +02:00
|
|
|
$job = $jobRepo->find($jobId);
|
|
|
|
if($job === NULL) {
|
|
|
|
$output->writeln('Job does not exist');
|
|
|
|
return Command::FAILURE;
|
|
|
|
}
|
|
|
|
$jobRunning = $jobRepo->isLockedJob($job);
|
2021-07-15 13:22:52 +02:00
|
|
|
if($jobRunning) {
|
2022-05-13 15:24:05 +02:00
|
|
|
$output->writeln('Job is already running');
|
2021-07-15 13:22:52 +02:00
|
|
|
return Command::FAILURE;
|
|
|
|
}
|
2022-05-19 11:56:06 +02:00
|
|
|
$jobRepo->setJobRunning($job, true);
|
|
|
|
$jobRepo->setTempVar($job, 'consolerun', true);
|
|
|
|
$result = $jobRepo->runNow($job, true);
|
|
|
|
if($job->getData('crontype') == 'reboot') {
|
2021-07-15 13:14:36 +02:00
|
|
|
$sleeping = true;
|
|
|
|
while($sleeping) {
|
2022-05-19 11:56:06 +02:00
|
|
|
if(time() >= $job->getRunning()) $sleeping = false;
|
2021-07-15 13:14:36 +02:00
|
|
|
sleep(1);
|
|
|
|
}
|
2022-05-19 11:56:06 +02:00
|
|
|
$result = $jobRepo->runNow($job, true);
|
2021-07-15 13:14:36 +02:00
|
|
|
}
|
2022-05-19 11:56:06 +02:00
|
|
|
$jobRepo->setJobRunning($job, false);
|
|
|
|
$jobRepo->setTempVar($job, 'consolerun', false);
|
2021-07-15 12:41:34 +02:00
|
|
|
$output->write($result['output']);
|
|
|
|
if($result['success']) {
|
2022-04-28 16:25:34 +02:00
|
|
|
$output->writeln('Job 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-28 16:25:34 +02:00
|
|
|
$output->writeln('Job 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
|
|
|
}
|
|
|
|
}
|