Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kill any forked tasks when force closed in terminal #48

Merged
merged 2 commits into from
Nov 23, 2023
Merged

Kill any forked tasks when force closed in terminal #48

merged 2 commits into from
Nov 23, 2023

Conversation

stevebauman
Copy link
Contributor

I discovered that if you run one or more forked processes and force-terminate the parent running process in the CLI before the child (forked) processes finish, Fork won't terminate the stray running processes, leaving them running in the background.

This PR simply iterates through any running tasks if the CLI command is terminated and kills them via their PID.

@freekmurze freekmurze merged commit 5c38486 into spatie:main Nov 23, 2023
5 of 6 checks passed
@freekmurze
Copy link
Member

Nice, thanks!

@malohtie
Copy link

malohtie commented Dec 2, 2023

after this release we start to notice that fork return empty string in a random way

 function is_server_running(string $ip, int $port = 22, int $timeout = 5): bool
{
    $result = command("nc -zv -w$timeout $ip $port 2>&1");
    $result = str($result['output'])->lower();
    if ($result->contains(['succeeded', 'connected'])) {
        return true;
    }

    return false;
}
  
[$ssh, $apache, $goservice] = Fork::new()->run(
    fn () => is_server_running($server->ip, $server->port, timeout:10),
    fn () => is_server_running($server->ip, 80, timeout:10),
    fn () => is_server_running($server->ip, config('settings.goservice'), timeout:10)
) ;

dump($ssh);
dump($apache);
dump($goservice);
[root@hubdms ~]# artisan server:check_status 11907
Checking Server:  11907 - 62.4.16.xxx
true // app/Console/Commands/servers/ServerCheckStatus.php:43
"" // app/Console/Commands/servers/ServerCheckStatus.php:43
true // app/Console/Commands/servers/ServerCheckStatus.php:43
[root@hubdms ~]# artisan server:check_status 11907
Checking Server: 11907 - 62.4.16.xxx
true // app/Console/Commands/servers/ServerCheckStatus.php:43
"" // app/Console/Commands/servers/ServerCheckStatus.php:43
true // app/Console/Commands/servers/ServerCheckStatus.php:43
[root@hubdms ~]# artisan server:check_status 11907
Checking Server:  11907 - 62.4.16.xxx
true // app/Console/Commands/servers/ServerCheckStatus.php:43
true // app/Console/Commands/servers/ServerCheckStatus.php:43
true // app/Console/Commands/servers/ServerCheckStatus.php:43
[root@hubdms ~]# artisan server:check_status 11907
Checking Servers Status
Checking Server:  11907 - 62.4.16.xxx
"" // app/Console/Commands/servers/ServerCheckStatus.php:43
true // app/Console/Commands/servers/ServerCheckStatus.php:43
true // app/Console/Commands/servers/ServerCheckStatus.php:43


PHP 8.1.18 (cli) (built: Apr 11 2023 16:47:45) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.18, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.18, Copyright (c), by Zend Technologies

we rollback to 1.2.0 and everything start to work as expected.

cc @stevebauman

@stevebauman
Copy link
Contributor Author

Hey @malohtie, looking into this now, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants