我使用" Running Rails jobs in background threads"中描述的技术实现了异步文件处理:
def upload_images
begin
uploaded_io = params[:filetoupload]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
file.flush
job = Job.create(:status => 0, :file_name => uploaded_io.original_filename)
processing_job(job)
end
rescue => e
upload_log.error(e.to_s)
end
redirect_to '/admin/jobs'
end
def processing_job(job)
background do
# do processing
end
end
def background(&block)
Thread.new do
yield
ActiveRecord::Base.connection.close
end
end
但是在大约5分钟的工作后,线程停止处理数据:记录停止,CPU负载几乎为零。免费记忆也不是问题。
为什么会发生这种情况以及如何解决?
我使用Ruby 1.9.3p551,Rails 4.1.9,Phusion Passenger 4.0.59。