Class: Arsenicum::Core::Worker

Inherits:
Object
  • Object
show all
Includes:
Commands, IOHelper
Defined in:
lib/arsenicum/core/worker.rb

Defined Under Namespace

Classes: InvokerThread

Constant Summary collapse

RESULT_SUCCESS =
0
RESULT_FAILURE =
0x80
CONTROL_STOP =
0xFF
CONTROL_PING =
0x30

Constants included from IOHelper

IOHelper::TYPE_INT, IOHelper::TYPE_STRING

Constants included from Commands

Commands::COMMAND_STOP, Commands::COMMAND_STRING_STOP, Commands::COMMAND_STRING_TASK, Commands::COMMAND_TASK

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IOHelper

#read_message, #write_message

Constructor Details

#initialize(broker, index, worker_configuration) ⇒ Worker

Returns a new instance of Worker.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/arsenicum/core/worker.rb', line 22

def initialize(broker, index, worker_configuration)
  @broker     = WeakRef.new broker # avoiding circular references.
  @index      = index
  @serializer = worker_configuration[:serializer]
  @formatter  = worker_configuration[:formatter]
  @thread     = InvokerThread.new(self)
  @work_at    = :parent
  @state      = :parent

  run
end

Instance Attribute Details

#activeObject (readonly) Also known as: active?

Returns the value of attribute active.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def active
  @active
end

#brokerObject (readonly)

Returns the value of attribute broker.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def broker
  @broker
end

#ctrl_in_childObject (readonly)

Returns the value of attribute ctrl_in_child.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def ctrl_in_child
  @ctrl_in_child
end

#ctrl_in_parentObject (readonly)

Returns the value of attribute ctrl_in_parent.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def ctrl_in_parent
  @ctrl_in_parent
end

#ctrl_out_childObject (readonly)

Returns the value of attribute ctrl_out_child.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def ctrl_out_child
  @ctrl_out_child
end

#ctrl_out_parentObject (readonly)

Returns the value of attribute ctrl_out_parent.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def ctrl_out_parent
  @ctrl_out_parent
end

#formatterObject (readonly)

Returns the value of attribute formatter.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def formatter
  @formatter
end

#in_childObject (readonly)

Returns the value of attribute in_child.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def in_child
  @in_child
end

#in_parentObject (readonly)

Returns the value of attribute in_parent.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def in_parent
  @in_parent
end

#indexObject (readonly)

Returns the value of attribute index.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def index
  @index
end

#out_childObject (readonly)

Returns the value of attribute out_child.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def out_child
  @out_child
end

#out_parentObject (readonly)

Returns the value of attribute out_parent.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def out_parent
  @out_parent
end

#pidObject (readonly)

Returns the value of attribute pid.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def pid
  @pid
end

#serializerObject (readonly)

Returns the value of attribute serializer.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def serializer
  @serializer
end

#stateObject (readonly)

Returns the value of attribute state.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def state
  @state
end

#threadObject (readonly)

Returns the value of attribute thread.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def thread
  @thread
end

#work_atObject (readonly)

Returns the value of attribute work_at.



13
14
15
# File 'lib/arsenicum/core/worker.rb', line 13

def work_at
  @work_at
end

Instance Method Details

#==(another) ⇒ Object



34
35
36
37
38
# File 'lib/arsenicum/core/worker.rb', line 34

def ==(another)
  return false unless another.is_a? ::Arsenicum::Core::Worker

  return another.pid == self.pid
end

#ask(task_id, *parameters) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/arsenicum/core/worker.rb', line 40

def ask(task_id, *parameters)
  write_message                   out_parent, task_id,  serialize(parameters)
  loop do
    rs, = select([in_parent], [], [], 5)
    break if rs
    sleep 0.5
  end

  result, marshaled_exception = read_message  in_parent
  return if result == RESULT_SUCCESS
  raise Marshal.load(marshaled_exception)
end

#ask_async(success_handler, failure_handler, task_id, *parameters) ⇒ Object



53
54
55
# File 'lib/arsenicum/core/worker.rb', line 53

def ask_async(success_handler, failure_handler, task_id, *parameters)
  thread.ask success_handler, failure_handler, task_id, *parameters
end

#return_to_brokerObject



69
70
71
# File 'lib/arsenicum/core/worker.rb', line 69

def return_to_broker
  broker.get_back_worker self
end

#stopObject



57
58
59
60
61
62
63
# File 'lib/arsenicum/core/worker.rb', line 57

def stop
  thread.terminate
  return unless child_process_alive?

  write_message   ctrl_out_parent, COMMAND_STOP
  Process.waitpid pid
end