How do I run Ruby tasks that use my Rails models?

后端 未结 9 1309
醉梦人生
醉梦人生 2021-02-01 08:58

I have a Rails app with some basic models. The website displays data retrieved from other sources. So I need to write a Ruby script that creates new instances in my database. I

相关标签:
9条回答
  • 2021-02-01 09:00

    Check out my answer in "A cron job for rails: best practices?".

    It contains two examples for using cron to run Rake tasks and class methods (via script/runner). In both cases, Rails is loaded and you can use your models.

    0 讨论(0)
  • 2021-02-01 09:09

    You can load the entire rails environment in any ruby script by simply requiring environment.rb:

    require "#{ENV['RAILS_ROOT']}/config/environment" 
    

    This assumes the RAILS_ROOT environment variable is set, see my comment for other ways of doing this.

    This has the added bonus of giving you all the nice classes and objects that you have in the rest of your rails code.

    To kick off your processes it sounds like cron will do what you want, and I would also add a task to your capistrano recipe that would add your script to the crontab to periodically get the data from the external source and update your DB. This can easily be done with the cronedit gem.

    The cron approach does have some drawbacks, mostly overhead and control, for other more sophisticated options see HowToRunBackgroundJobsInRails from the rails wiki.

    0 讨论(0)
  • 2021-02-01 09:17

    There are few steps to this and more details needed to really answer well.

    You say your site retrieves data from other sources? How often? If it is semi-regularly you definitely want to look into background processing/messaging. If it is frequently you really want to avoid loading your rails environment every time your script runs since you will be paying too high a startup tax each time.

    There are a multitude of options out there you will want to research. Reading about each of them, particularly reviews from people who post about why they made the choice they did, will give you a good feel for what questions you need to ask yourself before you make your choice. How big a job is loading the data? etc...

    Off the top of my head these are some of the things you may want to look into

    Script/Runner & Cron Background/RB Starling Workling MemcacheQ Beanstalk Background Job (Bj) delayed_job (Dj) Daemon Generator

    0 讨论(0)
  • 2021-02-01 09:18

    I'd suggest creating custom rake tasks (lib/task/foo.rake). This give you easy access to most of the functionality of your rails app.

    namespace :foo do
      desc 'do something cool'
      def something_cool
         test = MyModel.new
         test.name = 'test'
         test.save
      end
    end
    

    Then:

    $ rake -T foo
    rake foo:something_cool       # do something cool
    

    You can even run the tasks via a cronjob.

    0 讨论(0)
  • 2021-02-01 09:18

    Nice Joyent write up of using rake to run rails tasks from a cron job - http://wiki.joyent.com/accelerators:kb:rails:cron

    0 讨论(0)
  • 2021-02-01 09:23

    You can open a connection in your scripts as such:

    ActiveRecord::Base.establish_connection(
        :adapter  => "mysql",
        :username => "root",
        :host     => "localhost",
        :password => "******",
        :database => "******" 
    )
    

    I'm sure there is a more elegant way to do it, so that it grabs the info from your database.yml.

    0 讨论(0)
提交回复
热议问题