Skip to content

Commit

Permalink
refactor: clean JobCommand before implementing new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidmattei committed Jul 11, 2024
1 parent 33ee4e6 commit dfb960b
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions EMS/core-bundle/src/Command/JobCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace EMS\CoreBundle\Command;

use EMS\CommonBundle\Common\Command\AbstractCommand;
Expand All @@ -11,10 +13,12 @@

class JobCommand extends AbstractCommand
{
protected static $defaultName = 'ems:job:run';

private const OPTION_DUMP = 'dump';
private const OPTION_TAG = 'tag';
protected static $defaultName = 'ems:job:run';
private const USER_JOB_COMMAND = 'User-Job-Command';

private bool $dump = false;
private ?string $tag = null;

Expand All @@ -28,19 +32,11 @@ public function __construct(

protected function configure(): void
{
$this->setDescription('Execute the next pending job if exists. If not execute the oldest due scheduled job if exists.')
->addOption(
self::OPTION_DUMP,
null,
InputOption::VALUE_NONE,
'Shows the job\'s output at the end of the execution'
)
->addOption(
self::OPTION_TAG,
null,
InputOption::VALUE_OPTIONAL,
'Will treat the next scheduled job flagged with the provided tag (don\'t execute pending jobs)'
);
$this
->setDescription('Execute the next pending job if exists. If not execute the oldest due scheduled job if exists.')
->addOption(self::OPTION_DUMP, null, InputOption::VALUE_NONE, 'Shows the job\'s output at the end of the execution')
->addOption(self::OPTION_TAG, null, InputOption::VALUE_OPTIONAL, 'Will treat the next scheduled job flagged with the provided tag (do not execute pending jobs)')
;
}

protected function initialize(InputInterface $input, OutputInterface $output): void
Expand All @@ -57,24 +53,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$job = $this->jobService->nextJob($this->tag);

if (null === $job) {
$this->io->comment('No pending job to treat. Looking for due scheduled job.');
$this->io->comment('None pending job to treat. Looking for due scheduled job.');
$job = $this->jobService->nextJobScheduled(self::USER_JOB_COMMAND, $this->tag);
}

if (null === $job) {
$this->io->comment('Nothing to run. Cleaning jobs.');
$this->cleanJobs();
$this->jobService->cleanJob(self::USER_JOB_COMMAND, $this->cleanJobsTimeString);

return self::EXECUTE_SUCCESS;
}

return $this->runJob($job, $input, $output);
return $this->runJob($job, $output);
}

/**
* @throws \Throwable
*/
protected function runJob(Job $job, InputInterface $input, OutputInterface $output): int
private function runJob(Job $job, OutputInterface $output): int
{
$this->io->title('Preparing the job');
$this->io->listing([
Expand Down Expand Up @@ -109,9 +102,4 @@ protected function runJob(Job $job, InputInterface $input, OutputInterface $outp

return parent::EXECUTE_SUCCESS;
}

private function cleanJobs(): void
{
$this->jobService->cleanJob(self::USER_JOB_COMMAND, $this->cleanJobsTimeString);
}
}

0 comments on commit dfb960b

Please sign in to comment.