Sha256: adc33f9c500452ce24272bcc04511ee17d00da896107a209aa78fd05d6470c8c

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

module ActiveHook
  module Server
    # The Queue object processes any hooks that are queued into our Redis server.
    # It will perform a 'blocking pop' on our hook list until one is added.
    #
    class Queue
      def initialize
        @done = false
      end

      # Starts our queue process. This will run until instructed to stop.
      #
      def start
        until @done
          json = retrieve_hook
          HookRunner.new(json) if json
        end
      end

      # Shutsdown our queue process.
      #
      def shutdown
        @done = true
      end

      private

      # Performs a 'blocking pop' on our redis queue list.
      #
      def retrieve_hook
        json = ActiveHook.redis.with { |c| c.brpop('ah:queue') }
        json.last if json
      end
    end

    class HookRunner
      def initialize(json)
        @hook = Hook.new(JSON.parse(json))
        @post = Send.new(hook: @hook)
        start
      end

      def start
        @post.start
        ActiveHook.redis.with do |conn|
          @post.success? ? hook_success(conn) : hook_failed(conn)
        end
      end

      private

      def hook_success(conn)
        conn.incr('ah:total_success')
      end

      def hook_failed(conn)
        conn.zadd('ah:retry', @hook.retry_at, @hook.to_json) if @hook.retry?
        conn.incr('ah:total_failed')
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
activehook-0.1.9 lib/activehook/server/queue.rb
activehook-0.1.8 lib/activehook/server/queue.rb
activehook-0.1.7 lib/activehook/server/queue.rb
activehook-0.1.6 lib/activehook/server/queue.rb
activehook-0.1.5 lib/activehook/server/queue.rb