我有以下流程:
upload big file and send token
|
V
save uploaded file temporary
|
V
response that file was correctly uploaded
|
V
callback that checks is file is valid
|
V
after validation make http request to defined address with token and
validation result, delete temporary file
在sinatra中实现这个的最佳方法是什么?
我找到的只有一个解决方案是使用类似这样的内容:sinatra_run_later
还有其他事情要处理这个案子吗?
答案 0 :(得分:0)
我用于这样的东西的一个模式是后台线程池。请注意,我使用的是webserver puma,它可能不适用于所有其他人。
这个想法是在响应之前的上传路由中,将代码添加到将进行验证和http请求的持久后台线程池中。我从gem thread获取的线程池。
最小例子:
require 'sinatra/base'
require 'thread/pool'
class Threadexample < Sinatra::Application
class << self; attr_accessor :pool end # thread pool as class variable
@pool = Thread.pool(2)
post '/upload' do
…
# right before responding
Threadexample::pool.process {
# Add code that does the validation and the request
}
end
end