Sha256: 713b9bb375bb850ad20e4a90ac8046271cf7a804336ca1b9d17423454995d21d

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

require_relative 'env'

require 'alephant/publisher/version'
require 'alephant/publisher/options'
require 'alephant/publisher/sqs_helper/queue'
require 'alephant/publisher/sqs_helper/archive'
require 'alephant/publisher/writer'
require 'alephant/logger'

module Alephant
  module Publisher
    include Logger

    def self.create(opts = {})
      Publisher.new(opts)
    end

    class Publisher
      VISIBILITY_TIMEOUT = 60
      RECEIVE_WAIT_TIME  = 15

      attr_reader :queue, :executor, :opts

      def initialize(opts)
        @opts = opts

        @queue = SQSHelper::Queue.new(
          aws_queue,
          archiver,
          opts.queue[:visibility_timeout] || VISIBILITY_TIMEOUT,
          opts.queue[:receive_wait_time]  || RECEIVE_WAIT_TIME,
        )
      end

      def run!
        loop { process(@queue.message) }
      end

      private

      def archiver
        SQSHelper::Archiver.new(archive_cache)
      end

      def archive_cache
        Cache.new(
          opts.writer[:s3_bucket_id],
          opts.writer[:s3_object_path]
        )
      end

      def aws_queue
        AWS::SQS.new.queues[opts.queue[:sqs_queue_url]]
      end

      def process(msg)
        unless msg.nil?
          Writer.new(@opts.writer, msg).run!
          msg.delete
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alephant-publisher-0.2.7 lib/alephant/publisher.rb
alephant-publisher-0.2.6 lib/alephant/publisher.rb