class CC::Service::Invocation

Constants

MIDDLEWARE

Attributes

result[R]

Public Class Methods

invoke(service, &block) click to toggle source

Build a chain of invocation wrappers which eventually calls receive on the given service, then execute that chain.

Order is important. Each call to with, wraps the last.

Usage:

CC::Service::Invocation.invoke(service) do |i|
  i.with :retries, 3
  i.with :metrics, $statsd
  i.with :error_handling, Rails.logger
end

In the above example, service.receive could happen 4 times (once, then three retries) before an exception is re-raised up to the metrics collector, then up again to the error handling. If the order were reversed, the error handling middleware would prevent the other middleware from seeing any exceptions at all.

# File lib/cc/service/invocation.rb, line 35
def self.invoke(service, &block)
  instance = new(service, &block)
  instance.result
end
new(service) { |self| ... } click to toggle source
# File lib/cc/service/invocation.rb, line 40
def initialize(service)
  @chain = InvocationChain.new { service.receive }

  yield(self) if block_given?

  @result = @chain.call
end

Public Instance Methods

with(middleware, *args) click to toggle source
# File lib/cc/service/invocation.rb, line 48
def with(middleware, *args)
  if klass = MIDDLEWARE[middleware]
    wrap(klass, *args)
  end
end
wrap(klass, *args) click to toggle source
# File lib/cc/service/invocation.rb, line 54
def wrap(klass, *args)
  @chain.wrap(klass, *args)
end