Sha256: cff568e439b6c83475c3b2d1bccf36fb8433d72ce5220fbd58c5cb8e66a5f830

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Miteru
  class Orchestrator < Service
    def call
      Miteru.logger.info("#{websites.length} websites loaded in total.") if verbose?

      if Miteru.sidekiq?
        websites.each do |website|
          Jobs::CrawleJob.perform_async(website.url, website.source)
          Miteru.logger.info("Website:#{website.truncated_url} crawler job queued.") if verbose?
        end
      else
        Miteru.logger.info("Use #{threads} thread(s).") if verbose?
        Parallel.each(websites, in_threads: threads) do |website|
          Miteru.logger.info("Website:#{website.truncated_url} crawling started.") if verbose?

          result = Crawler.result(website)
          if result.success?
            Miteru.logger.info("Crawler:#{website.truncated_url} succeeded.")
          else
            Miteru.logger.info("Crawler:#{website.truncated_url} failed - #{result.failure}.")
          end
        end
      end
    end

    #
    # @return [Array<Miteru::Websites>]
    #
    def websites
      @websites ||= [].tap do |out|
        feeds.each do |feed|
          result = feed.result
          if result.success?
            websites = result.value!
            Miteru.logger.info("Feed:#{feed.source} has #{websites.length} websites.") if verbose?
            out << websites
          else
            Miteru.logger.warn("Feed:#{feed.source} failed - #{result.failure}")
          end
        end
      end.flatten
    end

    private

    def threads
      Miteru.config.threads
    end

    def verbose?
      Miteru.config.verbose
    end

    #
    # @return [Array<Miteru::Feeds::Base>]
    #
    def feeds
      Miteru.feeds.map(&:new)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
miteru-2.0.2 lib/miteru/orchestrator.rb