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

Rename process #44

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Fork.php
Expand Up @@ -66,10 +66,10 @@ public function run(callable ...$callables): array
$tasks[] = Task::fromCallable($callable, $order);
}

return $this->waitFor(...$tasks);
return $this->runTasks(...$tasks);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that these changes are not required to make the feature work. Make sure that there are only related changes in this PR.

}

protected function waitFor(Task ...$queue): array
public function runTasks(Task ...$queue): array
{
$output = [];

Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/Task.php
Expand Up @@ -9,7 +9,7 @@ class Task
{
protected const SERIALIZATION_TOKEN = '[[serialized::';

protected string $name;
protected string $name = '';

protected int $order;

Expand Down
22 changes: 22 additions & 0 deletions tests/ForkTest.php
Expand Up @@ -2,6 +2,7 @@

use Carbon\Carbon;
use Spatie\Fork\Fork;
use Spatie\Fork\Task;

it('will execute the given closures')
->expect(
Expand Down Expand Up @@ -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])->toEqual(true)
->and($result[1])->toEqual(true)
->and($result[2])->toEqual(true);
});