Class: Riser::DRbServiceCall

Inherits:
Object
  • Object
show all
Defined in:
lib/riser/services.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDRbServiceCall

Returns a new instance of DRbServiceCall.



149
150
151
152
153
154
155
# File 'lib/riser/services.rb', line 149

def initialize
  @mutex = Thread::Mutex.new
  @druby_call_list = []
  @single_process_service_count = 0
  @services = {}
  @random = nil
end

Class Method Details

.is_callable(type_or_object) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/riser/services.rb', line 178

def self.is_callable(type_or_object)
  case (type_or_object)
  when Class
    object_type = type_or_object
    object_type.method_defined? :call
  else
    object = type_or_object
    object.respond_to? :call
  end
end

Instance Method Details

#[](name, *optional, &block) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
# File 'lib/riser/services.rb', line 310

def [](name, *optional, &block)
  if (@services.key? name) then
    if (@services[name].callable) then
      call_service(name, *optional, &block)
    else
      get_service(name, *optional)
    end
  else
    raise KeyError, "not found a service: #{name}"
  end
end

#add_any_process_service(name, callable = false) ⇒ Object



162
163
164
165
# File 'lib/riser/services.rb', line 162

def add_any_process_service(name, callable=false)
  @services[name] = DRbAnyProcessService.new(:any, callable)
  nil
end

#add_any_process_service_with_type(name, type_or_object) ⇒ Object



189
190
191
192
# File 'lib/riser/services.rb', line 189

def add_any_process_service_with_type(name, type_or_object)
  add_any_process_service(name, DRbServiceCall.is_callable(type_or_object))
  nil
end

#add_druby_call(uri) ⇒ Object



157
158
159
160
# File 'lib/riser/services.rb', line 157

def add_druby_call(uri)
  @druby_call_list << DRbCall.new(DRbObject.new_with_uri(uri), {})
  nil
end

#add_single_process_service(name, callable = false) ⇒ Object



167
168
169
170
171
# File 'lib/riser/services.rb', line 167

def add_single_process_service(name, callable=false)
  @services[name] = DRbSingleProcessService.new(:single, callable, @single_process_service_count)
  @single_process_service_count += 1
  nil
end

#add_single_process_service_with_type(name, type_or_object) ⇒ Object



194
195
196
197
# File 'lib/riser/services.rb', line 194

def add_single_process_service_with_type(name, type_or_object)
  add_single_process_service(name, DRbServiceCall.is_callable(type_or_object))
  nil
end

#add_sticky_process_service(name, callable = false) ⇒ Object



173
174
175
176
# File 'lib/riser/services.rb', line 173

def add_sticky_process_service(name, callable=false)
  @services[name] = DRbStickyProcessService.new(:sticky, callable)
  nil
end

#add_sticky_process_service_with_type(name, type_or_object) ⇒ Object



199
200
201
202
# File 'lib/riser/services.rb', line 199

def add_sticky_process_service_with_type(name, type_or_object)
  add_sticky_process_service(name, DRbServiceCall.is_callable(type_or_object))
  nil
end

#call_service(name, *optional, &block) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/riser/services.rb', line 293

def call_service(name, *optional, &block)
  if (@services.key? name) then
    case (@services[name].process_type)
    when :any
      call_any_process_service(name, *optional, &block)
    when :single
      call_single_process_service(name, *optional, &block)
    when :sticky
      call_sticky_process_service(name, *optional, &block)
    else
      raise "internal error: (service_name,process_type)=(#{name},#{@services[name].process_type})"
    end
  else
    raise KeyError, "not found a service: #{name}"
  end
end

#druby_ping(timeout_seconds) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/riser/services.rb', line 204

def druby_ping(timeout_seconds)
  t0 = Time.now
  if (timeout_seconds > 0.1) then
    dt = 0.1
  else
    dt = timeout_seconds * 0.1
  end

  for druby_call in @druby_call_list
    begin
      druby_call.there.ping{ 'pong' }
    rescue DRb::DRbConnError
      if (Time.now - t0 >= timeout_seconds) then
        raise
      else
        sleep(dt)
      end
      retry
    end
  end

  nil
end

#get_service(name, *optional) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/riser/services.rb', line 261

def get_service(name, *optional)
  if (@services.key? name) then
    case (@services[name].process_type)
    when :any
      get_any_process_service(name, *optional)
    when :single
      get_single_process_service(name, *optional)
    when :sticky
      get_sticky_process_service(name, *optional)
    else
      raise "internal error: (service_name,process_type)=(#{name},#{@services[name].process_type})"
    end
  else
    raise KeyError, "not found a service: #{name}"
  end
end

#start(timeout_seconds = 30, local_druby_uri = Riser::TemporaryPath.make_drbunix_uri, config = { UNIXFileMode: 0600 }) ⇒ Object



228
229
230
231
232
233
234
235
236
# File 'lib/riser/services.rb', line 228

def start(timeout_seconds=30, local_druby_uri=Riser::TemporaryPath.make_drbunix_uri, config={ UNIXFileMode: 0600 })
  @random = Random.new
  unless (DRb.primary_server) then
    DRb.start_service(local_druby_uri, nil, config)
  end
  druby_ping(timeout_seconds)

  nil
end