I have an administrative task that I need to run once a day: I need to iterate through each user in the User table in the database, do some calculation involving some other models, and update each user record in the database. I am thinking about just doing it at the console (env=production). But I don''t want to type all the statements again each time I carry out this task. Is there a way to put this in a script and just run the script? Eventually, I''d like to schedule the task to run automatically every day, and have some sort of progress display when the task is run. Thanks, Vincent. -- 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.
On Tue, Jan 26, 2010 at 8:47 PM, Vincent P <easebus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have an administrative task that I need to run once a day: I need > to iterate through each user in the User table in the database, do > some calculation involving some other models, and update each user > record in the database. > > I am thinking about just doing it at the console (env=production). > But I don''t want to type all the statements again each time I carry > out this task. Is there a way to put this in a script and just run > the script? > > Eventually, I''d like to schedule the task to run automatically every > day, and have some sort of progress display when the task is run. > > Thanks, > > Vincent. > >Vincent, you might want to consider the whenver gem and you can view Ryan''s screencast here: http://media.railscasts.com/videos/164_cron_in_ruby.mov Good luck, -Conrad> -- > 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.
You want to write a rake task that runs as a cron task every day. lib/tasks/my_task.rake task :touch_every_user => :environment do User.find_each {|user| user.touch } # perform some other heavy calculation and save it back to the user end For the cron task: go to server, type "crontab -e" 15 0* * * /path/to/rails/root rake touch_every_user RAILS_ENV=production On Jan 26, 8:47 pm, Vincent P <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have an administrative task that I need to run once a day: I need > to iterate through each user in the User table in the database, do > some calculation involving some other models, and update each user > record in the database. > > I am thinking about just doing it at the console (env=production). > But I don''t want to type all the statements again each time I carry > out this task. Is there a way to put this in a script and just run > the script? > > Eventually, I''d like to schedule the task to run automatically every > day, and have some sort of progress display when the task is run. > > Thanks, > > Vincent.-- 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.
On Tue, Jan 26, 2010 at 9:06 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, Jan 26, 2010 at 8:47 PM, Vincent P <easebus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I have an administrative task that I need to run once a day: I need >> to iterate through each user in the User table in the database, do >> some calculation involving some other models, and update each user >> record in the database. >> >> I am thinking about just doing it at the console (env=production). >> But I don''t want to type all the statements again each time I carry >> out this task. Is there a way to put this in a script and just run >> the script? >> >> Eventually, I''d like to schedule the task to run automatically every >> day, and have some sort of progress display when the task is run. >> >> Thanks, >> >> Vincent. >> >> > Vincent, you might want to consider the whenver gem and you > can view Ryan''s screencast here: > > http://media.railscasts.com/videos/164_cron_in_ruby.mov > > Good luck, > > -Conrad >Vincent, you''ll install the gem as follows: sudo gem install whenever -Conrad> > >> -- >> >> 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.
On Tue, Jan 26, 2010 at 9:07 PM, David <furberd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You want to write a rake task that runs as a cron task every day. > > lib/tasks/my_task.rake > > task :touch_every_user => :environment do > User.find_each {|user| user.touch } > # perform some other heavy calculation and save it back to the user > end > > For the cron task: > go to server, type "crontab -e" > 15 0* * * /path/to/rails/root rake touch_every_user > RAILS_ENV=production >The Ruby way would be to do the following: every :day , :at => "6:00 pm" do runner "User.all.each { |user| <do_some_work> }" end Good luck, -Conrad> On Jan 26, 8:47 pm, Vincent P <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I have an administrative task that I need to run once a day: I need > > to iterate through each user in the User table in the database, do > > some calculation involving some other models, and update each user > > record in the database. > > > > I am thinking about just doing it at the console (env=production). > > But I don''t want to type all the statements again each time I carry > > out this task. Is there a way to put this in a script and just run > > the script? > > > > Eventually, I''d like to schedule the task to run automatically every > > day, and have some sort of progress display when the task is run. > > > > Thanks, > > > > Vincent. > > -- > 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.
Thank you! This is exactly what I need. On Jan 26, 6:18 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, Jan 26, 2010 at 9:07 PM, David <furb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > You want to write a rake task that runs as a cron task every day. > > > lib/tasks/my_task.rake > > > task :touch_every_user => :environment do > > User.find_each {|user| user.touch } > > # perform some other heavy calculation and save it back to the user > > end > > > For the cron task: > > go to server, type "crontab -e" > > 15 0* * * /path/to/rails/root rake touch_every_user > > RAILS_ENV=production > > The Ruby way would be to do the following: > > every :day , :at => "6:00 pm" do > runner "User.all.each { |user| <do_some_work> }" > end > > Good luck, > > -Conrad > > > > > On Jan 26, 8:47 pm, Vincent P <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I have an administrative task that I need to run once a day: I need > > > to iterate through each user in the User table in the database, do > > > some calculation involving some other models, and update each user > > > record in the database. > > > > I am thinking about just doing it at the console (env=production). > > > But I don''t want to type all the statements again each time I carry > > > out this task. Is there a way to put this in a script and just run > > > the script? > > > > Eventually, I''d like to schedule the task to run automatically every > > > day, and have some sort of progress display when the task is run. > > > > Thanks, > > > > Vincent. > > > -- > > 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%2Bunsubscrib e@googlegroups.com> > > . > > 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Learn by Doing wrote:> Thank you! This is exactly what I need.(Please quote properly when replying.) You will also want to use ar-extensions so that you''re not doing a separate query for each User you update. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Vincent, To answer your initial idea, .... Yes, you can create a script, using the same calls you would make in console, and run them using ./script/ runner. I do this all the time for various scripting tasks for projects. One of the great features of rails. You could do something like: ###### create the script to perform the updates: $ cat ./script/update_users.runnable puts "begin updating users: env=#{RAILS_ENV}" begin User.transaction do User.find(:all).each do |u| # perform calcs, update u, ..... end end puts "done." rescue Exception=>e #.... end ###### to run it against your dev env for testing: $ ./script/runner ./script/update_users.runnable begin updating users: env=development ... ###### to run it against your prod env: $ ./script/runner -e production ./script/update_users.runnable begin updating users: env=production ... As for scheduling the task, you could use cron (or the windows equiv if running under windows) to run the script at some particular scheduled time/interval. As for progress, you can always add logging calls in your script (and likely a command-line arg as to whether or not to show debug info to stdout, like the puts call in example above), either to the env''s existing log, or depending on your needs/wants, to a separate log file for the particular task at hand (ie ./log/ development_update_users.log ?), that you could check to see how the process is going, how it went, errors encountered, etc. Jeff On Jan 26, 5:47 pm, Vincent P <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have an administrative task that I need to run once a day: I need > to iterate through each user in the User table in the database, do > some calculation involving some other models, and update each user > record in the database. > > I am thinking about just doing it at the console (env=production). > But I don''t want to type all the statements again each time I carry > out this task. Is there a way to put this in a script and just run > the script? > > Eventually, I''d like to schedule the task to run automatically every > day, and have some sort of progress display when the task is run. > > Thanks, > > Vincent.-- 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.