Skip to content

Commit

Permalink
Fix: 'proc_close' blocking issue
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Jan 19, 2025
1 parent ef8c780 commit 67a21f3
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 139 deletions.
31 changes: 27 additions & 4 deletions src/Proc/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
use function posix_kill;
use function proc_close;
use function proc_get_status;
use function proc_terminate;

use const SIGTERM;

/**
* @Author cclilshy
Expand Down Expand Up @@ -132,9 +135,16 @@ public function close(): void
}

try {
proc_close($this->proc);
if (!$this->getStatus('running')) {
proc_close($this->proc);
} else {
$this->terminate(SIGTERM);
if (!$this->getStatus('running')) {
proc_close($this->proc);
}
}
} catch (Throwable) {
// ignore
proc_close($this->proc);
}

if (isset($this->onClose)) {
Expand Down Expand Up @@ -192,16 +202,29 @@ public function inputSignal(int $signalCode): bool
}

/**
* @param string $key
* @param string|null $key
*
* @return mixed
*/
public function getStatus(string $key): mixed
public function getStatus(string|null $key = null): mixed
{
$status = proc_get_status($this->proc);
return $key ? ($status[$key] ?? null) : $status;
}

/**
* @param int|null $signal
*
* @return bool
*/
public function terminate(int|null $signal = null): bool
{
if ($signal) {
return proc_terminate($this->proc, $signal);
}
return proc_terminate($this->proc);
}

/**
* @return mixed
*/
Expand Down
17 changes: 12 additions & 5 deletions src/Worker/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ class Manager
/**
* @var Worker[]
*/
private array $workers = [];
protected array $workers = [];

/**
* @var Zx7e
*/
private Zx7e $zx7e;
protected Zx7e $zx7e;

/**
* @var int
*/
private int $index = 1;
protected int $index = 1;

/**
* @var int
*/
private int $processID;
protected int $processID;

/**
* @Author cclilshy
Expand All @@ -59,7 +59,6 @@ class Manager
*/
public function addWorker(Worker $worker): void
{
$worker->register($this);
$workerName = $worker->getName();
if (isset($this->workers[$workerName])) {
Output::warning("Worker {$workerName} already exists");
Expand Down Expand Up @@ -116,6 +115,14 @@ public function stop(): void
}
}

/**
* @return \Ripple\Worker\Worker[]
*/
public function getWorkers(): array
{
return $this->workers;
}

/**
* @Author cclilshy
* @Date 2024/8/17 00:13
Expand Down
Loading

0 comments on commit 67a21f3

Please sign in to comment.