From 575a6d3d286dbec7dba29104911ef1420e1ecd25 Mon Sep 17 00:00:00 2001 From: clementzarch Date: Fri, 8 Sep 2023 17:11:59 +0200 Subject: [PATCH] fix: instead of `loadAction` directly returning the factory (which incorrectly runs it immediately), put the action in an ActionProxy to run it later, in `run`, along with the other jobs --- src/ActionProxy.php | 42 ++++++++++++++++++++++++++++++++ src/Console.php | 7 +++--- src/WorkflowRuntimeInterface.php | 4 +-- 3 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 src/ActionProxy.php diff --git a/src/ActionProxy.php b/src/ActionProxy.php new file mode 100644 index 0000000..96348ef --- /dev/null +++ b/src/ActionProxy.php @@ -0,0 +1,42 @@ + */ + private array $queuedCalls = []; + + public function __construct( + callable $factory, + private readonly ConsoleOutput $output, + private readonly ExecutingActionInterface $action, + private readonly State\StateOutput\Workflow $state, + private readonly string $filename, + ) { + $this->queuedCalls[] = static function (ActionConsoleRuntime $runtime) use ($factory): void { + $factory($runtime); + }; + } + + public function run(int $interval = 1000): int + { + $runtime = new ActionConsoleRuntime($this->output, $this->action, $this->state->withAction($this->filename)); + + foreach ($this->queuedCalls as $queuedCall) { + $queuedCall($runtime); + } + + $this->queuedCalls = []; + + return $runtime->run($interval); + } +} diff --git a/src/Console.php b/src/Console.php index f4d4ff6..9b9a8fd 100644 --- a/src/Console.php +++ b/src/Console.php @@ -6,11 +6,10 @@ use Kiboko\Component\Action\Action; use Kiboko\Component\Pipeline\Pipeline; -use Kiboko\Component\Runtime\Action\ActionRuntimeInterface; -use Kiboko\Component\Runtime\Action\Console as ActionConsoleRuntime; use Kiboko\Component\Runtime\Pipeline\PipelineRuntimeInterface; use Kiboko\Component\State; use Kiboko\Contract\Pipeline\PipelineRunnerInterface; +use Kiboko\Contract\Satellite\RunnableInterface; use Kiboko\Contract\Satellite\RunnableInterface as JobRunnableInterface; use Symfony\Component\Console\Output\ConsoleOutput; @@ -37,13 +36,13 @@ public function loadPipeline(string $filename): PipelineRuntimeInterface return new PipelineProxy($factory, $this->output, $pipeline, $this->state, basename($filename)); } - public function loadAction(string $filename): ActionRuntimeInterface + public function loadAction(string $filename): RunnableInterface { $factory = require $filename; $action = new Action(); - return $factory(new ActionConsoleRuntime($this->output, $action, $this->state->withAction(basename($filename)))); + return new ActionProxy($factory, $this->output, $action, $this->state, basename($filename)); } public function job(JobRunnableInterface $job): self diff --git a/src/WorkflowRuntimeInterface.php b/src/WorkflowRuntimeInterface.php index 9c6fcd2..f55c635 100644 --- a/src/WorkflowRuntimeInterface.php +++ b/src/WorkflowRuntimeInterface.php @@ -7,6 +7,4 @@ use Kiboko\Contract\Pipeline\SchedulingInterface; use Kiboko\Contract\Satellite\RunnableInterface; -interface WorkflowRuntimeInterface extends SchedulingInterface, RunnableInterface -{ -} +interface WorkflowRuntimeInterface extends SchedulingInterface, RunnableInterface {}