我有一个简单的Sinatra应用程序,需要通过外部进程生成一个文件,将该文件发送到浏览器,最后从文件系统中删除该文件。 大致如下:
class MyApp < Sinatra::Base
get '/generate-file' do
# calls out to an external process,
# and returns the path to the generated file
file_path = generate_the_file()
# send the file to the browser
send_file(file_path)
# remove the generated file, so we don't
# completely fill up the filesystem.
File.delete(file_path)
# File.delete is never called.
end
end
然而,似乎send_file
调用会完成请求,并且其后的任何代码都不会运行。
是否有某种方法确保生成的文件在成功发送到浏览器后被清除?或者我需要使用cron作业定期运行清理脚本?