3

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?

1

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, antiqe. What I want to do is for the controller to download the file using wget, send the file to the user (in effect, let the user download the file), and then delete the file from server. Can I do it without having to set timeout?
not sure if I understand you correctly? try after_filter delete_file_after_file_send
by another words, to send file to user use send_file method instead of giving permanent link to user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.