Class: JerbilService::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/jerbil/jerbil_service/base.rb

Overview

JerbilService::Base

Parent class to be used for all services. Manages all interactions with Jerbil and sets up a Jellog logger.

In general, services using this class are intended to be started and stopped using Supervisor and clients are expected to interact with the service through Client.

Further details can be found in Jerbil Service Readme

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pkey, options) ⇒ Base

create a Jerbil Service

Parameters:

  • name (String)
    • symbol for the service needs to correspond with /etc/services

  • pkey (String)
    • private key used for privileged methods

  • options (Hash)

    for the service. The easiest way to generate this hash is to use Jeckyl and inherit the Config class. See class description for details of the options required by a service



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jerbil/jerbil_service/base.rb', line 74

def initialize(name, pkey, options)
  @name = name.to_s.capitalize
  @env = options[:environment]
  @private_key = pkey
  
  # change the process name 
  $0 = "#{@name.downcase}-#{@env}"
  
  # start up a logger
  log_opts = Jellog::Config.intersection(options)
  @logger = Jellog::Logger.new(@name, log_opts)
  # @logger.log_level = options[:log_level]
  
  @exit = options[:exit_on_stop]

  begin
    @service = Jerbil::ServiceRecord.new(name, @env, :verify_callback, :stop_callback)

    # register the service
    # Note that if the options hash includes a :jerbil_env, then this
    # will be passed to select a Jerbil system running at other than :prod, the default
    # This is not documented above because it is for testing Jerbil only
    @jerbil_server = Jerbil::Servers.get_local_server(options[:jerbil_env])

    # now connect to it
    jerbil = @jerbil_server.connect

    # and register self
    jerbil.register(@service)

    # and start it - preventing anything nasty from coming over DRb
    $SAFE = 1 unless options[:unsafe]
    DRb.start_service(@service.drb_address, self)

  rescue Jerbil::MissingServer
    @logger.fatal("Cannot find a local Jerbil server")
    raise
  rescue Jerbil::JerbilConfigError => err
    @logger.fatal("Error in Jerbil Config File: #{err.message}")
    raise
  rescue Jerbil::JerbilServiceError =>jerr
    @logger.fatal("Error with Jerbil Service: #{jerr.message}")
    raise
  rescue Jerbil::ServerConnectError
    @logger.fatal("Error connecting to Jerbil Server")
    raise
  rescue DRb::DRbConnError =>derr
    @logger.fatal("Error setting up DRb Server: #{derr.message}")
    raise Jerbil::ServerConnectError
  end

  @logger.system "Started service: #{@service.ident}"
  @logger.info "Jerbil Service #{@service.ident} running under #{RUBY_VERSION}"
end

Instance Attribute Details

#serviceObject (readonly)

Deprecated.
  • unless I can find why I allowed it!

give access to Jerbil::ServiceRecord Record for this service



131
132
133
# File 'lib/jerbil/jerbil_service/base.rb', line 131

def service
  @service
end

Instance Method Details

#drb_addressObject

Deprecated.
  • as above

return the DRb address for the service



135
136
137
# File 'lib/jerbil/jerbil_service/base.rb', line 135

def drb_address
  @service.drb_address
end

#stop_callback(pkey = "") ⇒ Object

used to stop the service. Requires the private key

This method is not intended to be called directly. To stop a service, the user should use the Supervisor class.

Parameters:

  • pkey (String) (defaults to: "")
    • private key given when service is created



160
161
162
163
164
165
166
167
168
169
# File 'lib/jerbil/jerbil_service/base.rb', line 160

def stop_callback(pkey="")
  check_key(pkey)
  # deregister
  jerbil = @jerbil_server.connect
  jerbil.remove(@service)
  @logger.system "Stopped service: #{@service.ident}"
  @logger.close
  # and stop the DRb service, to exit gracefully
  DRb.stop_service
end

#verify_callback(skey = "") ⇒ Object

this is used by callers just to check that the service is running if caller is unaware of the key, this will fail

This method is not intended to be called directly. It is usually called when connecting to the service (see Jerbil::ServiceRecord#connect).

Parameters:

  • skey (String) (defaults to: "")
    • private key given to the service when created



147
148
149
150
151
# File 'lib/jerbil/jerbil_service/base.rb', line 147

def verify_callback(skey="")
  check_key(skey, @service.key)
  @logger.info "Verify called"
  return true
end

#wait(pkey = '') ⇒ Object

wait for calls. This hides the DRb call to be made once the service is up and running. It is not intended to be called by anyone outside of Supervisor



175
176
177
178
# File 'lib/jerbil/jerbil_service/base.rb', line 175

def wait(pkey='')
  check_key(pkey)
  DRb.thread.join
end