vuongxuongminh / laravel-async

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

Job doing nothing, and response of task execution is always null #28

Closed bogdan-dubyk closed 1 year ago

bogdan-dubyk commented 2 years ago

I have a task like this:

class MyJob
{
    use Invocation;
    use SerializesModels;

    protected array $product;

    public function __construct(array $product) {
        $this->product = $product;
    }

    public function handle()
    {
        $deletedProduct = PermanentlyDeletedProduct::createFromProductArray($productData);
        $saved = $deletedProduct->save()

        return $saved ? 'saved' : 'failed';
    }
}

and in my code I'm running it like this:

foreach ($productsData as $product) {
    AsyncFacade::run(
        new MyJob($product),
        [
             'success' => fn($output) => Log::info('success ',['output' => $output,]),
             'timeout' => fn($output) => Log::info('timeout'),
             'error' => fn (\Throwable $exception) => Log::info('error', ['output' => $exception,]),
        ]
    );
}
AsyncFacade::wait();

and it does not work, I'm getting the message success in the logs but output is null, and nothing is inserted into the database. What I'm doing wrong???? Maybe the issue is in mogo? I tried to use a function before instead of the job and it always failed with the error that mongo driver can't be serialized, with a job I do not see that error but also it's not working :)

bogdan-dubyk commented 2 years ago

and I have pcntl installed and enabled, when I run php -i | grep pcntl it shows support => enabled , and when add extension_loaded('pcntl') it returns true

bogdan-dubyk commented 2 years ago

hmm, and honestly I'm not sure if async execution working for me in general

        $startTime = microtime(true);
        AsyncFacade::run(function(){sleep(1); return 1;});
        AsyncFacade::run(function(){sleep(1); return 1;});
        AsyncFacade::wait();

        dd(microtime(true) -  $startTime);

execution time is more than 3 seconds

vntrungld commented 1 year ago

I'm facing this problem too, do we have a solution for this?

vuongxuongminh commented 1 year ago

Please try the latest version and make sure your PHP extensions have POSIX and PCNTL.