Administrative task in Rails

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.

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 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

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 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

Thank you! This is exactly what I need.

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,

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