JJ Beck
JJ Beck

Reputation: 5283

Rails setting timeout in controller

In my controller in Rails, I want to download a file into the public folder of the project (using the Linux system command wget). Then I'd like the file to stay there for an hour, before calling the command rm to delete it. Can I set a timeout so that the code execution pauses at some point before resuming later?

Upvotes: 3

Views: 3064

Answers (1)

antiqe
antiqe

Reputation: 1124

You can not set time out for two reasons:
Firstly, the process will be killed by the web server timeout.
Second, even if you set the timeout is appropriate for your needs, the process responsible for the wait, will eat resources, and will not be available for use. To solve this problem you will have to fork another server process, do you realy need this?

But you can use https://github.com/jmettraux/rufus-scheduler

for example, in your controller

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
  ...

And some_code_to_rm_file will be started in one hour after you wget file

Upvotes: 3

Related Questions