1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Improve handling when threads cannot be used

This commit is contained in:
Matthew Brown 2020-02-17 17:48:01 -05:00
parent a2c2a55ae6
commit e48d2aef9c
3 changed files with 61 additions and 33 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace Psalm\Internal\Fork;
class ForkProcessErrorMessage implements ForkMessage
{
/** @var string */
public $message;
public function __construct(string $message)
{
$this->message = $message;
}
}

View File

@ -177,30 +177,36 @@ class Pool
$task_done_buffer = '';
foreach ($task_data_iterator as $i => $task_data) {
$task_result = $task_closure($i, $task_data);
$task_done_message = new ForkTaskDoneMessage($task_result);
$serialized_message = $task_done_buffer . base64_encode(serialize($task_done_message)) . "\n";
try {
foreach ($task_data_iterator as $i => $task_data) {
$task_result = $task_closure($i, $task_data);
if (strlen($serialized_message) > 200) {
$bytes_written = @fwrite($write_stream, $serialized_message);
$task_done_message = new ForkTaskDoneMessage($task_result);
$serialized_message = $task_done_buffer . base64_encode(serialize($task_done_message)) . "\n";
if (strlen($serialized_message) !== $bytes_written) {
$task_done_buffer = substr($serialized_message, $bytes_written);
if (strlen($serialized_message) > 200) {
$bytes_written = @fwrite($write_stream, $serialized_message);
if (strlen($serialized_message) !== $bytes_written) {
$task_done_buffer = substr($serialized_message, $bytes_written);
} else {
$task_done_buffer = '';
}
} else {
$task_done_buffer = '';
$task_done_buffer = $serialized_message;
}
} else {
$task_done_buffer = $serialized_message;
}
// Execute each child's shutdown closure before
// exiting the process
$results = $shutdown_closure();
// Serialize this child's produced results and send them to the parent.
$process_done_message = new ForkProcessDoneMessage($results ?: []);
} catch (\Throwable $t) {
$process_done_message = new ForkProcessErrorMessage($t->getMessage());
}
// Execute each child's shutdown closure before
// exiting the process
$results = $shutdown_closure();
// Serialize this child's produced results and send them to the parent.
$process_done_message = new ForkProcessDoneMessage($results ?: []);
$serialized_message = $task_done_buffer . base64_encode(serialize($process_done_message)) . "\n";
$bytes_to_write = strlen($serialized_message);
@ -324,6 +330,8 @@ class Pool
if ($this->task_done_closure !== null) {
($this->task_done_closure)($message->data);
}
} elseif ($message instanceof ForkProcessErrorMessage) {
throw new \Exception($message->message);
} else {
error_log('Child should return ForkMessage - response type=' . gettype($message));
$this->did_have_error = true;

View File

@ -294,11 +294,30 @@ if ($config->resolve_from_config_file) {
chdir($current_dir);
}
$threads = isset($options['threads'])
? (int)$options['threads']
: max(1, ProjectAnalyzer::getCpuCount() - 2);
$in_ci = isset($_SERVER['TRAVIS'])
|| isset($_SERVER['CIRCLECI'])
|| isset($_SERVER['APPVEYOR'])
|| isset($_SERVER['JENKINS_URL'])
|| isset($_SERVER['SCRUTINIZER'])
|| isset($_SERVER['GITLAB_CI'])
|| isset($_SERVER['GITHUB_WORKFLOW']);
if ($threads === 1
// disable progressbar on CI
if ($in_ci) {
$options['long-progress'] = true;
}
if (isset($options['threads'])) {
$threads = (int)$options['threads'];
} elseif (isset($options['debug']) || $in_ci) {
$threads = 1;
} else {
$threads = max(1, ProjectAnalyzer::getCpuCount() - 2);
}
if (!isset($options['threads'])
&& !isset($options['debug'])
&& $threads === 1
&& ini_get('pcre.jit') === '1'
&& PHP_OS === 'Darwin'
&& version_compare(PHP_VERSION, '7.3.0') >= 0
@ -433,18 +452,6 @@ if (isset($options['clear-global-cache'])) {
exit;
}
// disable progressbar on CI
if (isset($_SERVER['TRAVIS'])
|| isset($_SERVER['CIRCLECI'])
|| isset($_SERVER['APPVEYOR'])
|| isset($_SERVER['JENKINS_URL'])
|| isset($_SERVER['SCRUTINIZER'])
|| isset($_SERVER['GITLAB_CI'])
|| isset($_SERVER['GITHUB_WORKFLOW'])
) {
$options['long-progress'] = true;
}
$debug = array_key_exists('debug', $options) || array_key_exists('debug-by-line', $options);
if ($debug) {