0

Sorry for what seems like a basic question, but I'm stumped.

I have a Rails app that relies in part on third party data that I download periodically (generally daily) and integrate into the app's database. The Ruby code that I use to get the third party data is in separate Ruby files, i.e., not integrated as code in any of my controllers. So, I run these programs when needed via rails runner program.rb (probably not relevant, but all of them use the mechanize gem in gathering the third party data).

I want to try using Heroku's scheduler to make my data gathering more automated, and the recommendation for doing this is to set up rake tasks. Is there a rake task equivalent for rails runner program.rb?

Thanks

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
dugq
  • 63
  • 7
  • Related: http://stackoverflow.com/questions/591503/i-have-a-rails-task-should-i-use-script-runner-or-rake – MurifoX Feb 06 '14 at 23:53

1 Answers1

0

As far as converting a rails command into a rake task it's pretty easy, you just namespace it like this:

# /lib/tasks/my_task.rake
namespace :mytask do

  desc 'the description of your task'
  task :my_task => :environment do
    puts 'my task was executed'
  end
end

Then you'd call it with 'rake mytask:my_task' later on, which you should be able to do with Heroku's scheduler. Also I'd like to recommend the 'whenever' gem for setting things up on a schedule as I have no experience with Heroku but have found that gem to be fantastic.

Lethjakman
  • 997
  • 9
  • 15
  • My confusion is with how to execute the necessary code from the Ruby script without putting it all in the rake file. Is there a way to do this? Where you have "puts 'my task was executed'" I want to have my Ruby script execute . . . so, if it's in "program.rb" how do I reference it/get it to execute from within the rake file? – dugq Feb 07 '14 at 22:12
  • Ahhh ok. You're probably thinking of a "system" call. You'd do that as system('program.rb'). You also have the (better) option to either load or require it like: load('myfile.rb') or require('./my_file.rb') However I would recommend putting it in you rake task so it could be checked into your version control with the rest of your website. Possibly including a few extra libraries in /lib if you'd like to just call some classes in the rake task. – Lethjakman Feb 07 '14 at 23:56