Module: Distribunaut::Utils::Rinda

Defined in:
lib/distribunaut/utils/rinda.rb

Class Method Summary collapse

Class Method Details

.available_servicesObject



57
58
59
60
61
62
63
64
65
# File 'lib/distribunaut/utils/rinda.rb', line 57

def available_services
  ring_server = self.ring_server
  all = ring_server.read_all([nil, nil, nil, nil])
  services = []
  all.each do |service|
    services << Distribunaut::Tuple.from_array(service)
  end
  services
end

.borrow(values = {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/distribunaut/utils/rinda.rb', line 39

def borrow(values = {}, &block)
  tuple = build_tuple(values)
  results = ring_server.take(tuple.to_array, tuple.timeout)
  tuple = Distribunaut::Tuple.from_array(results)
  tuple.space = "#{tuple.space}-onloan-#{Time.now}".to_sym
  register(tuple)
  begin
    yield tuple if block_given?
  rescue Exception => e
    raise e
  ensure
    # (.+)-onloan-.+$
    tuple.space.to_s.match(/(.+)-onloan-.+$/)
    tuple.space = $1.to_sym
    register(tuple)
  end
end

.read(values = {}) ⇒ Object



32
33
34
35
36
37
# File 'lib/distribunaut/utils/rinda.rb', line 32

def read(values = {})
  tuple = build_tuple(values)
  results = ring_server.read(tuple.to_array, tuple.timeout)
  tuple = Distribunaut::Tuple.from_array(results)
  tuple.object
end

.register(values = {}) ⇒ Object



17
18
19
20
# File 'lib/distribunaut/utils/rinda.rb', line 17

def register(values = {})
  tuple = build_tuple(values)
  ring_server.write(tuple.to_array, ::Rinda::SimpleRenewer.new)
end

.register_or_renew(values = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/distribunaut/utils/rinda.rb', line 7

def register_or_renew(values = {})
  tuple = build_tuple(values)
  begin
    ring_server.take(tuple.to_search_array, tuple.timeout)
  rescue ::Rinda::RequestExpiredError => e
    # it's ok that it expired. It could be that it was never registered.
  end
  register(values)
end

.remove_all_services!Object



67
68
69
70
71
# File 'lib/distribunaut/utils/rinda.rb', line 67

def remove_all_services!
  available_services.each do |service|
    ring_server.take(service.to_array)
  end
end

.ring_serverObject



22
23
24
25
26
27
28
29
30
# File 'lib/distribunaut/utils/rinda.rb', line 22

def ring_server
  if configatron.distribunaut.retrieve(:acl, nil)
    acl = ACL.new(configatron.distribunaut.acl)
    DRb.install_acl(acl)
  end
  ::DRb.start_service
  rs = ::Rinda::RingFinger.primary
  rs
end