Rails在控制器中设置超时

时间:2012-11-01 03:43:06

标签: ruby-on-rails ruby-on-rails-3 controller timeout

在我的Rails控制器中,我想将文件下载到项目的public文件夹中(使用Linux系统命令wget)。然后我想让文件在那里停留一个小时,然后再调用命令rm来删除它。我可以设置超时,以便代码执行在某个时刻暂停,然后再恢复吗?

1 个答案:

答案 0 :(得分:3)

您无法设置超时有两个原因:
首先,该过程将被Web服务器超时终止 其次,即使你设置的超时时间适合你的需要,负责等待的进程也会占用资源,而且无法使用。要解决这个问题,你必须分叉另一个服务器进程,你真的需要这个吗?

但您可以使用https://github.com/jmettraux/rufus-scheduler

例如,在您的控制器中

require 'rubygems'
require 'rufus/scheduler'

def download_using_wget
  ...
  if some_method_to_wget_file
    scheduler = Rufus::Scheduler.start_new

    scheduler.in '1h' do
      some_code_to_rm_file(file)
    end
  end
  ...

在wget文件

后一小时内将启动some_code_to_rm_file