Skip to content

Commit

Permalink
Parallel - process timeout made configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 15, 2020
1 parent bc5c80a commit ebe779c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 4 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ parameters:
reportStaticMethodSignatures: false
parallel:
jobSize: 20
processTimeout: 60.0
polluteScopeWithLoopInitialAssignments: true
polluteScopeWithAlwaysIterableForeach: true
polluteCatchScopeWithTryAssignments: false
Expand Down Expand Up @@ -148,7 +149,8 @@ parametersSchema:
reportMaybesInMethodSignatures: bool()
reportStaticMethodSignatures: bool()
parallel: structure([
jobSize: int()
jobSize: int(),
processTimeout: float()
])
polluteScopeWithLoopInitialAssignments: bool()
polluteScopeWithAlwaysIterableForeach: bool()
Expand Down Expand Up @@ -377,6 +379,7 @@ services:
class: PHPStan\Parallel\ParallelAnalyser
arguments:
internalErrorsCountLimit: %internalErrorsCountLimit%
processTimeout: %parallel.processTimeout%

-
class: PHPStan\Parallel\Scheduler
Expand Down
9 changes: 7 additions & 2 deletions src/Parallel/ParallelAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ class ParallelAnalyser
/** @var int */
private $internalErrorsCountLimit;

/** @var float */
private $processTimeout;

/** @var ProcessPool */
private $processPool;

public function __construct(
IgnoredErrorHelper $ignoredErrorHelper,
int $internalErrorsCountLimit
int $internalErrorsCountLimit,
float $processTimeout
)
{
$this->ignoredErrorHelper = $ignoredErrorHelper;
$this->internalErrorsCountLimit = $internalErrorsCountLimit;
$this->processTimeout = $processTimeout;
}

/**
Expand Down Expand Up @@ -120,7 +125,7 @@ public function analyse(
$serverPort,
$processIdentifier,
$input
), $loop);
), $loop, $this->processTimeout);
$process->start(function (array $json) use ($process, &$internalErrors, &$errors, &$jobs, $postFileCallback, &$hasInferrablePropertyTypesFromConstructor, &$internalErrorsCount, &$reachedInternalErrorsCountLimit, $processIdentifier): void {
foreach ($json['errors'] as $jsonError) {
if (is_string($jsonError)) {
Expand Down
11 changes: 8 additions & 3 deletions src/Parallel/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Process
/** @var LoopInterface */
private $loop;

/** @var float */
private $timeoutSeconds;

/** @var WritableStreamInterface */
private $in;

Expand All @@ -39,11 +42,13 @@ class Process

public function __construct(
string $command,
LoopInterface $loop
LoopInterface $loop,
float $timeoutSeconds
)
{
$this->command = $command;
$this->loop = $loop;
$this->timeoutSeconds = $timeoutSeconds;
}

public function start(callable $onData, callable $onError, callable $onExit): void
Expand Down Expand Up @@ -103,9 +108,9 @@ public function request(array $data): void
{
$this->cancelTimer();
$this->in->write($data);
$this->timer = $this->loop->addTimer(60.0, function (): void {
$this->timer = $this->loop->addTimer($this->timeoutSeconds, function (): void {
$onError = $this->onError;
$onError(new \Exception('Child process timed out after 60 seconds.'));
$onError(new \Exception(sprintf('Child process timed out after %.1f seconds.', $this->timeoutSeconds)));
});
}

Expand Down

0 comments on commit ebe779c

Please sign in to comment.