forked from kriswallsmith/spork
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix erroneous exit code not propagating for BatchJob
- Loading branch information
Showing
4 changed files
with
132 additions
and
1 deletion.
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
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 |
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,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); | ||
} | ||
} |
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,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.
Sorry, something went wrong. |
||
|
||
$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; | ||
}); | ||
} |
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
YAY! SergioRunner! 👍