From 748dabd4485ad5c90c51c146f2dc33e7789d3f91 Mon Sep 17 00:00:00 2001 From: Sergio Santoro Date: Thu, 10 Sep 2015 00:03:11 +0200 Subject: [PATCH] Fix erroneous exit code not propagating for BatchJob --- Dockerfile | 47 ++++++++++++++++++++++++++++++++++++ sergio/SergioRunner.php | 38 +++++++++++++++++++++++++++++ sergio/main.php | 41 +++++++++++++++++++++++++++++++ src/Spork/Batch/BatchJob.php | 7 +++++- 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100644 sergio/SergioRunner.php create mode 100644 sergio/main.php diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8781d6d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,47 @@ +FROM php:cli + +## SYSTEM ## + +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + git \ + vim \ + wget \ + && rm -rf /var/lib/apt/lists/* + +## PHP ## + +# create php.ini +RUN echo "short_open_tag = Off" >> /usr/local/etc/php/php.ini +RUN echo "date.timezone = Europe/Rome" >> /usr/local/etc/php/php.ini + +# install basic extensions +RUN docker-php-ext-install opcache +RUN docker-php-ext-install mbstring + +# install zip extension (required by Composer) +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + zlib1g-dev \ + && rm -rf /var/lib/apt/lists/* +RUN docker-php-ext-install zip + +# install intl extension +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + libicu-dev \ + g++ \ + && rm -rf /var/lib/apt/lists/* +RUN docker-php-ext-install intl + +# install Composer +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/bin --filename=composer + +# install PhpUnit +RUN wget https://phar.phpunit.de/phpunit.phar \ + && chmod +x phpunit.phar \ + && mv phpunit.phar /usr/local/bin/phpunit + +# install modules for Spork +docker-php-ext-install pcntl +docker-php-ext-install shmop diff --git a/sergio/SergioRunner.php b/sergio/SergioRunner.php new file mode 100644 index 0000000..b71e7d8 --- /dev/null +++ b/sergio/SergioRunner.php @@ -0,0 +1,38 @@ +output = $output; + $this->erroneousProcesses = $erroneousProcesses; + } + + public function setProcessId($processId) + { + $this->processId = $processId; + } + + public function run($processId) + { + $this->write("Starting subprocess..."); + + sleep(rand(1, 5)); + + if ($this->erroneousProcesses[$processId]) { + $this->write("Error!"); + throw new \LogicException('Just a message'); + } + + $this->write("Done"); + } + + private function write($msg) + { + fwrite($this->output, "<$this->processId> " . $msg . PHP_EOL); + } +} diff --git a/sergio/main.php b/sergio/main.php new file mode 100644 index 0000000..119f135 --- /dev/null +++ b/sergio/main.php @@ -0,0 +1,41 @@ + false, + 2 => false, + 3 => true, + 4 => false, + 5 => false, + 6 => false, + 7 => false, + ]; + + $runner = new SergioRunner($output, $failingProcesses); + + $manager = new \Spork\ProcessManager(); + $batch = $manager->createBatchJob(array_keys($failingProcesses)); + + /** @var \Spork\Fork $promise */ + $promise = $batch->execute(function ($pId) use ($runner) { + $runner->setProcessId($pId); + $runner->run($pId); + }); + + echo 'Main process waiting...' . PHP_EOL; + $promise->wait(); + + $promise->done(function () { + echo 'Promise Success!' . PHP_EOL; + }); + $promise->fail(function () { + echo 'Promise Failed!' . PHP_EOL; + }); +} diff --git a/src/Spork/Batch/BatchJob.php b/src/Spork/Batch/BatchJob.php index f783a96..dddcd15 100644 --- a/src/Spork/Batch/BatchJob.php +++ b/src/Spork/Batch/BatchJob.php @@ -93,7 +93,12 @@ public function __invoke() $results = array(); foreach ($forks as $fork) { - $results = array_merge($results, $fork->getResult()); + $exitStatus = $fork->getExitStatus(); + if (0 !== $exitStatus) { + // Propagate erroneous state + die($exitStatus); + } + $results = array_merge($results, (array) $fork->getResult()); } return $results;