Class: Porcupine

Inherits:
Object
  • Object
show all
Defined in:
lib/porcupine/future.rb,
lib/porcupine/porcupine.rb,
lib/porcupine/exceptions.rb,
lib/porcupine/observable.rb

Defined Under Namespace

Classes: FailedExecutionError, Future, Observable, RejectedError, ShortCircuitError, TimeoutError

Constant Summary collapse

DEFAULT_TIMEOUT =
10_000
DEFAULT_GROUP =
"default"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_or_setter, group = DEFAULT_GROUP, timeout = DEFAULT_TIMEOUT, &block) ⇒ Porcupine

Returns a new instance of Porcupine.



12
13
14
15
16
17
18
19
20
21
# File 'lib/porcupine/porcupine.rb', line 12

def initialize(name_or_setter, group=DEFAULT_GROUP, timeout=DEFAULT_TIMEOUT, &block)
  @block = block

  setter = name_or_setter if name_or_setter.is_a?(com.netflix.hystrix.HystrixCommand::Setter)
  setter ||= Setter.withGroupKey(HystrixCommandGroupKey::Factory.asKey(group))
                   .andCommandKey(HystrixCommandKey::Factory.asKey(name_or_setter))
                   .andCommandPropertiesDefaults(HystrixCommandProperties::Setter().withExecutionIsolationThreadTimeoutInMilliseconds(timeout))

  super(setter)
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



10
11
12
# File 'lib/porcupine/porcupine.rb', line 10

def block
  @block
end

Instance Method Details

#executeObject

Only catch Java exceptions since we already handle most exceptions in Observable#get



24
25
26
27
28
# File 'lib/porcupine/porcupine.rb', line 24

def execute
  queue.get
rescue Java::JavaLang::Throwable => e
  raise decomposeException(e)
end

#getFallbackObject



54
55
56
57
58
59
60
# File 'lib/porcupine/porcupine.rb', line 54

def getFallback
  return getFailedExecutionException.getException if isFailedExecution
  return RejectedError.new                        if isResponseRejected
  return ShortCircuitError.new                    if isResponseShortCircuited
  return TimeoutError.new                         if isResponseTimedOut
  RuntimeError.new
end

#observeObject



30
31
32
# File 'lib/porcupine/porcupine.rb', line 30

def observe
  Observable.new(super)
end

#queueObject



46
47
48
# File 'lib/porcupine/porcupine.rb', line 46

def queue
  Future.new(super)
end

#runObject



50
51
52
# File 'lib/porcupine/porcupine.rb', line 50

def run
  block.call
end

#toObservable(*args) ⇒ Object

Only wrap the outer-most call; otherwise Java gets angry because the class of the returned object won’t match the signature when the calls recurse



36
37
38
39
40
41
42
43
44
# File 'lib/porcupine/porcupine.rb', line 36

def toObservable(*args)
  result = super

  unless caller.first.match(/toObservable/) || caller.first.match(/observe/)
    result = Observable.new(result)
  end

  result
end