Class: ScbiMapreduce::Worker

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EM::P::ObjectProtocol
Defined in:
lib/scbi_mapreduce/worker.rb

Constant Summary collapse

@@want_to_exit_worker =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Worker

Returns a new instance of Worker.



41
42
43
# File 'lib/scbi_mapreduce/worker.rb', line 41

def initialize(*args)
  super
end

Class Method Details

.controlled_exit_workerObject



135
136
137
# File 'lib/scbi_mapreduce/worker.rb', line 135

def self.controlled_exit_worker
  @@want_to_exit_worker=true
end

.start_worker(worker_id, ip, port, log_file = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/scbi_mapreduce/worker.rb', line 139

def self.start_worker(worker_id,ip,port,log_file=nil)
  #puts "NEW WORKER - INIIIIIIIIIIIIIIIIIIIIT #{self}"
  
  
  ip = ip
  port = port
  @@count = -1

  @@worker_id=worker_id
  
  # Signal.trap("INT")  { puts "TRAP INT in worker #{@@worker_id}"; controlled_exit_worker; EM.stop}
  # Signal.trap("TERM") { puts "TRAP TERM in worker #{@@worker_id}";controlled_exit_worker; EM.stop}

  if log_file.nil?
    log_file = 'logs/worker'+worker_id+'_'+`hostname`.chomp+'_log.txt'
  end

  FileUtils.mkdir_p(File.dirname(log_file)) if ((log_file!=STDOUT) && (!File.exists?(File.dirname(log_file))))

  $WORKER_LOG = Logger.new(log_file)
  $WORKER_LOG.datetime_format = "%Y-%m-%d %H:%M:%S"

  $LOG = $WORKER_LOG

  total_seconds = Time.now

  EM.error_handler{ |e|
    $WORKER_LOG.error(e.message + ' => ' + e.backtrace.join("\n"))
  }

  EventMachine::run {

    EventMachine::connect ip, port, self
    $WORKER_LOG.info "Worker connected to #{ip}:#{port}"

  }

  total_seconds = Time.now-total_seconds
  $WORKER_LOG.info "Client #{@@worker_id} processed: #{@@count} objs"
  $WORKER_LOG.info "Client #{@@worker_id} proc rate: #{@@count/total_seconds.to_f} objects/seg"

end

Instance Method Details

#closing_workerObject



35
36
37
38
# File 'lib/scbi_mapreduce/worker.rb', line 35

def closing_worker


end

#post_initObject



45
46
47
48
49
50
51
52
# File 'lib/scbi_mapreduce/worker.rb', line 45

def post_init
  $WORKER_LOG.info('WORKER CONNECTED')

  worker_connected
rescue Exception => e
  $WORKER_LOG.error("Exiting worker #{@@worker_id} due to exception:\n"	+ e.message+"\n"+e.backtrace.join("\n"))
  #raise e
end

#process_object(obj) ⇒ Object



21
22
23
# File 'lib/scbi_mapreduce/worker.rb', line 21

def process_object(obj)

end

#receive_initial_config(obj) ⇒ Object



15
16
17
18
# File 'lib/scbi_mapreduce/worker.rb', line 15

def receive_initial_config(obj)


end

#receive_object(obj) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
# File 'lib/scbi_mapreduce/worker.rb', line 54

def receive_object(obj)

  if @@count < 0
    @@count += 1
    # receive initial config
    if obj != :no_initial_config then
      receive_initial_config(obj[:initial_config])

      $WORKER_LOG.info('Initial config: received')
    else
      $WORKER_LOG.info('Initial config: empty config')
    end
    # At first iteration, start worker
    starting_worker
  else
    $WORKER_LOG.info("received:"+obj.to_s)
    
    if (obj == :quit) || @@want_to_exit_worker
      $WORKER_LOG.info('Quit received')
      
      stop_worker
      
    elsif @@want_to_exit_worker
      $WORKER_LOG.info('Want to exit worker')
      stop_worker
    else
      @@count += 1

      # OJO  - HAY QUE PASAR EL MODIFIED OBJECT
      #	 operation = proc {
      #	 							# calculations
      #								obj=process_object(obj)
      #								#puts '.' + obj.seq_name
      #								#return obj
      #							}
      #
      #							callback = proc { |modified_obj|
      #								send_object(modified_obj)
      #							}
      #
      #							EventMachine.defer(operation, callback)
      #send_object(obj)


      begin

        modified_data=process_object(obj.data)
        obj.data = modified_data

        # if obj.job_identifier==3
        #   sleep 15
        # end

        send_object(obj)

      rescue Exception => e
        $WORKER_LOG.error("Error processing object\n" + e.message + ":\n" + e.backtrace.join("\n"))
        exception= WorkerError.new('Message',e,@@worker_id,obj)
        send_object(exception)

      end


    end
  end
end

#starting_workerObject



26
27
28
29
# File 'lib/scbi_mapreduce/worker.rb', line 26

def starting_worker


end

#stop_workerObject



126
127
128
129
130
131
132
133
# File 'lib/scbi_mapreduce/worker.rb', line 126

def stop_worker
  $WORKER_LOG.info "Closing  connection with WORKER"
  $WORKER_LOG.info("Worker processed #{@@count} chunks")
  
  close_connection
  EventMachine::stop_event_loop
  closing_worker
end

#unbindObject



121
122
123
124
# File 'lib/scbi_mapreduce/worker.rb', line 121

def unbind
  $WORKER_LOG.info "EXITING WORKER"
  EventMachine::stop_event_loop
end

#worker_connectedObject



31
32
33
# File 'lib/scbi_mapreduce/worker.rb', line 31

def worker_connected

end