Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return data from Laravel Jobs

I am developing API on Laravel for mobile application.

Methods will make requests to other API's, combine and filter data, changing it's structure etc.

One of the requirements to app is to respond no more than 30 seconds, or not respond at all. So, I have to repeat requests as much as I have time. I trying to realize that with Laravel Queues, and currently have something like that in my Job class:

private $apiActionName;

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

public function handle(SomeService $someService)
{
    return $someService->{$this->apiActionName}();
}

And this action code in controller:

public function someAction()
{ 
    $data = $this->dispatch(new MyJob($apiActionName));
    return response()->json($data);
}

Yes, I know it is bad idea to return value from job, but expect that it's possible. However $this->dispatch() returns only queued job ID, not result of handle method.

TL;DR: How can I return data from queued Job, without saving it anywhere, and even if it have more than one tries in the queue? Maybe somebody know other ways if Jobs are not suitable for this. Any advice will be appreciated.

Thanks in advance!

like image 531
Bushikot Avatar asked May 19 '16 20:05

Bushikot


3 Answers

You are returning data in your Job class, but assigning $data to a dispatcher - note that dispatch() method is not a part of your Job class.

You could try something like this, assuming that your jobs run synchronously:

private $apiActionName;
private $response;

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

public function handle(SomeService $someService)
{
    $this->response = $someService->{$this->apiActionName}();
}

public function getResponse()
{
    return $this->response;
}

And then in your controller:

public function someAction()
{ 
    $job = new MyJob($apiActionName);
    $data = $this->dispatch($job);
    return response()->json($job->getResponse());
}

Obviously, this won't work once you move to async mode and queues - response won't be there yet by the time you call getResponse(). But that's the whole purpose of async jobs :)

like image 51
Denis Mysenko Avatar answered Nov 16 '22 05:11

Denis Mysenko


Laravel 7 and prior

If you are using a queue driver other than sync and you're using Laravel version 7 or prior, you can use the @Denis Mysenko approach and the method dispatchNow to get the data from the Job object that it works well.

Laravel 8

However, in Laravel version 8, the dispatchNow method was deprecated in favor of dispatchSync. But, this new method creates a new instance of the Job and you won't be able to access this new instance or any of its properties from the client script.

Solution

But, accordingly to the rodrigo.pedra's answer, if you remove the Illuminate\Bus\Queueable trait from your job, you will be able to return the data from your job handle method and use this result in your client script.

if the job does not use the Queueable trait, the return of the handle method will be available to the request when using dispatchSync - see this answer.

<?php
// ...

class MyJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, SerializesModels;
    // use Queueable
    /* do not use ^^ this trait */

    private $apiActionName;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($apiActionName) 
    {
        $this->apiActionName = $apiActionName;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle() 
    {
        // operations generating result
        $result = $someService->{$this->apiActionName}();
 
        return $result;
    }
}

in the client script

public function someAction()
{ 
    $result = MyJob::dispatchSync($apiActionName);
    return response()->json($result);
}

Note: the dispatchSync method is the same as using dispatch()->onQueue('sync') which will force the Queue system to use the sync driver, running the job immediately.

like image 37
Wesley Gonçalves Avatar answered Nov 16 '22 06:11

Wesley Gonçalves


If you want return data from Laravel jobs, you need write some Queue methods at Providers/AppServiceProvider.php inside boot method (Laravel 7.x / 8)

public function boot()
{
   Queue::before(function ( JobProcessing $event ) {
      Log::info('Job ready: ' . $event->job->resolveName());
      Log::info('Job started: ' . $event->job->resolveName());
   });
    
   Queue::after(function ( JobProcessed $event ) {
      Log::notice('Job done: ' . $event->job->resolveName());
      Log::notice('Job payload: ' . print_r($event->job->payload(), true));
   });
    
   Queue::failing(function ( JobFailed $event ) {
       Log::error('Job failed: ' . 
                  $event->job->resolveName() . 
                 '(' . $event->exception->getMessage() . ')'
                 );
   });
}
like image 1
data one Avatar answered Nov 16 '22 06:11

data one