cirandas.net

ref: master

app/services/feed_updater.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
# This class implements the feed updater. To change how often a feed gets
# updated, change FeedUpdater#update_interval in your config/local.rb file like
# this:
#
#   FeedUpdater.update_interval = 24.hours
#
# You can also customize the time between update runs setting
# FeedUpdater#daemon_sleep_interval. Give it an integer representing the number
# of seconds to wait between runs in your config/local.rb:
#
#   FeedUpdater.daemon_sleep_interval = 10
#
# The feed updaters is controlled by script/feed-updater, which starts and
# stops the process.
class FeedUpdater

  class ExceptionNotification < ActionMailer::Base
    def mail container, error
      environment = Environment.default

      recipients NOOSFERO_CONF['exception_recipients']
      from       environment.noreply_email
      reply_to   environment.noreply_email
      subject    "[#{environment.name}] Feed-updater: #{error.message}"
      body       render(:text => "
Container:
#{container.inspect}

Backtrace:
#{error.backtrace.join("\n")}
      ")
    end
  end

  # indicates how much time one feed will be left without updates
  # (ActiveSupport::Duration). Default: <tt>4.hours</tt>
  cattr_accessor :update_interval
  self.update_interval = 4.hours

  # indicates for how much time the daemon sleeps before looking for new feeds
  # to load (in seconds, an integer). Default: 30
  cattr_accessor :daemon_sleep_interval
  self.daemon_sleep_interval = 30

  attr_accessor :running

  def initialize
    self.running = true
  end

  def start
    ['TERM', 'INT'].each do |signal|
      Signal.trap(signal) do
        stop
        puts "Feed updater exiting gracefully ..."
      end
    end
    puts "Feed updater started."
    run
    puts "Feed updater exited."
  end

  def run
    while running
      process_round
      wait
    end
  end

  def wait
    i = 0
    while running && i < FeedUpdater.daemon_sleep_interval
      sleep 1
      i += 1
    end
  end

  def stop
    self.running = false
  end

  def process_round
    feed_handler = FeedHandler.new
    [FeedReaderBlock, ExternalFeed].each do |source|
      if !running
        break
      end
      source.expired.all.each do |container|
        if !running
          break
        end
        begin
          feed_handler.process(container)
        rescue Exception => e
          FeedUpdater::ExceptionNotification.deliver_mail container, e if NOOSFERO_CONF['exception_recipients'].present?
        end
      end
    end
  end
end