从Laravel Jobs返回数据 [英] Return data from Laravel Jobs

查看:536
本文介绍了从Laravel Jobs返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel上为移动应用程序开发API.

方法 将向其他API发出请求 ,合并和过滤数据,更改其结构等.

对应用程序的要求之一是响应时间不超过30秒,或者完全不响应.因此,我必须尽可能多地重复请求.我试图通过Laravel Queues意识到这一点,目前在Job类中有类似的东西:

private $apiActionName;

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

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

此操作代码在控制器中:

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

是的,我知道从工作中返回价值不是一个好主意,但是希望这是可能的.但是 $ this-> dispatch()仅返回排队的作业ID,而不返回 handle 方法的结果.

TL; DR::如何从排队的Job中返回数据,而不将其保存在任何位置,即使队列中有多次尝试?如果乔布斯不适合这样做,也许有人知道其他方式.任何建议将不胜感激.

提前谢谢!

解决方案

您正在返回Job类中的数据,但是将$ data分配给调度程序-请注意,dispatch()方法不是Job类的一部分. /p>

假设您的作业同步运行,您可以尝试执行以下操作:

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;
}

然后在您的控制器中:

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

很明显,一旦您进入异步模式并排队,此功能将无法正常工作-到调用getResponse()时,响应还没有到达那里.但这就是异步作业的全部目的:)

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!

解决方案

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

这篇关于从Laravel Jobs返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆