1

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

Marcos Placona
  • 21,468
  • 11
  • 68
  • 93

2 Answers2

1

There was a quite interesting article about a year ago:

Ruby Daemons: Verifying Good Behavior

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
0

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
  • 19,037
  • 12
  • 60
  • 93