Sha256: 9df13f3d78b58d350b996bfb81053429d4ebc6743ef2339ff12b169bb482922c

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

require 'alephant/logger'
require 'date'

module Alephant
  module Publisher
    module Queue
      module SQSHelper
        class Archiver
          include Logger

          attr_reader :cache, :async

          def initialize(cache, async = true)
            @async = async
            @cache = cache
          end

          def see(message)
            return if message.nil?
            message.tap { |m| async ? async_store(m) : store(m) }
          end

          private

          def async_store(m)
            Thread.new { store(m) }
            logger.metric(:name => "PublisherQueueSQSHelperAsynchronouslyArchivedData", :unit => "Count", :value => 1)
          end

          def store(m)
            logger.metric(:name => "PublisherQueueSQSHelperSynchronouslyArchivedData", :unit => "Count", :value => 1)
            logger.info "Publisher::Queue::SQSHelper::Archiver#store: '#{m.body}' at 'archive/#{date_key}/#{m.id}'"
            cache.put("archive/#{date_key}/#{m.id}", m.body, meta_for(m))
          end

          def date_key
            DateTime.now.strftime('%d-%m-%Y_%H')
          end

          def meta_for(m)
            {
              :id                => m.id,
              :md5               => m.md5,
              :logged_at         => DateTime.now.to_s,
              :queue             => m.queue.url,
            }
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alephant-publisher-queue-1.3.3 lib/alephant/publisher/queue/sqs_helper/archiver.rb
alephant-publisher-queue-1.3.2 lib/alephant/publisher/queue/sqs_helper/archiver.rb