Is it possible to run queues working synchronously with Laravel

女生的网名这么多〃 提交于 2019-12-11 06:29:36

问题


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.

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 receives a request from user
  • APP puts request on a queue
  • Workers look for jobs on the queue and eventually finds one.
  • Worker resolves job
  • Worker responses to APP OR APP finds out somehow that job is resolved
  • APP sends response to user

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?

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

EDIT: 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.}

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.


回答1:


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.

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.

  • User loads the web app, connecting to the pusher server
  • User uploads file (at this point you could show something to tell the user that the file is processing)
  • Worker sees that there is a file
  • Worker processes file
  • Worker triggers and event when done or on fail
  • This event is broadcasted to the pusher server
  • Since the user is listening to the pusher server the event is received via javascript
  • You can now show a popup or update the table with javascript (works even if the user has navigated away)

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.



来源:https://stackoverflow.com/questions/42955635/is-it-possible-to-run-queues-working-synchronously-with-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!