Sha256: f1338250f6441c14900aaa70dd5db804f5b0f991ef1d10f69025bf36b0be094e

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 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::Server.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::Server.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-server-0.1.1 lib/activehook/server/queue.rb
activehook-server-0.1.0 lib/activehook/server/queue.rb