From c846515286aa056acca0f144c1ac1abb18bdbe03 Mon Sep 17 00:00:00 2001 From: Ivan Dmitriev <28255085+zKoz210@users.noreply.github.com> Date: Mon, 29 May 2023 14:22:54 +0300 Subject: [PATCH 1/2] Rename process --- src/Fork.php | 8 ++++++-- src/Task.php | 2 +- tests/ForkTest.php | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Fork.php b/src/Fork.php index 5aef9f5..98c1fd0 100644 --- a/src/Fork.php +++ b/src/Fork.php @@ -66,10 +66,10 @@ public function run(callable ...$callables): array $tasks[] = Task::fromCallable($callable, $order); } - return $this->waitFor(...$tasks); + return $this->runTasks(...$tasks); } - protected function waitFor(Task ...$queue): array + public function runTasks(Task ...$queue): array { $output = []; @@ -123,6 +123,10 @@ protected function forkForTask(Task $task): Task $processId = pcntl_fork(); + if ($task->name()) { + cli_set_process_title($task->name()); + } + if ($this->currentlyInChildTask($processId)) { $socketToChild->close(); diff --git a/src/Task.php b/src/Task.php index 6640757..4a8b24e 100644 --- a/src/Task.php +++ b/src/Task.php @@ -9,7 +9,7 @@ class Task { protected const SERIALIZATION_TOKEN = '[[serialized::'; - protected string $name; + protected string $name = ''; protected int $order; diff --git a/tests/ForkTest.php b/tests/ForkTest.php index 94eb919..a467bd8 100644 --- a/tests/ForkTest.php +++ b/tests/ForkTest.php @@ -2,6 +2,7 @@ use Carbon\Carbon; use Spatie\Fork\Fork; +use Spatie\Fork\Task; it('will execute the given closures') ->expect( @@ -138,3 +139,24 @@ function () { fn () => 1 ); }); + + +it('custom process name', function () { + $tasks = []; + + for ($order = 0; $order < 3; $order++) { + $taskName = "Async Process Name. Index: $order"; + + $task = Task::fromCallable(fn () => cli_get_process_title() === $taskName, $order); + $task->setName($taskName); + + $tasks[] = $task; + } + + $result = Fork::new() + ->runTasks(...$tasks); + + expect($result[0])->toBeBool() + ->and($result[1])->toBeBool() + ->and($result[2])->toBeBool(); +}); From de372ae2e5cf2b9032e56e997a4e0e46345fd093 Mon Sep 17 00:00:00 2001 From: Oreo Oreoniv <28255085+zKoz210@users.noreply.github.com> Date: Mon, 29 May 2023 14:30:32 +0300 Subject: [PATCH 2/2] Fixed check tests --- tests/ForkTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ForkTest.php b/tests/ForkTest.php index a467bd8..adb7787 100644 --- a/tests/ForkTest.php +++ b/tests/ForkTest.php @@ -156,7 +156,7 @@ function () { $result = Fork::new() ->runTasks(...$tasks); - expect($result[0])->toBeBool() - ->and($result[1])->toBeBool() - ->and($result[2])->toBeBool(); + expect($result[0])->toEqual(true) + ->and($result[1])->toEqual(true) + ->and($result[2])->toEqual(true); });