Skip to content

Commit

Permalink
Merge pull request spatie#42 from zKoz210/fix-terminating-process
Browse files Browse the repository at this point in the history
Listen signals for terminating process
  • Loading branch information
freekmurze authored May 29, 2023
2 parents d901ec9 + 390f34f commit 686af73
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"8.2.4","version":"3.16.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"curly_braces_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"single_trait_insert_per_statement":true},"hashes":{"src\/Task.php":"3876dd1bc99af211f5eeb4e96289b17b","src\/Exceptions\/CouldNotManageTask.php":"ff4093a3769e440abb4f1d59e502a85e","src\/Fork.php":"cfd58a7a98133141fc9720dc17649689","src\/Connection.php":"2cfcaf4c3df1c8488f448badfdcfaad0","tests\/Pest.php":"d505920f050280f3fd9432b355daa382","tests\/ForkTest.php":"743fe8646b4edf28801062c5ef806e1c"}}
{"php":"8.2.6","version":"3.16.0:v3.16.0#d40f9436e1c448d309fa995ab9c14c5c7a96f2dc","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"curly_braces_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"single_trait_insert_per_statement":true},"hashes":{"src\/Connection.php":"2cfcaf4c3df1c8488f448badfdcfaad0","src\/Exceptions\/CouldNotManageTask.php":"ff4093a3769e440abb4f1d59e502a85e","src\/Task.php":"3876dd1bc99af211f5eeb4e96289b17b","src\/Fork.php":"a25fd8bbbbe0e30ef736ac7d4139c268","tests\/ForkTest.php":"743fe8646b4edf28801062c5ef806e1c","tests\/Pest.php":"d505920f050280f3fd9432b355daa382"}}
34 changes: 28 additions & 6 deletions src/Fork.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ protected function waitFor(Task ...$queue): array
{
$output = [];

$this->listenForSignals();
$this->startRunning(...$queue);

while ($this->isRunning()) {
Expand Down Expand Up @@ -125,13 +126,11 @@ protected function forkForTask(Task $task): Task
if ($this->currentlyInChildTask($processId)) {
$socketToChild->close();

$this->executeInChildTask($task, $socketToParent);

if (extension_loaded('posix')) {
posix_kill(getmypid(), SIGKILL);
try {
$this->executeInChildTask($task, $socketToParent);
} finally {
$this->exit();
}

exit;
}

$socketToParent->close();
Expand All @@ -142,6 +141,29 @@ protected function forkForTask(Task $task): Task
->setConnection($socketToChild);
}

/**
* Enable async signals for the process.
*
* @return void
*/
protected function listenForSignals(): void
{
pcntl_async_signals(true);

pcntl_signal(SIGQUIT, fn () => $this->exit());
pcntl_signal(SIGTERM, fn () => $this->exit());
pcntl_signal(SIGINT, fn () => $this->exit());
}

protected function exit(): void
{
if (extension_loaded('posix')) {
posix_kill(getmypid(), SIGKILL);
}

exit;
}

protected function currentlyInChildTask(int $pid): bool
{
return $pid === 0;
Expand Down

0 comments on commit 686af73

Please sign in to comment.