Sha256: bf8b7cd213c51fbb34e200f1b1bd54e2266fb8e3dab2b187ebb0004f1d4bb245

Contents?: true

Size: 884 Bytes

Versions: 4

Compression:

Stored size: 884 Bytes

Contents

require 'rack'
require 'sidekiq/hierarchy/http'

module Sidekiq
  module Hierarchy
    module Rack
      class Middleware
        # transform from http header to rack names
        JID_HEADER_KEY = "HTTP_#{Sidekiq::Hierarchy::Http::JID_HEADER.upcase.gsub('-','_')}".freeze
        WORKFLOW_HEADER_KEY = "HTTP_#{Sidekiq::Hierarchy::Http::WORKFLOW_HEADER.upcase.gsub('-','_')}".freeze

        def initialize(app)
          @app = app
        end

        def call(env)
          if env[WORKFLOW_HEADER_KEY] && env[JID_HEADER_KEY]
            Sidekiq::Hierarchy.current_jid = env[JID_HEADER_KEY]
            Sidekiq::Hierarchy.current_workflow = Workflow.find_by_jid(env[WORKFLOW_HEADER_KEY])
          end
          @app.call(env)
        ensure
          Sidekiq::Hierarchy.current_workflow = nil
          Sidekiq::Hierarchy.current_jid = nil
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sidekiq-hierarchy-1.1.0 lib/sidekiq/hierarchy/rack/middleware.rb
sidekiq-hierarchy-1.0.0 lib/sidekiq/hierarchy/rack/middleware.rb
sidekiq-hierarchy-0.1.4 lib/sidekiq/hierarchy/rack/middleware.rb
sidekiq-hierarchy-0.1.3 lib/sidekiq/hierarchy/rack/middleware.rb