Skip to content

Commit

Permalink
Fix erroneous exit code not propagating for BatchJob
Browse files Browse the repository at this point in the history
  • Loading branch information
taueres committed Sep 9, 2015
1 parent 530fcf5 commit 748dabd
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
47 changes: 47 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions sergio/SergioRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

class SergioRunner
{
private $output;
private $erroneousProcesses;
private $processId;

public function __construct($output, array $erroneousProcesses)
{
$this->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);
}
}
41 changes: 41 additions & 0 deletions sergio/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

main();

function main() {
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/SergioRunner.php';

$output = fopen('php://stdout', 'w+');

$failingProcesses = [
1 => false,
2 => false,
3 => true,
4 => false,
5 => false,
6 => false,
7 => false,
];

$runner = new SergioRunner($output, $failingProcesses);

This comment has been minimized.

Copy link
@fntlnz

fntlnz Sep 10, 2015

YAY! SergioRunner! 👍


$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;
});
}
7 changes: 6 additions & 1 deletion src/Spork/Batch/BatchJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 748dabd

Please sign in to comment.