Sha256: a15f0372d4f75d3cad35ca1463649ce945d925662559c6eab28ecf771f8d3d13

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 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(uri: @hook.uri, payload: @hook.secure_payload)
        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

2 entries across 2 versions & 1 rubygems

Version Path
activehook-0.1.4 lib/activehook/server/queue.rb
activehook-0.1.3 lib/activehook/server/queue.rb