vuongxuongminh / laravel-async

Package provide simple way to run code asynchronously for your Laravel application.
MIT License
154 stars 26 forks source link

"exception": "Non-static method VXM\\Async\\Async::run() should not be called statically", #10

Closed valh1996 closed 4 years ago

valh1996 commented 4 years ago

Hello,

Whatever I do, your example gives me an error :

"exception": "Non-static method VXM\\Async\\Async::run() should not be called statically"

Here is my code based on your example :

public function index(Request $request)
{
    Async::run(function () {
        // Can I use a new instance of Laravel here?
        return 123;
    }, [
        'success' => function ($output) { 
            \Log::debug($output);
        },
        'timeout' => function () { 
            \Log::debug('timeout');
        },
        'error' => function (\Throwable $exception) {
            \Log::debug($exception->getMessage());
        },
    ]);
}

Do you have any idea please ?

Is it possible to use all the functions of Laravel in the child process please?

PS : Is it also possible to keep the child process open while the parent process has not been completed?

Thank you Val'

vuongxuongminh commented 4 years ago

Hi, Make sure you are using Async facade not call direct to VXM\Async\Async. Yes, you can call all functions of Laravel in child process.

valh1996 commented 4 years ago

Thanks! So I can use all the functions of Laravel inside the child process, but we agree that it is not shared with the parent process? I have a singleton that I would like to "duplicate" (not the same user id) in the child process to make it communicate with the one in the parent process. I don't know if it's clear

Here is my complete new code :

<?php

namespace App\Console\Commands;

use Async;
use VXM\Async\Invocation;

class MyTask
{
    use Invocation;

    public function handle()
    {
        return 1;
    }
}

class test extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Async::run(MyTask::class, [
            'success' => function ($output) {
                echo $output;
            },
            'error' => function (\Throwable $e) {
                var_dump($e->getMessage()); die;
            }
        ]);

        return true;
    }
}

I'm getting an empty error each time :

$: php artisan test
string(0) ""

Do you know why please? I can't get this piece of code to work :(

vuongxuongminh commented 4 years ago

Task should be PSR-4 named, can you rename and try again?