Class: Realm::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Mixins::DependencyInjection
Defined in:
lib/realm/dispatcher.rb

Instance Method Summary collapse

Methods included from Mixins::DependencyInjection

included

Constructor Details

#initialize(runtime) ⇒ Dispatcher

Returns a new instance of Dispatcher.



8
9
10
11
# File 'lib/realm/dispatcher.rb', line 8

def initialize(runtime)
  @runtime = runtime
  @threads = []
end

Instance Method Details

#query(identifier, params = {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/realm/dispatcher.rb', line 13

def query(identifier, params = {})
  callable, action = get_callable(QueryHandler, identifier)
  callable, action = get_repo_adapter(identifier) unless callable
  raise QueryHandlerMissing, identifier unless callable

  dispatch(callable, action, params)
end

#run(identifier, params = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/realm/dispatcher.rb', line 21

def run(identifier, params = {})
  callable, action = get_callable(CommandHandler, identifier)
  raise CommandHandlerMissing, identifier unless callable

  dispatch(callable, action, params)
end

#run_as_job(identifier, params = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/realm/dispatcher.rb', line 28

def run_as_job(identifier, params = {})
  callable, action = get_callable(CommandHandler, identifier)
  raise CommandHandlerMissing, identifier unless callable

  @threads.delete_if(&:stop?)
  @threads << Thread.new do # TODO: back by SQS
    result = dispatch(callable, action, params)
    yield result if block_given?
  end
end

#wait_for_jobsObject

Blocks until all jobs are finished. Useful mainly in tests.



40
41
42
# File 'lib/realm/dispatcher.rb', line 40

def wait_for_jobs
  @threads.each(&:join)
end