Skip to content

Commit

Permalink
#14: Replace debug setting with logger and improve exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLevti committed Apr 5, 2020
1 parent f6ecc55 commit 600aabd
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Exception/ForkException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* Turns an error passed through shared memory into an exception.
*/
class ForkException extends RuntimeException
class ForkException extends RuntimeException implements PhpforkExceptionInterface
{
/** @var string $name */
private $name;
Expand Down
18 changes: 18 additions & 0 deletions src/Exception/OutputBufferingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the thelevti/phpfork package.
*
* (c) Petr Levtonov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace TheLevti\phpfork\Exception;

class OutputBufferingException extends ProcessManagerException implements PhpforkExceptionInterface
{
}
20 changes: 20 additions & 0 deletions src/Exception/PhpforkExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the thelevti/phpfork package.
*
* (c) Petr Levtonov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace TheLevti\phpfork\Exception;

use Throwable;

interface PhpforkExceptionInterface extends Throwable
{
}
2 changes: 1 addition & 1 deletion src/Exception/ProcessControlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

use RuntimeException;

class ProcessControlException extends RuntimeException
class ProcessControlException extends RuntimeException implements PhpforkExceptionInterface
{
}
20 changes: 20 additions & 0 deletions src/Exception/ProcessManagerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the thelevti/phpfork package.
*
* (c) Petr Levtonov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace TheLevti\phpfork\Exception;

use RuntimeException;

class ProcessManagerException extends RuntimeException implements PhpforkExceptionInterface
{
}
2 changes: 1 addition & 1 deletion src/Exception/UnexpectedTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use LogicException;

class UnexpectedTypeException extends LogicException
class UnexpectedTypeException extends LogicException implements PhpforkExceptionInterface
{
/**
* @param mixed $value
Expand Down
14 changes: 3 additions & 11 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace TheLevti\phpfork;

use Psr\Log\LoggerInterface;
use TheLevti\phpfork\Batch\BatchJob;
use TheLevti\phpfork\Batch\Strategy\StrategyInterface;

Expand Down Expand Up @@ -45,17 +46,8 @@ public function createSharedMemory(?int $pid = null, ?int $signal = null): Share
return new SharedMemory($pid, $signal);
}

/**
* Creates a new fork instance.
*
* @param int $pid Process id
* @param SharedMemory $shm Shared memory
* @param bool $debug Debug mode
*
* @return Fork A new fork instance
*/
public function createFork($pid, SharedMemory $shm, $debug = false)
public function createFork(int $pid, SharedMemory $shm, ?LoggerInterface $logger = null): Fork
{
return new Fork($pid, $shm, $debug);
return new Fork($pid, $shm, $logger);
}
}
33 changes: 20 additions & 13 deletions src/Fork.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@

namespace TheLevti\phpfork;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use TheLevti\phpfork\Deferred\Deferred;
use TheLevti\phpfork\Deferred\DeferredInterface;
use TheLevti\phpfork\Deferred\PromiseInterface;
use TheLevti\phpfork\Exception\ForkException;
use TheLevti\phpfork\Exception\ProcessControlException;
use TheLevti\phpfork\Util\ExitMessage;

class Fork implements DeferredInterface
{
/** @var \TheLevti\phpfork\Deferred\DeferredInterface $defer */
private $defer;

/** @var int $pid */
private $pid;
protected $pid;

/** @var \TheLevti\phpfork\SharedMemory $shm */
private $shm;
protected $shm;

/** @var \Psr\Log\LoggerInterface $logger */
protected $logger;

/** @var bool $debug */
private $debug;
/** @var \TheLevti\phpfork\Deferred\DeferredInterface $defer */
private $defer;

/** @var string $name */
private $name;
Expand All @@ -46,12 +47,12 @@ class Fork implements DeferredInterface
/** @var array<int,mixed> $messages */
private $messages;

public function __construct(int $pid, SharedMemory $shm, bool $debug = false)
public function __construct(int $pid, SharedMemory $shm, ?LoggerInterface $logger = null)
{
$this->defer = new Deferred();
$this->pid = $pid;
$this->shm = $shm;
$this->debug = $debug;
$this->logger = $logger ?? new NullLogger();
$this->defer = new Deferred();
$this->name = '<anonymous>';
$this->status = null;
$this->message = null;
Expand Down Expand Up @@ -105,8 +106,14 @@ public function processWaitStatus(int $status): void

$this->isSuccessful() ? $this->resolve() : $this->reject();

if ($this->debug && (!$this->isSuccessful() || $this->getError())) {
throw new ForkException($this->name, $this->pid, $this->getError());
if (!$this->isSuccessful() || $this->getError()) {
$this->logger->error(
'Processed wait status with not successful result.',
[
'fork_pid' => $this->pid,
'fork_name' => $this->name,
]
);
}
}
}
Expand Down
44 changes: 25 additions & 19 deletions src/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,33 @@

use Exception;
use InvalidArgumentException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Contracts\EventDispatcher\Event;
use TheLevti\phpfork\Batch\BatchJob;
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\OutputBufferingException;
use TheLevti\phpfork\Exception\ProcessControlException;
use TheLevti\phpfork\Util\Error;
use TheLevti\phpfork\Util\ExitMessage;

class ProcessManager
class ProcessManager implements LoggerAwareInterface
{
use LoggerAwareTrait;

/** @var \TheLevti\phpfork\EventDispatcher\SignalEventDispatcherInterface $dispatcher */
private $dispatcher;
protected $dispatcher;

/** @var \TheLevti\phpfork\Factory $factory */
private $factory;
protected $factory;

/** @var bool $debug */
private $debug;
/** @var \Psr\Log\LoggerInterface $logger */
protected $logger;

/** @var bool $zombieOkay */
private $zombieOkay;
Expand All @@ -48,11 +55,12 @@ class ProcessManager
public function __construct(
SignalEventDispatcherInterface $dispatcher = null,
Factory $factory = null,
bool $debug = false
?LoggerInterface $logger = null
) {
$this->dispatcher = $dispatcher ?: new SignalEventDispatcher();
$this->factory = $factory ?: new Factory();
$this->debug = $debug;
$this->dispatcher = $dispatcher ?? new SignalEventDispatcher();
$this->factory = $factory ?? new Factory();
$this->logger = $logger ?? new NullLogger();

$this->zombieOkay = false;
$this->signal = null;
$this->forks = [];
Expand All @@ -75,11 +83,6 @@ public function getEventDispatcher(): SignalEventDispatcherInterface
return $this->dispatcher;
}

public function setDebug(bool $debug): void
{
$this->debug = $debug;
}

public function zombieOkay(bool $zombieOkay = true): void
{
$this->zombieOkay = $zombieOkay;
Expand Down Expand Up @@ -132,8 +135,8 @@ public function fork(callable $callable): Fork
$shm = $this->factory->createSharedMemory(null, $this->signal);
$message = new ExitMessage();

if (!$this->debug) {
ob_start();
if (ob_start() === false) {
throw new OutputBufferingException('Failed to start output buffering.');
}

try {
Expand All @@ -145,10 +148,13 @@ public function fork(callable $callable): Fork
$status = 1;
}

if (!$this->debug) {
$message->setOutput(ob_get_clean());
$output = ob_get_clean();
if ($output === false) {
throw new OutputBufferingException('Failed to get and clean output buffer.');
}

$message->setOutput($output);

try {
$shm->send($message, false);
} catch (Exception $exception) {
Expand All @@ -167,7 +173,7 @@ public function fork(callable $callable): Fork
return $this->forks[$pid] = $this->factory->createFork(
$pid,
$this->factory->createSharedMemory($pid),
$this->debug
$this->logger
);
}

Expand Down
1 change: 0 additions & 1 deletion tests/ProcessManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public function testBatchProcessingWithNewlineReturnValues(): void
9 => "SomeString\n109",
];

$this->manager->setDebug(true);
$fork = $this->manager->process($range, function ($item) {
return "SomeString\n$item";
});
Expand Down

0 comments on commit 600aabd

Please sign in to comment.