Class: Legion::Object

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/legion/object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



32
33
34
# File 'lib/legion/object.rb', line 32

def pid
  @pid
end

#uriObject (readonly)

Returns the value of attribute uri.



32
33
34
# File 'lib/legion/object.rb', line 32

def uri
  @uri
end

Class Method Details

.before(name, &block) ⇒ Object



27
28
29
# File 'lib/legion/object.rb', line 27

def before(name, &block)
  callbacks[:before][name] = block
end

.callbacksObject



23
24
25
# File 'lib/legion/object.rb', line 23

def callbacks
  @callbacks ||= {before: {}}
end

.method_added(name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/legion/object.rb', line 10

def method_added(name)
  return if name =~ /_async\z/i
  define_method "#{name}_async" do |*args|
    Thread.new do
      synchronize { @busy = true }
      self.class.callbacks[:before][name].call unless self.class.callbacks[:before][name].nil?
      send(name, *args)
      synchronize { @busy = false }
      Thread.terminate
    end
  end
end

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/legion/object.rb', line 38

def busy?
  !!@busy
end

#exitObject



52
53
54
# File 'lib/legion/object.rb', line 52

def exit
  DRb.stop_service
end

#ok?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/legion/object.rb', line 34

def ok?
  true
end

#start_remote_instance(port: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/legion/object.rb', line 42

def start_remote_instance(port: nil)
  return unless pid.nil?
  @uri = "druby://localhost:#{port}"
  @pid = fork do
    DRb.start_service uri, self
    DRb.thread.join
  end
  Process.detach pid
end