Topic: Deleting all entries by date

I just want to ask what could be the best approach here.

SCENARIO:

I have entries to the database called "queue" that is time based. Every now and then I would need to remove old  and non-usable entries on my database. I can delete it by calling a function every time the page loads but I'm afraid that it slow down the application.

Where do you think would be the right place to call the code that deletes it? Or is there a proper way to approach this? Thanks in advance.

Re: Deleting all entries by date

Write a rake task to clean up the old data and use a cron job set to run at whatever intervals/time you desire to call the rake task

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Deleting all entries by date

thanks for replying jamesw, I actually read a same scenarios and everyone was advising the same thing (doing a cron job), but isn't there a way to do it inside the app? just wondering.

Thanks again.

Re: Deleting all entries by date

your rake task is "inside the app" What's the issue with using a cron job?

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Deleting all entries by date

It is easy with rufus-scheduler gem:
http://rufus.rubyforge.org/rufus-scheduler/

I already used it by creating a ruby file inside config/initializers folder:

require 'rufus/scheduler'
require 'rubygems'

scheduler = Rufus::Scheduler.start_new

#scheduled for every day at 06h00am
=begin
* * * * * *
| | | | | |
| | | | | +-- Year              (range: 1900-3000)
| | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month  (range: 1-31)
| +---------- Hour              (range: 0-23)
+------------ Minute            (range: 0-59)

see http://www.nncron.ru/help/EN/working/cron-format.htm for more details and examples
=end


#if log file size > 1 Mb its content will be erased
scheduler.cron '00 06 * * 7' do
  log_file = File.open("#{RAILS_ROOT}/log/#{ENV['RAILS_ENV']}.log", "w")
  if File.exist?(log_file) && File.size(log_file) > 1.megabytes
    File.truncate(log_file.trunc, 0)
  end
end

It's up to you to adapt the task. smile

Re: Deleting all entries by date

hi thanks for your information