Class: Shatter::Service::Base

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Async
Defined in:
lib/shatter/service/base.rb

Defined Under Namespace

Classes: ReloadWrapper

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.service_definitionObject

Returns the value of attribute service_definition.



24
25
26
# File 'lib/shatter/service/base.rb', line 24

def service_definition
  @service_definition
end

Class Method Details

.closeObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/shatter/service/base.rb', line 58

def self.close
  logger = Shatter.logger
  logger.info "Closing down DRb service: #{@service_instance}"
  @service_instance.stop_service
  port = Shatter::Config.service_port
  uri = "localhost:#{port}"
  logger.info "Removing my existnce at #{port} to zookeeper"
  Shatter::Service::Discovery.deregister_service(uri)
  logger.info "Closed DRb service"
end

.initObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/shatter/service/base.rb', line 69

def self.init
  logger.info "Initing DRb service"
  port = Shatter::Config.service_port
  uri = "localhost:#{port}"
  logger.info "Logging my existnce at #{uri} to zookeeper"
  Shatter::Service::Discovery.register_service(uri)
  logger.info "Starting DRb service"
  @service_instance = DRb.start_service("druby://#{uri}", Shatter::Config.reload_classes ? ReloadWrapper : self)
  logger.info "DRb service started"
  DRb.thread.join
end

.loggerObject



81
82
83
# File 'lib/shatter/service/base.rb', line 81

def self.logger
  Shatter.logger
end

.method_missing(method, *args) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/shatter/service/base.rb', line 39

def self.method_missing(method, *args, &)
  super unless respond_to_missing?(method)
  uuid = args[0].is_a?(Hash) ? args[0][:uuid] : args[0].uuid
  return { error: "missing uuid" } if uuid.nil?

  future = @service_definition.new.async.send(method, *args, &)
  future.add_observer(self, :populate_pool_with_result)
end

.populate_pool_with_result(_time, value, err) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/shatter/service/base.rb', line 48

def self.populate_pool_with_result(_time, value, err)
  Shatter.logger.info err if err
  Shatter.logger.info "#{value[:uuid]} => #{value}" unless err
  Shatter::Service::ResponsePool.instance.pool[value[:uuid]] = value
  Shatter::Service::Discovery.populate_result_location(value[:uuid])
rescue StandardError => e
  Shatter.logger.error e
  raise e
end

.respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/shatter/service/base.rb', line 31

def self.respond_to_missing?(method)
  @service_definition.new.respond_to?(method)
end

.response_for(uuid) ⇒ Object



27
28
29
# File 'lib/shatter/service/base.rb', line 27

def self.response_for(uuid)
  Shatter::Service::ResponsePool.instance.pool[uuid]
end

.set_static_result_for(uuid, result) ⇒ Object



35
36
37
# File 'lib/shatter/service/base.rb', line 35

def self.set_static_result_for(uuid, result)
  populate_pool_with_result(Time.now, { uuid:, result: }, nil)
end