[英]How to organize delayed results processing in microservices communication?
I have the following system: my Rails server issues commands to the Flask server and the latest one responses immediately with status 200. After that Flask server runs a background task with some time-consuming function.我有以下系统:我的 Rails 服务器向 Flask 服务器发出命令,最新的一个立即响应状态 200。之后 Flask 服务器运行具有一些耗时功能的后台任务。 After a little while, it comes up with some results and designed to send data back to the Rails server via HTTP (see diagram)过了一会儿,它得出了一些结果,旨在通过 HTTP 将数据发送回 Rails 服务器(见图)
Each Flask data portion can affect several Rails models ( User
, Post
etc...).每个 Flask 数据部分都可以影响多个 Rails 模型( User
、 Post
等...)。 Here I faced with two questions:在这里我面临两个问题:
This sounds like pretty much your standard webhook process.这听起来很像您的标准 webhook 过程。 Rails pokes Flask with a GET or POST request and Flask pokes back after a while. Rails 使用 GET 或 POST 请求向 Flask 发送请求,而 Flask 在一段时间后返回。
For example lets say we have reports, and after creating the report we need flask to verify the report:例如,假设我们有报告,在创建报告后,我们需要烧瓶来验证报告:
class ReportsController
# POST /reports
def create
@report = Report.new(report_params)
if @report.save
FlaskClient.new.verify(report) # this could be delegated to a background job
redirect_to @report
else
render :new
end
end
# PATCH /reports/:id/verify
def verify
# process request from flask
end
end
class FlaskClient
include Httparty
base_uri 'example.com/api'
format 'json'
def verify(report)
self.class.post('/somepath', data: { id: report.id, callback_url: "/reports/#{report.id}/verify", ... })
end
end
Of course the Rails app does not actually know when Flask will respond or that Flask and the background service are different.当然,Rails 应用程序实际上并不知道 Flask 什么时候会响应,或者 Flask 和后台服务是不同的。 It just sends and responds to http requests.它只是发送和响应 http 请求。 And you definitely don't want rails to wait around so save what you have and then later the hook can update the data.而且您绝对不希望 rails 等待,因此请保存您拥有的内容,然后挂钩可以更新数据。
If you have to update the UI on the Rails side without the user having to refresh manually you can use polling or websockets in the form of ActionCable.如果您必须在 Rails 端更新 UI 而无需用户手动刷新,您可以使用轮询或 ActionCable 形式的 websockets。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.