1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-30 04:39:01 +01:00

Minor cleanup/refactoring

This commit is contained in:
Aaron Piotrowski 2016-09-07 11:38:46 -05:00
parent 4b5ca2013d
commit c4a5082d90
5 changed files with 27 additions and 33 deletions

View File

@ -249,7 +249,7 @@ class Fork implements Process, Strand {
public function kill() {
if ($this->isRunning()) {
// Forcefully kill the process using SIGKILL.
\posix_kill($this->pid, SIGKILL);
\posix_kill($this->pid, \SIGKILL);
}
if ($this->channel !== null) {
@ -271,7 +271,7 @@ class Fork implements Process, Strand {
throw new StatusError('The fork has not been started or has already finished.');
}
\posix_kill($this->pid, (int) $signo);
\posix_kill($this->pid, $signo);
}
/**
@ -308,11 +308,11 @@ class Fork implements Process, Strand {
\is_object($response) ? \get_class($response) : \gettype($response)
));
}
return $response->getResult();
} finally {
$this->kill();
}
return $response->getResult();
}
/**

View File

@ -200,22 +200,18 @@ class Thread implements Strand {
private function doJoin(): \Generator {
try {
$response = yield $this->channel->receive();
if (!$response instanceof ExitStatus) {
throw new SynchronizationError('Did not receive an exit status from thread.');
}
$result = $response->getResult();
$this->thread->join();
} catch (\Throwable $exception) {
$this->kill();
throw $exception;
} finally {
$this->close();
}
$this->close();
return $result;
return $response->getResult();
}
/**

View File

@ -6,10 +6,7 @@ use Amp\Failure;
use Amp\Parallel\TaskException;
use Interop\Async\Awaitable;
class TaskFailure implements TaskResult {
/** @var string */
private $id;
class TaskFailure extends TaskResult {
/** @var string */
private $type;
@ -23,16 +20,12 @@ class TaskFailure implements TaskResult {
private $trace;
public function __construct(string $id, \Throwable $exception) {
$this->id = $id;
parent::__construct($id);
$this->type = \get_class($exception);
$this->message = $exception->getMessage();
$this->code = $exception->getCode();
$this->trace = $exception->getTraceAsString();
}
public function getId(): string {
return $this->id;
}
public function getAwaitable(): Awaitable {
return new Failure(new TaskException(

View File

@ -4,14 +4,26 @@ namespace Amp\Parallel\Worker\Internal;
use Interop\Async\Awaitable;
interface TaskResult {
abstract class TaskResult {
/** @var string Task identifier. */
private $id;
/**
* @param string $id Task identifier.
*/
public function __construct(string $id) {
$this->id = $id;
}
/**
* @return string Task identifier.
*/
public function getId(): string;
public function getId(): string {
return $this->id;
}
/**
* @return \Interop\Async\Awaitable<mixed> Resolved with the task result or failure reason.
*/
public function getAwaitable(): Awaitable;
abstract public function getAwaitable(): Awaitable;
}

View File

@ -5,22 +5,15 @@ namespace Amp\Parallel\Worker\Internal;
use Amp\Success;
use Interop\Async\Awaitable;
class TaskSuccess implements TaskResult {
/** @var string */
private $id;
class TaskSuccess extends TaskResult {
/** @var mixed Result of task. */
private $result;
public function __construct(string $id, $result) {
$this->id = $id;
parent::__construct($id);
$this->result = $result;
}
public function getId(): string {
return $this->id;
}
public function getAwaitable(): Awaitable {
return new Success($this->result);
}