views:

72

answers:

2

Hi, I've written this ruby daemon, and was wondering if somebody could have a look at it, and tell me if the approach I've taken is correct.

#!/usr/bin/env ruby

require 'logger'  

# You might want to change this
ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/environment"

$running = true
Signal.trap("TERM") do 
  $running = false
end

service = Post.new('http://feed.com/feeds')
logger  = Logger.new('reader.log')

while($running) do
  # Log my calls
  logger.info "Run at #{Time.now}"

  service.update_from_feed_continuously
  # only run it every 5 minutes or so
  sleep 300
end

I feel like this last loop is not quite the right thing to do, and can be memory intensive, but I'm not sure. Also, the 5 minutes seem to never happen exactly every 5 minutes, and I'll see variations of 4-6 minutes.

thanks in advance

+1  A: 

The time discrepancy could come from how long service.update_from_feed_continuously takes. Is this a non-trivial computation or one that relies on a web service (their added delays could dwarf many client-side computations).

Not sure about the structure of the rest though, sorry!

Alex Mcp
That's exactly what I've guessed. thanks a lot
Marcos Placona
+1  A: 

There was a quite interesting article about a year ago:

Ruby Daemons: Verifying Good Behavior

Michael Kohl