I have a couple of rake tasks run in the background using RyanB’s technique from Railscast 127, which work fine locally and on Rails 3, but not on 3.1 in production on Heroku’s cedar stack. Before I rip them out and replace them with delayed_job, should this method of forking a process still work?
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
Answering this myself, after digging around: the system bit does but not the &. Delayed_job is the way to go on this. FWIW here’s my working code to run a rake task in a rails 3.1 app using delayed_job on Heroku’s Cedar stack, to generate an XML file, save it to temp then upload to S3. The XML output file is large, hence the need to handle it asynchronously.
app/classes/callrake.rb:
isbns controller:
slightly off-topic config/locales/en.yml for completeness:
rake task:
Then Onixarchive has a regular paperclip attachment set-up in the model.
Note the Rails.root.public in the filepath – I kept getting a “does not exist” when I tried to write to app/tmp, because, sure enough when I looked using heroku’s console, there is no tmp folder. I suppose I could have created one, but this app is on cedar, which has an ephemeral filesystem so you can write anywhere for the duration of the session.