ct9a
2010-Apr-06 07:32 UTC
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Andy Jeffries
2010-Apr-06 09:21 UTC
Re: ruby script - how to write one that deals with different environment?
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 Jeffries http://andyjeffries.co.uk/ #rubyonrails #mysql #jquery Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS Company number: 5452840 On 6 April 2010 08:32, ct9a <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Peter Hickman
2010-Apr-06 09:37 UTC
Re: ruby script - how to write one that deals with different environment?
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gordon Yeong
2010-Apr-06 09:40 UTC
Re: ruby script - how to write one that deals with different environment?
Cool but how abt accessing the models? Andy gave a solution but i want to see what others think On 4/6/10, Peter Hickman <peterhickman386-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 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. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Sent from my mobile device Regards, Gordon Yeong 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 Yeong -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Andy Jeffries
2010-Apr-06 09:53 UTC
Re: ruby script - how to write one that deals with different environment?
On 6 April 2010 10:40, Gordon Yeong <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Peter Hickman
2010-Apr-06 09:55 UTC
Re: ruby script - how to write one that deals with different environment?
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gordon Yeong
2010-Apr-06 10:55 UTC
Re: ruby script - how to write one that deals with different environment?
hi, Andy and Peter, Thanks for the feedback :) i will try it out :) Regards, Gordon Yeong On 6 April 2010 19:55, Peter Hickman <peterhickman386-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 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. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Luke Pearce
2010-Apr-06 11:44 UTC
Re: ruby script - how to write one that deals with different environment?
> 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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gordon Yeong
2010-Apr-06 11:49 UTC
Re: ruby script - how to write one that deals with different environment?
guys, in addition to Peter''s reply, here''s a good reference for script/runner. On 6 April 2010 19:55, Peter Hickman <peterhickman386-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 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. > > -- >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
bseanvt
2010-Apr-06 13:50 UTC
Re: ruby script - how to write one that deals with different environment?
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. On Apr 6, 3:32 am, ct9a <anexi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ct9a
2010-Apr-08 05:49 UTC
Re: ruby script - how to write one that deals with different environment?
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ct9a
2010-Apr-08 05:50 UTC
Re: ruby script - how to write one that deals with different environment?
http://www.ameravant.com/posts/recurring-tasks-in-ruby-on-rails-using-runner-and-cron-jobs On Apr 6, 9:49 pm, Gordon Yeong <anexi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> guys, in addition to Peter''s reply, here''s a good reference for > script/runner. > > On 6 April 2010 19:55, Peter Hickman <peterhickman...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > 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. > > > ---- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.