Skip to content

Map output of concurrecy calls to the index of the input #53135

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

Merged
merged 8 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Illuminate/Concurrency/ForkDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ class ForkDriver implements Driver
*/
public function run(Closure|array $tasks): array
{
$tasks = Arr::wrap($tasks);

$keys = array_keys($tasks);
$values = array_values($tasks);

/** @phpstan-ignore class.notFound */
return Fork::new()->run(...Arr::wrap($tasks));
$results = Fork::new()->run(...$values);

return array_combine($keys, $results);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Concurrency/ProcessDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public function run(Closure|array $tasks): array
$command = Application::formatCommandString('invoke-serialized-closure');

$results = $this->processFactory->pool(function (Pool $pool) use ($tasks, $command) {
foreach (Arr::wrap($tasks) as $task) {
$pool->path(base_path())->env([
foreach (Arr::wrap($tasks) as $key => $task) {
$pool->as($key)->path(base_path())->env([
'LARAVEL_INVOKABLE_CLOSURE' => serialize(new SerializableClosure($task)),
])->command($command);
}
})->start()->wait();

return $results->collect()->map(function ($result) {
return $results->collect()->mapWithKeys(function ($result, $key) {
$result = json_decode($result->output(), true);

if (! $result['successful']) {
Expand All @@ -47,7 +47,7 @@ public function run(Closure|array $tasks): array
);
}

return unserialize($result['result']);
return [$key => unserialize($result['result'])];
})->all();
}

Expand Down
36 changes: 35 additions & 1 deletion tests/Integration/Concurrency/ConcurrencyTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace Illuminate\Tests\Integration\Console;
namespace Illuminate\Tests\Integration\Concurrency;

use Illuminate\Support\Facades\Concurrency;
use Orchestra\Testbench\TestCase;
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;

Expand Down Expand Up @@ -36,4 +37,37 @@ public function testWorkCanBeDistributed()
$this->assertEquals(2, $first);
$this->assertEquals(4, $second);
}

public function testOutputIsMappedToArrayInput()
{
$input = [
'first' => fn () => 1 + 1,
'second' => fn () => 2 + 2,
];

$processOutput = Concurrency::driver('process')->run($input);

$this->assertIsArray($processOutput);
$this->assertArrayHasKey('first', $processOutput);
$this->assertArrayHasKey('second', $processOutput);

$syncOutput = Concurrency::driver('sync')->run($input);

$this->assertIsArray($syncOutput);
$this->assertArrayHasKey('first', $syncOutput);
$this->assertArrayHasKey('second', $syncOutput);

/** As of now, the spatie/fork package is not included by default.
* $forkOutput = Concurrency::driver('fork')->run([
* 'first' => fn() => 1 + 1,
* 'second' => fn() => 2 + 2,
* ]);.
*
* $this->assertIsArray($forkOutput);
* $this->assertArrayHasKey('first', $forkOutput);
* $this->assertArrayHasKey('second', $forkOutput);
* $this->assertEquals(2, $forkOutput['first']);
* $this->assertEquals(4, $forkOutput['second']);
*/
}
}