From 27d0cc46942b6de838133d34531d9dd432d9fa29 Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sun, 9 Feb 2020 12:58:50 +0100 Subject: [PATCH 1/3] Add namespace prefix and improve README.md --- README.md | 106 +++++++++++------- composer.json | 4 +- phpunit.xml.dist | 2 +- src/Batch/BatchJob.php | 10 +- src/Batch/BatchRunner.php | 6 +- src/Batch/Strategy/AbstractStrategy.php | 4 +- src/Batch/Strategy/CallbackStrategy.php | 4 +- src/Batch/Strategy/ChunkStrategy.php | 4 +- src/Batch/Strategy/StrategyInterface.php | 2 +- src/Batch/Strategy/ThrottleStrategy.php | 4 +- src/Deferred/Deferred.php | 4 +- src/Deferred/DeferredAggregate.php | 6 +- src/Deferred/DeferredInterface.php | 2 +- src/Deferred/PromiseInterface.php | 2 +- src/EventDispatcher/Events.php | 2 +- src/EventDispatcher/SignalEvent.php | 2 +- src/EventDispatcher/SignalEventDispatcher.php | 2 +- .../SignalEventDispatcherInterface.php | 4 +- .../SignalEventDispatcherTrait.php | 6 +- .../WrappedEventDispatcher.php | 2 +- src/Exception/ForkException.php | 4 +- src/Exception/ProcessControlException.php | 2 +- src/Exception/UnexpectedTypeException.php | 2 +- src/Factory.php | 8 +- src/Fork.php | 12 +- src/ProcessManager.php | 20 ++-- src/SharedMemory.php | 4 +- src/Signal/SignalHandlerWrapper.php | 2 +- src/Util/Error.php | 2 +- src/Util/ExitMessage.php | 2 +- src/Util/ThrottleIterator.php | 4 +- tests/Batch/Strategy/ChunkStrategyTest.php | 2 +- tests/Deferred/DeferredAggregateTest.php | 4 +- tests/Deferred/DeferredTest.php | 4 +- .../SignalEventDispatcherTest.php | 6 +- .../SignalEventDispatcherTestTrait.php | 4 +- .../EventDispatcher/SignalEventSubscriber.php | 2 +- .../WrappedEventDispatcherTest.php | 6 +- tests/ProcessManagerTest.php | 2 +- tests/Signal/SignalHandlerWrapperTest.php | 2 +- tests/Util/ThrottleIteratorStub.php | 2 +- tests/Util/ThrottleIteratorTest.php | 2 +- 42 files changed, 150 insertions(+), 126 deletions(-) diff --git a/README.md b/README.md index 2fe88a0..668c8d1 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,23 @@ +# thelevti/phpfork + **[Requirements](#requirements)** | **[Installation](#installation)** | **[Usage](#usage)** -# thelevti/phpfork +[![Build Status][1]][2] -[![Build Status](https://travis-ci.com/TheLevti/phpfork.svg?branch=master)](https://travis-ci.com/TheLevti/phpfork) +A simple library to make forking a processes as easy as possible. -PHP on a fork. - -`thelevti/phpfork` follows semantic versioning. Read more on [semver.org][1]. +`thelevti/phpfork` follows semantic versioning. Read more on [semver.org][3]. ---- ## Requirements - - PHP 7.2 or above - - [php-pcntl][2] to allow this library forking processes. - - [php-posix][3] to allow this library getting process information. - - [php-shmop][4] to allow this library doing interprocess communication. +- PHP 7.2 or above +- [php-pcntl][4] to allow this library forking processes. +- [php-posix][5] to allow this library getting process information. +- [php-shmop][6] to allow this library doing interprocess communication. ---- @@ -25,31 +25,39 @@ PHP on a fork. ### Composer -To use this library through [composer][5], run the following terminal command +To use this library with [composer][7], run the following terminal command inside your repository's root folder. -```sh +```bash composer require "thelevti/phpfork" ``` ## Usage -This library uses the namespace `Phpfork`. +This library uses the namespace `TheLevti\phpfork`. + +### Example: Basic process forking ```php fork(function () { - // do something in another process! - return 'Hello from ' . getmypid(); -})->then(function (Phpfork\Fork $fork) { - // do something in the parent process when it's done! +use TheLevti\phpfork\Fork; +use TheLevti\phpfork\ProcessManager; +use TheLevti\phpfork\SharedMemory; + +$manager = new ProcessManager(); +$fork = $manager->fork(function (SharedMemory $shm) { + // Do something in a forked process! + return 'Hello from ' . posix_getpid(); +})->then(function (Fork $fork) { + // Do something in the parent process when the fork is done! echo "{$fork->getPid()} says '{$fork->getResult()}'\n"; }); + +$manager->wait(); ``` -### Example: Upload images to your CDN +### Example: Upload images to a CDN Feed an iterator into the process manager and it will break the job into multiple batches and spread them across many processes. @@ -57,11 +65,14 @@ multiple batches and spread them across many processes. ```php process($files, function(SplFileInfo $file) { +$manager = new ProcessManager(); +$batchJob = $manager->process($files, function(SplFileInfo $file) { // upload this file }); @@ -70,21 +81,31 @@ $manager->wait(); ### Example: Working with Doctrine DBAL -When working with database connections, there is a known issue regarding parent/child processes. -From http://php.net/manual/en/function.pcntl-fork.php#70721: +When working with database connections, there is a known issue regarding +parent/child processes. See php doc for [pcntl_fork][8]: -> the child process inherits the parent's database connection. -> When the child exits, the connection is closed. -> If the parent is performing a query at this very moment, it is doing it on an already closed connection +> The reason for the MySQL "Lost Connection during query" issue when forking is +the fact that the child process inherits the parent's database connection. When +the child exits, the connection is closed. If the parent is performing a query +at this very moment, it is doing it on an already closed connection, hence the +error. -This will mean that in our example, we will see a `SQLSTATE[HY000]: General error: 2006 MySQL server has gone away` -exception being thrown in the parent process. +This will mean that in our example, we will see a `SQLSTATE[HY000]: General +error: 2006 MySQL server has gone away` exception being thrown in the parent +process. -One work-around for this situation is to force-close the DB connection before forking, by using the PRE_FORK event. +One work-around for this situation is to force-close the DB connection before +forking, by using the `PRE_FORK` event. ```php '...', 'user' => '...', @@ -98,7 +119,7 @@ $dataArray = range(0, 15); $callback = function ($value) use ($params) { // Child process acquires its own DB connection - $conn = Doctrine\DBAL\DriverManager::getConnection($params); + $conn = DriverManager::getConnection($params); $conn->connect(); $sql = 'SELECT NOW() AS now'; @@ -111,18 +132,18 @@ $callback = function ($value) use ($params) { }; // Get DB connection in parent -$parentConnection = Doctrine\DBAL\DriverManager::getConnection($params); +$parentConnection = DriverManager::getConnection($params); $parentConnection->connect(); -$dispatcher = new Phpfork\EventDispatcher\EventDispatcher(); -$dispatcher->addListener(Phpfork\EventDispatcher\Events::PRE_FORK, function () use ($parentConnection) { +$dispatcher = new SignalEventDispatcher(); +$dispatcher->addListener(Events::PRE_FORK, function () use ($parentConnection) { $parentConnection->close(); }); -$manager = new Phpfork\ProcessManager($dispatcher, null, true); +$manager = new ProcessManager($dispatcher, null, true); -/** @var Phpfork\Fork $fork */ -$fork = $manager->process($dataArray, $callback, new Phpfork\Batch\Strategy\ChunkStrategy($forks)); +/** @var TheLevti\phpfork\Fork $fork */ +$fork = $manager->process($dataArray, $callback, new ChunkStrategy($forks)); $manager->wait(); $result = $fork->getResult(); @@ -135,8 +156,11 @@ $dbResult = $stmt->fetch(); $parentConnection->close(); ``` -[1]: https://semver.org -[2]: https://php.net/manual/en/book.pcntl.php -[3]: https://php.net/manual/en/book.posix.php -[4]: https://php.net/manual/en/book.shmop.php -[5]: https://getcomposer.org +[1]: https://travis-ci.com/TheLevti/phpfork.svg?branch=master +[2]: https://travis-ci.com/TheLevti/phpfork +[3]: https://semver.org +[4]: https://php.net/manual/en/book.pcntl.php +[5]: https://php.net/manual/en/book.posix.php +[6]: https://php.net/manual/en/book.shmop.php +[7]: https://getcomposer.org +[8]: http://php.net/manual/en/function.pcntl-fork.php#70721 diff --git a/composer.json b/composer.json index 146fe8c..03e5835 100644 --- a/composer.json +++ b/composer.json @@ -51,12 +51,12 @@ }, "autoload": { "psr-4": { - "Phpfork\\": "src/" + "TheLevti\\phpfork\\": "src/" } }, "autoload-dev": { "psr-4": { - "Phpfork\\": "tests/" + "TheLevti\\phpfork\\": "tests/" } }, "suggest": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6ad7559..e8030db 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,7 +11,7 @@ processIsolation="false" stopOnFailure="false"> - + ./tests diff --git a/src/Batch/BatchJob.php b/src/Batch/BatchJob.php index cc1fda1..e3b9835 100644 --- a/src/Batch/BatchJob.php +++ b/src/Batch/BatchJob.php @@ -11,12 +11,12 @@ declare(strict_types=1); -namespace Phpfork\Batch; +namespace TheLevti\phpfork\Batch; -use Phpfork\Batch\Strategy\ChunkStrategy; -use Phpfork\Batch\Strategy\StrategyInterface; -use Phpfork\Exception\UnexpectedTypeException; -use Phpfork\ProcessManager; +use TheLevti\phpfork\Batch\Strategy\ChunkStrategy; +use TheLevti\phpfork\Batch\Strategy\StrategyInterface; +use TheLevti\phpfork\Exception\UnexpectedTypeException; +use TheLevti\phpfork\ProcessManager; class BatchJob { diff --git a/src/Batch/BatchRunner.php b/src/Batch/BatchRunner.php index 428b5c2..731e09f 100644 --- a/src/Batch/BatchRunner.php +++ b/src/Batch/BatchRunner.php @@ -11,10 +11,10 @@ declare(strict_types=1); -namespace Phpfork\Batch; +namespace TheLevti\phpfork\Batch; -use Phpfork\Exception\UnexpectedTypeException; -use Phpfork\SharedMemory; +use TheLevti\phpfork\Exception\UnexpectedTypeException; +use TheLevti\phpfork\SharedMemory; class BatchRunner { diff --git a/src/Batch/Strategy/AbstractStrategy.php b/src/Batch/Strategy/AbstractStrategy.php index d6fb22e..e41b90e 100644 --- a/src/Batch/Strategy/AbstractStrategy.php +++ b/src/Batch/Strategy/AbstractStrategy.php @@ -11,9 +11,9 @@ declare(strict_types=1); -namespace Phpfork\Batch\Strategy; +namespace TheLevti\phpfork\Batch\Strategy; -use Phpfork\Batch\BatchRunner; +use TheLevti\phpfork\Batch\BatchRunner; abstract class AbstractStrategy implements StrategyInterface { diff --git a/src/Batch/Strategy/CallbackStrategy.php b/src/Batch/Strategy/CallbackStrategy.php index 04f50cd..c9c598a 100644 --- a/src/Batch/Strategy/CallbackStrategy.php +++ b/src/Batch/Strategy/CallbackStrategy.php @@ -11,9 +11,9 @@ declare(strict_types=1); -namespace Phpfork\Batch\Strategy; +namespace TheLevti\phpfork\Batch\Strategy; -use Phpfork\Exception\UnexpectedTypeException; +use TheLevti\phpfork\Exception\UnexpectedTypeException; class CallbackStrategy extends AbstractStrategy { diff --git a/src/Batch/Strategy/ChunkStrategy.php b/src/Batch/Strategy/ChunkStrategy.php index 759709e..c61d3c0 100644 --- a/src/Batch/Strategy/ChunkStrategy.php +++ b/src/Batch/Strategy/ChunkStrategy.php @@ -11,9 +11,9 @@ declare(strict_types=1); -namespace Phpfork\Batch\Strategy; +namespace TheLevti\phpfork\Batch\Strategy; -use Phpfork\Exception\UnexpectedTypeException; +use TheLevti\phpfork\Exception\UnexpectedTypeException; /** * Creates the batch iterator using array_chunk(). diff --git a/src/Batch/Strategy/StrategyInterface.php b/src/Batch/Strategy/StrategyInterface.php index 4fd5e14..34a7562 100644 --- a/src/Batch/Strategy/StrategyInterface.php +++ b/src/Batch/Strategy/StrategyInterface.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Batch\Strategy; +namespace TheLevti\phpfork\Batch\Strategy; /** * @see BatchJob::__invoke() diff --git a/src/Batch/Strategy/ThrottleStrategy.php b/src/Batch/Strategy/ThrottleStrategy.php index f2dc892..72036d7 100644 --- a/src/Batch/Strategy/ThrottleStrategy.php +++ b/src/Batch/Strategy/ThrottleStrategy.php @@ -11,9 +11,9 @@ declare(strict_types=1); -namespace Phpfork\Batch\Strategy; +namespace TheLevti\phpfork\Batch\Strategy; -use Phpfork\Util\ThrottleIterator; +use TheLevti\phpfork\Util\ThrottleIterator; class ThrottleStrategy implements StrategyInterface { diff --git a/src/Deferred/Deferred.php b/src/Deferred/Deferred.php index c2711b9..ba85fa4 100644 --- a/src/Deferred/Deferred.php +++ b/src/Deferred/Deferred.php @@ -11,9 +11,9 @@ declare(strict_types=1); -namespace Phpfork\Deferred; +namespace TheLevti\phpfork\Deferred; -use Phpfork\Exception\UnexpectedTypeException; +use TheLevti\phpfork\Exception\UnexpectedTypeException; class Deferred implements DeferredInterface { diff --git a/src/Deferred/DeferredAggregate.php b/src/Deferred/DeferredAggregate.php index 70dab6e..e779efc 100644 --- a/src/Deferred/DeferredAggregate.php +++ b/src/Deferred/DeferredAggregate.php @@ -11,9 +11,9 @@ declare(strict_types=1); -namespace Phpfork\Deferred; +namespace TheLevti\phpfork\Deferred; -use Phpfork\Exception\UnexpectedTypeException; +use TheLevti\phpfork\Exception\UnexpectedTypeException; class DeferredAggregate implements PromiseInterface { @@ -25,7 +25,7 @@ public function __construct(array $children) // validate children foreach ($children as $child) { if (!$child instanceof PromiseInterface) { - throw new UnexpectedTypeException($child, 'Phpfork\Deferred\PromiseInterface'); + throw new UnexpectedTypeException($child, 'TheLevti\phpfork\Deferred\PromiseInterface'); } } diff --git a/src/Deferred/DeferredInterface.php b/src/Deferred/DeferredInterface.php index 415b2c3..224c16f 100644 --- a/src/Deferred/DeferredInterface.php +++ b/src/Deferred/DeferredInterface.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Deferred; +namespace TheLevti\phpfork\Deferred; interface DeferredInterface extends PromiseInterface { diff --git a/src/Deferred/PromiseInterface.php b/src/Deferred/PromiseInterface.php index 000d92e..e41ea85 100644 --- a/src/Deferred/PromiseInterface.php +++ b/src/Deferred/PromiseInterface.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Deferred; +namespace TheLevti\phpfork\Deferred; interface PromiseInterface { diff --git a/src/EventDispatcher/Events.php b/src/EventDispatcher/Events.php index c91e290..fdcba90 100644 --- a/src/EventDispatcher/Events.php +++ b/src/EventDispatcher/Events.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\EventDispatcher; +namespace TheLevti\phpfork\EventDispatcher; final class Events { diff --git a/src/EventDispatcher/SignalEvent.php b/src/EventDispatcher/SignalEvent.php index 3952d76..517a3ae 100644 --- a/src/EventDispatcher/SignalEvent.php +++ b/src/EventDispatcher/SignalEvent.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\EventDispatcher; +namespace TheLevti\phpfork\EventDispatcher; use Symfony\Contracts\EventDispatcher\Event as BaseEvent; diff --git a/src/EventDispatcher/SignalEventDispatcher.php b/src/EventDispatcher/SignalEventDispatcher.php index 7352de2..637c447 100644 --- a/src/EventDispatcher/SignalEventDispatcher.php +++ b/src/EventDispatcher/SignalEventDispatcher.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\EventDispatcher; +namespace TheLevti\phpfork\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher; diff --git a/src/EventDispatcher/SignalEventDispatcherInterface.php b/src/EventDispatcher/SignalEventDispatcherInterface.php index 7d618ae..72b785f 100644 --- a/src/EventDispatcher/SignalEventDispatcherInterface.php +++ b/src/EventDispatcher/SignalEventDispatcherInterface.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\EventDispatcher; +namespace TheLevti\phpfork\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -28,7 +28,7 @@ interface SignalEventDispatcherInterface extends EventDispatcherInterface * @param mixed $signinfo If operating systems supports siginfo_t * structures, this will be an array of signal * information dependent on the signal. - * @return \Phpfork\EventDispatcher\SignalEvent Holds the signal information. + * @return \TheLevti\phpfork\EventDispatcher\SignalEvent Holds the signal information. */ public function dispatchSignal(int $signo, $signinfo): SignalEvent; diff --git a/src/EventDispatcher/SignalEventDispatcherTrait.php b/src/EventDispatcher/SignalEventDispatcherTrait.php index 303efd2..c727f26 100644 --- a/src/EventDispatcher/SignalEventDispatcherTrait.php +++ b/src/EventDispatcher/SignalEventDispatcherTrait.php @@ -11,9 +11,9 @@ declare(strict_types=1); -namespace Phpfork\EventDispatcher; +namespace TheLevti\phpfork\EventDispatcher; -use Phpfork\Signal\SignalHandlerWrapper; +use TheLevti\phpfork\Signal\SignalHandlerWrapper; use UnexpectedValueException; /** @@ -26,7 +26,7 @@ trait SignalEventDispatcherTrait * Holds signal handler wrappers to preserve a potentially already existing * signal handler. * - * @var array $sigHandlerWrappers + * @var array $sigHandlerWrappers */ private $sigHandlerWrappers = []; diff --git a/src/EventDispatcher/WrappedEventDispatcher.php b/src/EventDispatcher/WrappedEventDispatcher.php index 605e894..77d12d0 100644 --- a/src/EventDispatcher/WrappedEventDispatcher.php +++ b/src/EventDispatcher/WrappedEventDispatcher.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\EventDispatcher; +namespace TheLevti\phpfork\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; diff --git a/src/Exception/ForkException.php b/src/Exception/ForkException.php index eaeb112..84b2f3b 100644 --- a/src/Exception/ForkException.php +++ b/src/Exception/ForkException.php @@ -11,9 +11,9 @@ declare(strict_types=1); -namespace Phpfork\Exception; +namespace TheLevti\phpfork\Exception; -use Phpfork\Util\Error; +use TheLevti\phpfork\Util\Error; /** * Turns an error passed through shared memory into an exception. diff --git a/src/Exception/ProcessControlException.php b/src/Exception/ProcessControlException.php index 4912e25..3c8bbd0 100644 --- a/src/Exception/ProcessControlException.php +++ b/src/Exception/ProcessControlException.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Exception; +namespace TheLevti\phpfork\Exception; class ProcessControlException extends \RuntimeException { diff --git a/src/Exception/UnexpectedTypeException.php b/src/Exception/UnexpectedTypeException.php index a50078e..b1ee9e5 100644 --- a/src/Exception/UnexpectedTypeException.php +++ b/src/Exception/UnexpectedTypeException.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Exception; +namespace TheLevti\phpfork\Exception; class UnexpectedTypeException extends \LogicException { diff --git a/src/Factory.php b/src/Factory.php index a93ecce..fe37da9 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -11,10 +11,10 @@ declare(strict_types=1); -namespace Phpfork; +namespace TheLevti\phpfork; -use Phpfork\Batch\BatchJob; -use Phpfork\Batch\Strategy\StrategyInterface; +use TheLevti\phpfork\Batch\BatchJob; +use TheLevti\phpfork\Batch\Strategy\StrategyInterface; class Factory { @@ -38,7 +38,7 @@ public function createBatchJob(ProcessManager $manager, $data = null, StrategyIn * @param int|null $pid The child process id or null if this is the child. * @param int|null $signal The signal to send after writing to shared memory. * - * @return \Phpfork\SharedMemory A new shared memory instance. + * @return \TheLevti\phpfork\SharedMemory A new shared memory instance. */ public function createSharedMemory(?int $pid = null, ?int $signal = null): SharedMemory { diff --git a/src/Fork.php b/src/Fork.php index 5a04341..f4136c0 100644 --- a/src/Fork.php +++ b/src/Fork.php @@ -11,13 +11,13 @@ declare(strict_types=1); -namespace Phpfork; +namespace TheLevti\phpfork; -use Phpfork\Deferred\Deferred; -use Phpfork\Deferred\DeferredInterface; -use Phpfork\Exception\ForkException; -use Phpfork\Exception\ProcessControlException; -use Phpfork\Util\ExitMessage; +use TheLevti\phpfork\Deferred\Deferred; +use TheLevti\phpfork\Deferred\DeferredInterface; +use TheLevti\phpfork\Exception\ForkException; +use TheLevti\phpfork\Exception\ProcessControlException; +use TheLevti\phpfork\Util\ExitMessage; class Fork implements DeferredInterface { diff --git a/src/ProcessManager.php b/src/ProcessManager.php index 59e7645..6ee892b 100644 --- a/src/ProcessManager.php +++ b/src/ProcessManager.php @@ -11,18 +11,18 @@ declare(strict_types=1); -namespace Phpfork; +namespace TheLevti\phpfork; use Exception; use InvalidArgumentException; -use Phpfork\Batch\Strategy\StrategyInterface; -use Phpfork\EventDispatcher\Events; -use Phpfork\EventDispatcher\SignalEventDispatcher; -use Phpfork\EventDispatcher\SignalEventDispatcherInterface; -use Phpfork\Exception\ProcessControlException; -use Phpfork\Util\Error; -use Phpfork\Util\ExitMessage; use Symfony\Contracts\EventDispatcher\Event; +use TheLevti\phpfork\Batch\Strategy\StrategyInterface; +use TheLevti\phpfork\EventDispatcher\Events; +use TheLevti\phpfork\EventDispatcher\SignalEventDispatcher; +use TheLevti\phpfork\EventDispatcher\SignalEventDispatcherInterface; +use TheLevti\phpfork\Exception\ProcessControlException; +use TheLevti\phpfork\Util\Error; +use TheLevti\phpfork\Util\ExitMessage; class ProcessManager { @@ -33,7 +33,7 @@ class ProcessManager private $zombieOkay; private $signal; - /** @var array $forks */ + /** @var array $forks */ private $forks; public function __construct( @@ -89,7 +89,7 @@ public function process($data, $callable, StrategyInterface $strategy = null) * Forks something into another process and returns a deferred object. * * @param callable $callable Code to execute in a fork. - * @return \Phpfork\Fork Newly created fork. + * @return \TheLevti\phpfork\Fork Newly created fork. */ public function fork(callable $callable): Fork { diff --git a/src/SharedMemory.php b/src/SharedMemory.php index e0880ec..f79faf0 100644 --- a/src/SharedMemory.php +++ b/src/SharedMemory.php @@ -11,9 +11,9 @@ declare(strict_types=1); -namespace Phpfork; +namespace TheLevti\phpfork; -use Phpfork\Exception\ProcessControlException; +use TheLevti\phpfork\Exception\ProcessControlException; /** * Sends messages between processes. diff --git a/src/Signal/SignalHandlerWrapper.php b/src/Signal/SignalHandlerWrapper.php index 64b9306..46c2c05 100644 --- a/src/Signal/SignalHandlerWrapper.php +++ b/src/Signal/SignalHandlerWrapper.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Signal; +namespace TheLevti\phpfork\Signal; /** * Wraps an existing signal handler with a new signal handler. diff --git a/src/Util/Error.php b/src/Util/Error.php index 1162c41..74f0657 100644 --- a/src/Util/Error.php +++ b/src/Util/Error.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Util; +namespace TheLevti\phpfork\Util; use Serializable; diff --git a/src/Util/ExitMessage.php b/src/Util/ExitMessage.php index 19e3191..dd57d40 100644 --- a/src/Util/ExitMessage.php +++ b/src/Util/ExitMessage.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Util; +namespace TheLevti\phpfork\Util; class ExitMessage implements \Serializable { diff --git a/src/Util/ThrottleIterator.php b/src/Util/ThrottleIterator.php index c6cf8cc..35b327f 100644 --- a/src/Util/ThrottleIterator.php +++ b/src/Util/ThrottleIterator.php @@ -11,9 +11,9 @@ declare(strict_types=1); -namespace Phpfork\Util; +namespace TheLevti\phpfork\Util; -use Phpfork\Exception\UnexpectedTypeException; +use TheLevti\phpfork\Exception\UnexpectedTypeException; /** * Throttles iteration based on a system load threshold. diff --git a/tests/Batch/Strategy/ChunkStrategyTest.php b/tests/Batch/Strategy/ChunkStrategyTest.php index c898cd3..30e61d5 100644 --- a/tests/Batch/Strategy/ChunkStrategyTest.php +++ b/tests/Batch/Strategy/ChunkStrategyTest.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Batch\Strategy; +namespace TheLevti\phpfork\Batch\Strategy; use PHPUnit\Framework\TestCase; diff --git a/tests/Deferred/DeferredAggregateTest.php b/tests/Deferred/DeferredAggregateTest.php index 444a24e..581f1cb 100644 --- a/tests/Deferred/DeferredAggregateTest.php +++ b/tests/Deferred/DeferredAggregateTest.php @@ -11,10 +11,10 @@ declare(strict_types=1); -namespace Phpfork\Deferred; +namespace TheLevti\phpfork\Deferred; -use Phpfork\Exception\UnexpectedTypeException; use PHPUnit\Framework\TestCase; +use TheLevti\phpfork\Exception\UnexpectedTypeException; class DeferredAggregateTest extends TestCase { diff --git a/tests/Deferred/DeferredTest.php b/tests/Deferred/DeferredTest.php index 9f99f5b..eba1494 100644 --- a/tests/Deferred/DeferredTest.php +++ b/tests/Deferred/DeferredTest.php @@ -11,11 +11,11 @@ declare(strict_types=1); -namespace Phpfork\Deferred; +namespace TheLevti\phpfork\Deferred; use LogicException; -use Phpfork\Exception\UnexpectedTypeException; use PHPUnit\Framework\TestCase; +use TheLevti\phpfork\Exception\UnexpectedTypeException; class DeferredTest extends TestCase { diff --git a/tests/EventDispatcher/SignalEventDispatcherTest.php b/tests/EventDispatcher/SignalEventDispatcherTest.php index a5527ee..03b6024 100644 --- a/tests/EventDispatcher/SignalEventDispatcherTest.php +++ b/tests/EventDispatcher/SignalEventDispatcherTest.php @@ -11,10 +11,10 @@ declare(strict_types=1); -namespace Phpfork\EventDispatcher; +namespace TheLevti\phpfork\EventDispatcher; -use Phpfork\ProcessManager; use PHPUnit\Framework\TestCase; +use TheLevti\phpfork\ProcessManager; class SignalEventDispatcherTest extends TestCase { @@ -23,7 +23,7 @@ class SignalEventDispatcherTest extends TestCase /** * Process manager instance. * - * @var \Phpfork\ProcessManager $processManager + * @var \TheLevti\phpfork\ProcessManager $processManager */ private $processManager; diff --git a/tests/EventDispatcher/SignalEventDispatcherTestTrait.php b/tests/EventDispatcher/SignalEventDispatcherTestTrait.php index dce2416..a163732 100644 --- a/tests/EventDispatcher/SignalEventDispatcherTestTrait.php +++ b/tests/EventDispatcher/SignalEventDispatcherTestTrait.php @@ -11,11 +11,11 @@ declare(strict_types=1); -namespace Phpfork\EventDispatcher; +namespace TheLevti\phpfork\EventDispatcher; -use Phpfork\SharedMemory; use ReflectionObject; use Symfony\Component\EventDispatcher\EventDispatcher; +use TheLevti\phpfork\SharedMemory; use UnexpectedValueException; /** diff --git a/tests/EventDispatcher/SignalEventSubscriber.php b/tests/EventDispatcher/SignalEventSubscriber.php index 19a6abf..6369358 100644 --- a/tests/EventDispatcher/SignalEventSubscriber.php +++ b/tests/EventDispatcher/SignalEventSubscriber.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\EventDispatcher; +namespace TheLevti\phpfork\EventDispatcher; use Symfony\Component\EventDispatcher\EventSubscriberInterface; diff --git a/tests/EventDispatcher/WrappedEventDispatcherTest.php b/tests/EventDispatcher/WrappedEventDispatcherTest.php index 38e235d..d1da603 100644 --- a/tests/EventDispatcher/WrappedEventDispatcherTest.php +++ b/tests/EventDispatcher/WrappedEventDispatcherTest.php @@ -11,11 +11,11 @@ declare(strict_types=1); -namespace Phpfork\EventDispatcher; +namespace TheLevti\phpfork\EventDispatcher; -use Phpfork\ProcessManager; use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\EventDispatcher; +use TheLevti\phpfork\ProcessManager; class WrappedEventDispatcherTest extends TestCase { @@ -24,7 +24,7 @@ class WrappedEventDispatcherTest extends TestCase /** * Process manager instance. * - * @var \Phpfork\ProcessManager $processManager + * @var \TheLevti\phpfork\ProcessManager $processManager */ private $processManager; diff --git a/tests/ProcessManagerTest.php b/tests/ProcessManagerTest.php index 32fe320..fd10c17 100644 --- a/tests/ProcessManagerTest.php +++ b/tests/ProcessManagerTest.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork; +namespace TheLevti\phpfork; use Exception; use PHPUnit\Framework\TestCase; diff --git a/tests/Signal/SignalHandlerWrapperTest.php b/tests/Signal/SignalHandlerWrapperTest.php index 1ec5cee..d4985e1 100644 --- a/tests/Signal/SignalHandlerWrapperTest.php +++ b/tests/Signal/SignalHandlerWrapperTest.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Signal; +namespace TheLevti\phpfork\Signal; use PHPUnit\Framework\TestCase; diff --git a/tests/Util/ThrottleIteratorStub.php b/tests/Util/ThrottleIteratorStub.php index 6230f36..f749715 100644 --- a/tests/Util/ThrottleIteratorStub.php +++ b/tests/Util/ThrottleIteratorStub.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Util; +namespace TheLevti\phpfork\Util; class ThrottleIteratorStub extends ThrottleIterator { diff --git a/tests/Util/ThrottleIteratorTest.php b/tests/Util/ThrottleIteratorTest.php index f440e15..cf3d324 100644 --- a/tests/Util/ThrottleIteratorTest.php +++ b/tests/Util/ThrottleIteratorTest.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace Phpfork\Util; +namespace TheLevti\phpfork\Util; use PHPUnit\Framework\TestCase; From c0219818a322a6fdc82ff7aa079d4d4a9db16233 Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sun, 9 Feb 2020 21:19:03 +0100 Subject: [PATCH 2/3] kriswallsmith/spork#40: Do not lose fork messages after receive --- src/Fork.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Fork.php b/src/Fork.php index f4136c0..54bab64 100644 --- a/src/Fork.php +++ b/src/Fork.php @@ -29,6 +29,9 @@ class Fork implements DeferredInterface private $status; private $message; + /** @var array $messages */ + private $messages; + public function __construct($pid, SharedMemory $shm, $debug = false) { $this->defer = new Deferred(); @@ -36,6 +39,7 @@ public function __construct($pid, SharedMemory $shm, $debug = false) $this->shm = $shm; $this->debug = $debug; $this->name = ''; + $this->messages = []; } /** @@ -94,19 +98,17 @@ public function processWaitStatus($status) } } - public function receive() + public function receive(): array { - $messages = []; - foreach ($this->shm->receive() as $message) { if ($message instanceof ExitMessage) { $this->message = $message; } else { - $messages[] = $message; + $this->messages[] = $message; } } - return $messages; + return $this->messages; } public function kill($signal = SIGINT) @@ -139,6 +141,11 @@ public function getError() } } + public function getMessages(): array + { + return $this->messages; + } + public function isSuccessful() { return 0 === $this->getExitStatus(); From 572831909538e72554dbd1eec03db5120f7844ab Mon Sep 17 00:00:00 2001 From: Petr Levtonov Date: Sun, 9 Feb 2020 21:28:34 +0100 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6cf566..78a3222 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [4.0.0] - 2020-02-09 + +### Changed + +- 27d0cc4: Add vendor namespace prefix and improve README.md. + +### Fixed + +- kriswallsmith/spork#40: Do not lose fork messages after receive call. + ## [3.0.0] - 2020-02-02 ### Changed @@ -72,7 +82,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added progress callbacks to Deferred. - Added serializable objects for exit and error messages. -[Unreleased]: https://github.com/TheLevti/phpfork/compare/3.0.0...HEAD +[Unreleased]: https://github.com/TheLevti/phpfork/compare/4.0.0...HEAD +[4.0.0]: https://github.com/TheLevti/phpfork/releases/4.0.0 [3.0.0]: https://github.com/TheLevti/phpfork/releases/3.0.0 [2.0.2]: https://github.com/TheLevti/phpfork/releases/2.0.2 [2.0.1]: https://github.com/TheLevti/phpfork/releases/2.0.1