Bushikot
Bushikot

Reputation: 797

Return data from Laravel Jobs

TL;DR: How can I return data from a queued Job without saving it anywhere and handling the situation when the job might run more than once because of retries? Or is there another way if Jobs are not suitable for this?


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.

Upvotes: 20

Views: 37565

Answers (4)

Khalaf Nasirov
Khalaf Nasirov

Reputation: 1

For SYNC mode, comment the use Queueable

$result = dispatch_sync(new Job());

For ASYNC mode you can't directly assign result to any variable BUT you can use livewire and event broadcasting to analyze background process and show results. It is easy to setup just check few docs and you're ready to go!

Upvotes: 0

Wesley Gonçalves
Wesley Gonçalves

Reputation: 2305

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.

Upvotes: 9

data one
data one

Reputation: 103

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() . ')'
                 );
   });
}

Upvotes: 5

Denis Mysenko
Denis Mysenko

Reputation: 6534

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 :)

Upvotes: 16

Related Questions