是否可以运行与Laravel同步工作的队列 [英] Is it possible to run queues working synchronously with Laravel

查看:291
本文介绍了是否可以运行与Laravel同步工作的队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个与Laravel中的许多工人同步通信的API系统.我使用Laravel 5.4,并且如果可能的话,希望在没有太多插件的情况下尽可能使用其功能.

I am trying to set up an API system that synchronously communicates with a number of workers in Laravel. I use Laravel 5.4 and, if possible, would like to use its functionality whenever possible without too many plugins.

我想到的是两台服务器.第一个具有Laravel实例的应用程序-称为APP-接收和回答来自用户的请求.第二个运行不同的工人,每个工人一个Laravel实例.这是我看到工作流程的方式:

What I had in mind are two servers. The first one with a Laravel instance – let’s call it APP – receiving and answering requests from and to a user. The second one runs different workers, each a Laravel instance. This is how I see the workflow:

  • APP收到用户的请求
  • APP将请求放入队列
  • 工人在队列中寻找工作,并最终找到工作.
  • 工人解决工作
  • 工作人员对APP或APP的答复发现工作可以通过某种方式解决
  • APP向用户发送响应

我的第一个想法是使用队列和beantalkd.问题在于这一切似乎都是异步工作的. APP是否有办法等待其中一个工作程序的结果?

My first idea was to work with queues and beanstalkd. The problem is that this all seem to work asynchronously. Is there a way for the APP to wait for the result of one of the workers?

经过更多研究后,我偶然发现了Guzzle.这是一个可行的方法吗?

After some more research I stumbled upon Guzzle. Would this be a way to go?

有关该项目的一些额外信息. 我说的是Restful API.例如.用户以" https://our.domain/article/1 "的形式发送请求及其标头中的API令牌.用户收到的是JSON格式的字符串,例如{"id":1,"name":"article_name"等}

Some extra info on the project. I am talking about a Restful API. E.g. a user sends a request in the form of "https://our.domain/article/1" and their API token in the header. What the user receives is a JSON formatted string like {"id":1,"name":"article_name",etc.}

使用两侧的原因是双重的.一方面,使用了不同的工人.另一方面,我们希望API的所有逻辑都尽可能安全.当进行黑客攻击时,只有APP端会受到威胁.

The reason for using two sides is twofold. At one hand there is the use of different workers. On the other hand we want all the logic of the API as secure as possible. When a hack attack is made, only the APP side would be compromised.

也许我在排队等所有事情上都变得很困难?如果您有更好的方法来达到相同的目的,那当然也会有所帮助.

Perhaps I am making things all to difficult with the queues and all that? If you have a better approach to meet the same ends, that would of course also help.

推荐答案

我知道您的问题是如何同步运行它,我认为您所面临的问题是在更新后无法更新第一个服务器工人完成了.您可以通过广播来实现这一目标.

I know your question was how you could run this synchronously, I think that the problem that you are facing is that you are not able to update the first server after the worker is done. The way you could achieve this is with broadcasting.

我在应用程序中对上传进行了类似的操作.我们使用Redis队列,但是beantalk将执行相同的工作.最重要的是,我们使用pusher,它使用用户可以订阅的套接字,并且看起来很棒.

I have done something similar with uploads in our application. We use a Redis queue but beanstalk will do the same job. On top of that we use pusher which the uses sockets that the user can subscribe to and it looks great.

  • 用户加载Web应用程序,并连接到推送器服务器
  • 用户上传文件(此时,您可以显示一些内容来告诉用户文件正在处理中)
  • 工作人员发现有一个文件
  • 工作人员处理文件
  • 工作完成或失败时的触发器和事件
  • 此事件广播到推送服务器
  • 由于用户正在监听推送服务器,因此会通过javascript接收事件
  • 您现在可以显示弹出窗口或使用javascript更新表格(即使用户已经离开导航也可以使用)

我们为此使用了pusher,但是您可以使用redis,beanstalk和许多其他解决方案来完成此任务.在Laravel文档中了解有关事件广播的信息.

We used pusher for this but you could use redis, beanstalk and many other solutions to do this. Read about Event Broadcasting in the Laravel documentation.

这篇关于是否可以运行与Laravel同步工作的队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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