Rails - 多线程避免松弛 3 秒 API 响应规则
Rails - multiple theads to avoid the slack 3 second API response rule
我正在工作 API。我的脚本进行了大量外部处理,在某些情况下可能需要大约 3-6 秒。发生的事情是 Slack API 期望在 3 秒内收到 200 个响应,因为我的功能没有在 3 秒内完成,它再次重试,然后它最终发布相同的自动响应 2-3 次。
我通过注释掉所有函数来确认这一点,我没有问题,它发布了对 slack fine 的响应。然后我添加了睡眠 10,它做了 3 次相同的响应,所以不同的是它花了更长的时间。
根据我的阅读,我需要有线程响应。然后我需要首先响应线程 1 中的松弛 API,然后开始处理我的函数。
这是我尝试过的:
def events
Thread.new do
json = {
"text": "Here is your 200 response immediately slack",
}
render(json: json)
end
puts "--------------------------------Json response started----------------------"
sleep 30
puts "--------------------------------Json response completed----------------------"
puts "this is a successful response"
end
当我测试它时,同样的问题发生了,所以我尝试使用在线 API 测试仪,它点击了页面,等待 30 秒,然后 returns 200 响应,但我需要它响应立即处理 200,然后处理其余部分,否则我会重复。
我是否正确使用了线程,或者是否有其他方法可以绕过这个 Slack API 3 秒响应限制?我对 rails 和 slack API 都是新手,所以在这里有点迷路。
欣赏眼睛:)
如果您不需要在响应中使用代码的结果,我建议使用 ActionJob 到 运行 后台代码。首先,通过 运行ning 创建一个 ActiveJob 作业:
bin/rails generate job do_stuff
然后打开在 app/jobs/do_stuff_job.rb
中创建的文件并编辑 #perform
函数以包含您的代码(因此您示例中的 puts
语句和 sleep 30
) .最后,您可以从控制器操作中调用 DoStuff.perform_later
,您的作业将在后台 运行!您的最终控制器操作将如下所示:
def events
DoStuff.perform_later # this schedules DoStuff to be done later, in
# the background, so it will return immediately
# and continue to the next line.
json = {
"text": "Here is your 200 response immediately slack",
}
render(json: json)
end
As an aside, I'd highly recommend never using Thread.new
in rails. It can create some really confusing behavior especially in test scripts for a number of reasons, but usually because of how it interacts with open connections and specifically ActiveRecord.
我正在工作 API。我的脚本进行了大量外部处理,在某些情况下可能需要大约 3-6 秒。发生的事情是 Slack API 期望在 3 秒内收到 200 个响应,因为我的功能没有在 3 秒内完成,它再次重试,然后它最终发布相同的自动响应 2-3 次。
我通过注释掉所有函数来确认这一点,我没有问题,它发布了对 slack fine 的响应。然后我添加了睡眠 10,它做了 3 次相同的响应,所以不同的是它花了更长的时间。
根据我的阅读,我需要有线程响应。然后我需要首先响应线程 1 中的松弛 API,然后开始处理我的函数。
这是我尝试过的:
def events
Thread.new do
json = {
"text": "Here is your 200 response immediately slack",
}
render(json: json)
end
puts "--------------------------------Json response started----------------------"
sleep 30
puts "--------------------------------Json response completed----------------------"
puts "this is a successful response"
end
当我测试它时,同样的问题发生了,所以我尝试使用在线 API 测试仪,它点击了页面,等待 30 秒,然后 returns 200 响应,但我需要它响应立即处理 200,然后处理其余部分,否则我会重复。
我是否正确使用了线程,或者是否有其他方法可以绕过这个 Slack API 3 秒响应限制?我对 rails 和 slack API 都是新手,所以在这里有点迷路。
欣赏眼睛:)
如果您不需要在响应中使用代码的结果,我建议使用 ActionJob 到 运行 后台代码。首先,通过 运行ning 创建一个 ActiveJob 作业:
bin/rails generate job do_stuff
然后打开在 app/jobs/do_stuff_job.rb
中创建的文件并编辑 #perform
函数以包含您的代码(因此您示例中的 puts
语句和 sleep 30
) .最后,您可以从控制器操作中调用 DoStuff.perform_later
,您的作业将在后台 运行!您的最终控制器操作将如下所示:
def events
DoStuff.perform_later # this schedules DoStuff to be done later, in
# the background, so it will return immediately
# and continue to the next line.
json = {
"text": "Here is your 200 response immediately slack",
}
render(json: json)
end
As an aside, I'd highly recommend never using
Thread.new
in rails. It can create some really confusing behavior especially in test scripts for a number of reasons, but usually because of how it interacts with open connections and specifically ActiveRecord.