回答這個我自己,周圍挖後:系統位做,但不是&。 Delayed_job是繼續下去的方法。 FWIW這裏是我的工作代碼,用於在Heroku的Cedar堆棧上使用delayed_job在rails 3.1應用程序中運行rake任務,以生成XML文件,將其保存爲temp,然後上傳到S3。 XML輸出文件很大,因此需要異步處理它。
應用程序/類/ callrake.rb:
class Callrake
def call_rake(task, options = {})
options[:rails_env] ||= Rails.env
args = options.map { |n, v| "#{n.to_s.upcase}='#{v}'" }
system "rake #{task} #{args.join(' ')} --trace 2>&1 >> #{Rails.root}/log/rake.log"
end
handle_asynchronously :call_rake
end
的ISBN控制器:
def onixtwo
#using Ransack, successor to Metasearch
@q = Isbn.where(:client_id => current_user.client_id).search(params[:q])
@isbns = @q.result(:distinct => true)
if @q.nil?
@isbns = Isbn.where(:client_id => current_user.client_id)
end
#I have an array to pass so this is a bit hacky - make an array, pass it as a string then turn it back into an array using eval()
is = []
@isbns_to_pass = @isbns.each {|isbn| is << isbn.id }
@client = current_user.client_id
@user = current_user.id
callrake = Callrake.new
callrake.call_rake(:onixtwo, :isbns => is, :client => @client, :user => @user)
redirect_to isbns_path, :flash => { :notice => t(:isbnonixtwo).html_safe }
end
稍微偏離主題配置/語言環境/出於完整性en.yml:
en:
isbnonixtwo: "Onix 2.1 message is generating. When it's done, you can download it from the <a href='/onixarchives'>Onix Archive</a> list."
耙任務:
desc "Generate an onix 2.1 xml file"
task :onixtwo => :environment do |t|
client_id = ENV["CLIENT"]
user_id = ENV["USER"]
isbns_passed = ENV["ISBNS"]
isbnsarray = eval(isbns_passed)
filename = "#{Rails.root}/public/#{Client.find_by_id(client_id).client_name}-#{Date.today}-#{Time.now}-onix-21.xml"
isbns = Isbn.find_all_by_id(isbnsarray)
File.open(filename, "w") do |file|
xml = Builder::XmlMarkup.new(:target => file, :indent => 2)
xml.instruct!(:xml, :version => "1.0", :encoding => "utf-8")
xml.declare! :DOCTYPE, :ONIXMessage, :SYSTEM, "http://www.editeur.org/onix/2.1/03/reference/onix-international.dtd"
xml.ONIXMessage do
xml.Header do
#masses of code
end #of file
xmlfile = File.open(filename, "r")
onx = Onixarchive.new(:client_id => client_id, :user_id => user_id)
onx.xml = xmlfile
onx.save!
end #of task
然後,Onixarchive在模型中設置了常規回形針附件設置。
請注意文件路徑中的Rails.root.public - 當我嘗試寫入app/tmp時,我不斷收到「不存在」,因爲當我使用heroku的控制檯查看時,沒有tmp文件夾。我想我可以創建一個,但這個應用程序是雪松,它有一個ephemeral filesystem,所以你可以在會話期間寫任何地方。