Ruby script - how to write one that deals with different environment?

hi guys,

I would like to write a script that is runned by cron. It will update
multiple entries.

Any guides for this?
I can’t figure out how do we choose which ruby environment (ie.
development/test/production) in the script
and also, how do I access models in the script (which is also used by
my rails application)?

thanks

The easiest way may be to have a class with a class method, e.g.

class FooRunner
def self.run

end
end

And then call:

/path/to/my/app/script/runner -e production FooRunner.run

from Cron.

Cheers,

Andy


Andy J.
http://andyjeffries.co.uk/ #rubyonrails #mysql #jquery
Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS
Company number: 5452840

All you need is access to the RAILS_ENV constant. For example this
script

– x.rb–
puts RAILS_ENV

When run produces the following output:

$ ruby script/runner -e production x.rb
production
$ ruby script/runner -e development x.rb
development
$ ruby script/runner -e test x.rb
test

for any environment that you have defined.

On 6 April 2010 10:40, Gordon Y. [email protected] wrote:

Cool but how abt accessing the models? Andy gave a solution but i want
to see what others think

You should just be able to access the models normally (i.e. just use the
class, without needing to require anything) if you’re using
script/runner.

If you want to do it with a raw Ruby script (rather than using
script/runner), you’ll have to include RubyGems, ActiveRecord and point
ActiveRecord at the database.yml section, load all the models found
below
the specific part.

To be honest, it’s much easier just to use script/runner.

Cheers,

Andy

Cool but how abt accessing the models? Andy gave a solution but i want
to see what others think

On 4/6/10, Peter H. [email protected] wrote:

$ ruby script/runner -e development x.rb
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Sent from my mobile device

Regards,
Gordon Y.

In matters of style, swim with the current,
In matters of principle, stand like a rock.

  • Thomas Jefferson

Hope is tomorrow’s veneer over today’s disappointment.

  • Evan Esar

I never argue with idiots anymore. They have brought me down to their
levels and beaten me with their experience.

  • Gordon Y.

hi, Andy and Peter,
Thanks for the feedback :slight_smile:
i will try it out :slight_smile:

Regards,
Gordon Y.

If you use script/runner then just use them as you normal would.

Scripts run from script/runner run as normal rails application so all
the
same setup that the web application has will be provided for your
script.

guys, in addition to Peter’s reply, here’s a good reference for
script/runner.

If you use script/runner then just use them as you normal would.

Just out of curiosity couldn’t you just to a rake task instead and call
that from cron?

hi there, bseanvt,

The thought of using the rake task is good but it’s not applicable
to my situation whereby I am just running this script after each run
of thinking_sphinx:index which takes place every 20 mins.

If you set up a rake task in lib/tasks/your_cron_job.rake with
something like this

namespace :your_cron_job do
desc “describe the task”
task :every_five_minutes => :environment do
@post = Post.new
end
end

you can add it manually to the cron by typing ‘crontab -e’ which opens
the file for editing (*nix only)
crontab -e

first specify how often you want the job to run… in this case every
5 minutes “*/5 * * * *” along with correct paths to app and rake, set
the RAILS_ENV and task name to execute. Cron is just a file so you’ll
enter all the info for the job on one line like so… (if you don’t
have a default editor set up already it will prompt you to do so
before entering in this command).

*/5 * * * * cd /var/www/my_ror_application && /usr/bin/rake
RAILS_ENV=production your_cron_job:every_five_minutes

More info here
http://seanbehan.com/programming/set-cron-job-to-run-every-five-minutes-for-a-ruby-on-rails-rake-task/
There are more elegant approaches to this problem. This is just the
quick and dirty.

http://www.ameravant.com/posts/recurring-tasks-in-ruby-on-rails-using-runner-and-cron-jobs

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs