Class: Expeditor::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/expeditor/command.rb

Direct Known Subclasses

ConstCommand

Defined Under Namespace

Classes: ConstCommand

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ Command



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/expeditor/command.rb', line 13

def initialize(opts = {}, &block)
  @service = opts.fetch(:service, Expeditor::Services.default)
  @timeout = opts[:timeout]
  @dependencies = opts.fetch(:dependencies, [])

  @normal_future = nil
  @retryable_options = Concurrent::IVar.new
  @normal_block = block
  @fallback_block = nil
  @ivar = Concurrent::IVar.new
end

Class Method Details

.const(value) ⇒ Object



121
122
123
# File 'lib/expeditor/command.rb', line 121

def self.const(value)
  ConstCommand.new(value)
end

.start(opts = {}, &block) ⇒ Object



125
126
127
# File 'lib/expeditor/command.rb', line 125

def self.start(opts = {}, &block)
  Command.new(opts, &block).start
end

Instance Method Details

#chain(opts = {}, &block) ⇒ Object

XXX: Raise ArgumentError when given ‘opts` has :dependencies because this forcefully change given :dependencies.

‘chain` returns new command that has self as dependencies



116
117
118
119
# File 'lib/expeditor/command.rb', line 116

def chain(opts = {}, &block)
  opts[:dependencies] = [self]
  Command.new(opts, &block)
end

#getObject

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/expeditor/command.rb', line 51

def get
  raise NotStartedError unless started?
  @normal_future.get_or_else do
    if @fallback_block && @service.fallback_enabled?
      @ivar.wait
      if @ivar.rejected?
        raise @ivar.reason
      else
        @ivar.value
      end
    else
      raise @normal_future.reason
    end
  end
end

#on_complete(&block) ⇒ Object

command.on_complete do |success, value, reason|

...

end



88
89
90
91
92
# File 'lib/expeditor/command.rb', line 88

def on_complete(&block)
  on do |_, value, reason|
    block.call(reason == nil, value, reason)
  end
end

#on_failure(&block) ⇒ Object

command.on_failure do |e|

...

end



106
107
108
109
110
# File 'lib/expeditor/command.rb', line 106

def on_failure(&block)
  on do |_, _, reason|
    block.call(reason) if reason
  end
end

#on_success(&block) ⇒ Object

command.on_success do |value|

...

end



97
98
99
100
101
# File 'lib/expeditor/command.rb', line 97

def on_success(&block)
  on do |_, value, reason|
    block.call(value) unless reason
  end
end

#set_fallback(&block) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/expeditor/command.rb', line 67

def set_fallback(&block)
  if started?
    raise AlreadyStartedError, "Do not allow set_fallback call after command is started"
  end
  reset_fallback(&block)
  self
end

#start(current_thread: false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/expeditor/command.rb', line 26

def start(current_thread: false)
  unless started?
    if current_thread
      prepare(Concurrent::ImmediateExecutor.new)
    else
      prepare
    end
    @normal_future.safe_execute
  end
  self
end

#start_with_retry(current_thread: false, **retryable_options) ⇒ Object

Equivalent to retryable gem options



39
40
41
42
43
44
45
# File 'lib/expeditor/command.rb', line 39

def start_with_retry(current_thread: false, **retryable_options)
  unless started?
    @retryable_options.set(retryable_options)
    start(current_thread: current_thread)
  end
  self
end

#started?Boolean



47
48
49
# File 'lib/expeditor/command.rb', line 47

def started?
  @normal_future && @normal_future.executed?
end

#waitObject

Raises:



80
81
82
83
# File 'lib/expeditor/command.rb', line 80

def wait
  raise NotStartedError unless started?
  @ivar.wait
end

#with_fallback(&block) ⇒ Object



75
76
77
78
# File 'lib/expeditor/command.rb', line 75

def with_fallback(&block)
  warn 'Expeditor::Command#with_fallback is deprecated. Please use set_fallback instead'
  set_fallback(&block)
end