-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
493 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\Scheduler\Job; | ||
|
||
use Orisai\Exceptions\Logic\InvalidState; | ||
use Orisai\Exceptions\Message; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\ArrayInput; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
use Throwable; | ||
use function array_merge; | ||
use function assert; | ||
use function is_numeric; | ||
|
||
final class SymfonyCommandJob implements Job | ||
{ | ||
|
||
private Command $command; | ||
|
||
private Application $application; | ||
|
||
/** @var array<int|string, mixed> */ | ||
private array $parameters = []; | ||
|
||
public function __construct(Command $command, Application $application) | ||
{ | ||
$this->command = $command; | ||
$this->application = $application; | ||
} | ||
|
||
/** | ||
* @param array<int|string, mixed> $parameters | ||
*/ | ||
public function setCommandParameters(array $parameters): void | ||
{ | ||
$this->parameters = $parameters; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
$name = $this->command->getName(); | ||
assert($name !== null); // It must be set in constructor | ||
|
||
return 'symfony/console: ' . $this->createInput(); | ||
} | ||
|
||
public function run(JobLock $lock): void | ||
{ | ||
$input = $this->createInput(); | ||
$output = new BufferedOutput(); | ||
|
||
try { | ||
// Using doRun() to prevent auto-exiting and error-handling | ||
$exitCode = $this->application->doRun($input, $output); | ||
} catch (Throwable $commandException) { | ||
$exitCode = $this->getExceptionCode($commandException); | ||
} | ||
|
||
$outputString = $output->fetch(); | ||
|
||
if ($exitCode !== 0) { | ||
$message = Message::create() | ||
->withContext("Running command '{$this->command->getName()}'.") | ||
->withProblem("Run failed with code '$exitCode'."); | ||
|
||
if ($outputString !== '') { | ||
$message->with('Output', $outputString); | ||
} | ||
|
||
$exception = InvalidState::create() | ||
->withCode($exitCode) | ||
->withMessage($message); | ||
|
||
if (isset($commandException)) { | ||
$exception->withPrevious($commandException) | ||
->withSuppressed([$commandException]); | ||
} | ||
|
||
throw $exception; | ||
} | ||
} | ||
|
||
private function createInput(): ArrayInput | ||
{ | ||
return new ArrayInput(array_merge( | ||
[$this->command->getName()], | ||
$this->parameters, | ||
)); | ||
} | ||
|
||
private function getExceptionCode(Throwable $exception): int | ||
{ | ||
$exitCode = $exception->getCode(); | ||
|
||
if (is_numeric($exitCode)) { | ||
$exitCode = (int) $exitCode; | ||
if ($exitCode <= 0) { | ||
$exitCode = 1; | ||
} | ||
|
||
return $exitCode; | ||
} | ||
|
||
/** @codeCoverageIgnore Hard to simulate, only php extensions can return non-int code */ | ||
return 1; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Orisai\Scheduler\Doubles; | ||
|
||
use Orisai\Exceptions\Logic\NotImplemented; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class TestExceptionCommand extends Command | ||
{ | ||
|
||
private int $code; | ||
|
||
public function __construct(int $code) | ||
{ | ||
parent::__construct(); | ||
$this->code = $code; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('test:exception'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$output->writeln('Failure!'); | ||
|
||
throw NotImplemented::create() | ||
->withMessage('Message') | ||
->withCode($this->code); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Orisai\Scheduler\Doubles; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class TestFailNoOutputCommand extends Command | ||
{ | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('test:fail-no-output'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
return 1; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Orisai\Scheduler\Doubles; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class TestFailOutputCommand extends Command | ||
{ | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('test:fail-output'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$output->writeln('Failure!'); | ||
$output->writeln('New line!'); | ||
|
||
return 256; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Orisai\Scheduler\Doubles; | ||
|
||
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; | ||
|
||
final class TestParametrizedCommand extends Command | ||
{ | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('test:parameters'); | ||
$this->addArgument('argument', InputArgument::REQUIRED); | ||
$this->addOption('option', null, InputOption::VALUE_REQUIRED); | ||
$this->addOption('bool-option', null, InputOption::VALUE_NONE); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Orisai\Scheduler\Doubles; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class TestSuccessCommand extends Command | ||
{ | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('test:success'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$output->writeln('Success!'); | ||
|
||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.