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?
-
check this screencast. railscasts.com/episodes/164-cron-in-rubyHungryCoder– HungryCoder2012-11-01 03:58:47 +00:00Commented Nov 1, 2012 at 3:58
Add a comment
|
1 Answer
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
3 Comments
JJ Beck
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?antiqe
not sure if I understand you correctly? try after_filter delete_file_after_file_send
antiqe
by another words, to send file to user use send_file method instead of giving permanent link to user