Sha256: bda40317b58792df28e1986809923c0f45eca2a50029889ce38b5bf9ddcc4081

Contents?: true

Size: 1.32 KB

Versions: 17

Compression:

Stored size: 1.32 KB

Contents

module Kabutops
  class Watchdog
    include Extensions::Logging
    include Sidekiq::Worker

    class << self
      include Extensions::Parameterable
      include Extensions::CallbackSupport

      params :crawler, :freshness, :wait
      callbacks :on_outdated

      def check!
        perform_async
      end

      def check
        new.check
      end

      def loop
        loop do
          sleep self.params[:wait] || 5
          check
        end
      end
    end

    def check
      logger.info "#{self.class} check started"

      outdated_resources.each do |resource|
        resource.update(scheduled_update_at: Time.now.to_i)

        adapters.each do |adapter|
          adapter.store(resource)
        end

        self.class.notify(:on_outdated, resource)
      end

      logger.info "#{self.class} check finished"
    end

    def perform
      check
      sleep self.class.params[:wait] || 5
      self.class.perform_async
    end

    protected

    def outdated_resources
      adapters.map{ |a| a.find_outdated(freshness) }
        .flatten
        .uniq
        .reject{ |r| (r[:scheduled_update_at] || 0) > Time.now.to_i - 3600 }
        .map{ |r| Hashie::Mash.new(r) }
    end

    def adapters
      self.class.params.crawler.adapters
    end

    def freshness
      self.class.params.freshness
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
kabutops-0.3.0 lib/kabutops/watchdog.rb
kabutops-0.2.0 lib/kabutops/watchdog.rb
kabutops-0.1.7 lib/kabutops/watchdog.rb
kabutops-0.1.6 lib/kabutops/watchdog.rb
kabutops-0.1.5 lib/kabutops/watchdog.rb
kabutops-0.1.4 lib/kabutops/watchdog.rb
kabutops-0.1.3 lib/kabutops/watchdog.rb
kabutops-0.1.2 lib/kabutops/watchdog.rb
kabutops-0.1.1 lib/kabutops/watchdog.rb
kabutops-0.1.0 lib/kabutops/watchdog.rb
kabutops-0.0.15 lib/kabutops/watchdog.rb
kabutops-0.0.14 lib/kabutops/watchdog.rb
kabutops-0.0.13 lib/kabutops/watchdog.rb
kabutops-0.0.12 lib/kabutops/watchdog.rb
kabutops-0.0.11 lib/kabutops/watchdog.rb
kabutops-0.0.10 lib/kabutops/watchdog.rb
kabutops-0.0.9 lib/kabutops/watchdog.rb