class Cadence::Worker

Attributes

activities[R]
activity_middleware[R]
decision_middleware[R]
options[R]
pollers[R]
workflows[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/cadence/worker.rb, line 10
def initialize(options = {})
  @options = options
  @workflows = Hash.new { |hash, key| hash[key] = ExecutableLookup.new }
  @activities = Hash.new { |hash, key| hash[key] = ExecutableLookup.new }
  @pollers = []
  @decision_middleware = []
  @activity_middleware = []
  @shutting_down = false
end

Public Instance Methods

add_activity_middleware(middleware_class, *args) click to toggle source
# File lib/cadence/worker.rb, line 38
def add_activity_middleware(middleware_class, *args)
  @activity_middleware << Middleware::Entry.new(middleware_class, args)
end
add_decision_middleware(middleware_class, *args) click to toggle source
# File lib/cadence/worker.rb, line 34
def add_decision_middleware(middleware_class, *args)
  @decision_middleware << Middleware::Entry.new(middleware_class, args)
end
register_activity(activity_class, options = {}) click to toggle source
# File lib/cadence/worker.rb, line 27
def register_activity(activity_class, options = {})
  execution_options = ExecutionOptions.new(activity_class, options)
  key = [execution_options.domain, execution_options.task_list]

  @activities[key].add(execution_options.name, activity_class)
end
register_workflow(workflow_class, options = {}) click to toggle source
# File lib/cadence/worker.rb, line 20
def register_workflow(workflow_class, options = {})
  execution_options = ExecutionOptions.new(workflow_class, options)
  key = [execution_options.domain, execution_options.task_list]

  @workflows[key].add(execution_options.name, workflow_class)
end
start() click to toggle source
# File lib/cadence/worker.rb, line 42
def start
  workflows.each_pair do |(domain, task_list), lookup|
    pollers << workflow_poller_for(domain, task_list, lookup)
  end

  activities.each_pair do |(domain, task_list), lookup|
    pollers << activity_poller_for(domain, task_list, lookup)
  end

  trap_signals

  pollers.each(&:start)

  # keep the main worker thread alive
  sleep(1) while !shutting_down?
end
stop() click to toggle source
# File lib/cadence/worker.rb, line 59
def stop
  @shutting_down = true

  Thread.new do
    pollers.each(&:stop)
    pollers.each(&:wait)
  end.join
end

Private Instance Methods

activity_poller_for(domain, task_list, lookup) click to toggle source
# File lib/cadence/worker.rb, line 81
def activity_poller_for(domain, task_list, lookup)
  Activity::Poller.new(domain, task_list, lookup.freeze, activity_middleware, options)
end
shutting_down?() click to toggle source
# File lib/cadence/worker.rb, line 73
def shutting_down?
  @shutting_down
end
trap_signals() click to toggle source
# File lib/cadence/worker.rb, line 85
def trap_signals
  %w[TERM INT].each do |signal|
    Signal.trap(signal) { stop }
  end
end
workflow_poller_for(domain, task_list, lookup) click to toggle source
# File lib/cadence/worker.rb, line 77
def workflow_poller_for(domain, task_list, lookup)
  Workflow::Poller.new(domain, task_list, lookup.freeze, decision_middleware, options)
end