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

Update concurrency.md #9893

Merged
merged 2 commits into from
Sep 13, 2024
Merged

Update concurrency.md #9893

merged 2 commits into from
Sep 13, 2024

Conversation

digitall-it
Copy link
Contributor

  • Add requirements of spatie/fork
  • Show example on how to switch drivers
  • Explain that the defer method will never run the tasks in the PHP CLI's context

- Add requirements of spatie/fork
- Show example on how to switch drivers
- Explain that the defer method will never run the tasks in the PHP CLI's context
@decadence
Copy link
Contributor

decadence commented Sep 13, 2024

Tasks will run in CLI but before Command exits, not in background

@digitall-it
Copy link
Contributor Author

@decadence I will have to create a minimum use case for this in the next minutes, but seems like tasks will not run at all if using the defer method in the CLI, only from the browser. In my project, tasks would simply never run. Allow me to check and report.

@digitall-it
Copy link
Contributor Author

@decadence uhm it seems like a bug in the defer method or something wrong in the way I use it.

The error I'm seeing is as follows:

Command "invoke-serialized-closure 2>&1 &" is not defined.

File is present at vendor/laravel/framework/src/Illuminate/Concurrency/Console/InvokeSerializedClosureCommand.php and executes fine, so it seems not be invokable from the context of the defer method somehow:

php artisan invoke-serialized-closure
{"successful":true,"result":"N;"}%

Maybe my fresh install is missing something. Here's the minimum use cases, it doesn't seem to work either in CLI or in a web page.

In routes/web.php:

<?php

use Illuminate\Support\Facades\Concurrency;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;

Route::get('/defer', function () {

    Concurrency::defer([
        fn () => Log::notice('Checking for concurrency defer issues at '.now()),
        fn () => Log::notice('Checking for concurrency defer issues at '.now()),
        fn () => Log::notice('Checking for concurrency defer issues at '.now()),
    ]);

    return view('welcome');
});

Route::get('/run', function () {

    Concurrency::run([
        fn () => Log::notice('Checking for concurrency run issues at '.now()),
        fn () => Log::notice('Checking for concurrency run issues at '.now()),
        fn () => Log::notice('Checking for concurrency run issues at '.now()),
    ]);

    return view('welcome');
});

then try navigating /run and /defer, the latter will log an error.

The same via CLI, in app/Console/Commands/ConcurrencyCheckDefer.php:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Concurrency;
use Illuminate\Support\Facades\Log;

class ConcurrencyCheckDefer extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:concurrency-check-defer';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Checks for concurrency defer method issues';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        Concurrency::defer([
            fn () => Log::notice('Checking for concurrency defer issues at '.now()),
            fn () => Log::notice('Checking for concurrency defer issues at '.now()),
            fn () => Log::notice('Checking for concurrency defer issues at '.now()),
        ]);
    }
}

while in app/Console/Commands/ConcurrencyCheckRun.php:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Concurrency;
use Illuminate\Support\Facades\Log;

class ConcurrencyCheckRun extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:concurrency-check-run';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Checks for concurrency run method issues';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        Concurrency::run([
            fn () => Log::notice('Checking for concurrency run issues at '.now()),
            fn () => Log::notice('Checking for concurrency run issues at '.now()),
            fn () => Log::notice('Checking for concurrency run issues at '.now()),
        ]);
    }
}

issuing php artisan app:concurrency-check-runwill run fine, while php artisan app:concurrency-check-defer will report the same problem. Here's my composer.json specific laravel framework line: "laravel/framework": "^11.9",, IDE says it's locked at v11.23.4. Ambient is macOS Sonoma 14.6.1 (23G93) and PHP 8.2.23 provided by Herd, fresh install.

@decadence
Copy link
Contributor

When I tried Concurrency::defer on existing Laravel 11 application I got error:

Illuminate\Contracts\Container\BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Support\MultipleInstanceManager

On fresh Laravel 11 app defer works in CLI but it just registers function for CommandFinished event so no code is executed in background after command actually. You can see it FoundationServiceProvider::registerDeferHandler method.

@digitall-it
Copy link
Contributor Author

@decadence did you issue a composer update before trying? Did you also try on a fresh installation? What is your Laravel version?

@decadence
Copy link
Contributor

@digitall-it
Of course I did. I tried on existing app (fail) and on the fresh app (works). Version is 11.23.2.

@digitall-it
Copy link
Contributor Author

@decadence sorry didn't read second part on the fresh installation. Latest Laravel version is 11.23.4. Try updating.

@decadence
Copy link
Contributor

Updated to 11.23.4. Same result.

@digitall-it
Copy link
Contributor Author

is it possible to post here the code that isn't working out for you? I fear yours is a serialization problem.

@taylorotwell taylorotwell merged commit ce657e9 into laravel:11.x Sep 13, 2024
@taylorotwell
Copy link
Member

Made adjustments.

@digitall-it digitall-it deleted the patch-1 branch September 13, 2024 17:49
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