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



145
146
147
# File 'lib/scbi_mapreduce/worker.rb', line 145

def self.controlled_exit_worker
  @@want_to_exit_worker=true
end

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



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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/scbi_mapreduce/worker.rb', line 149

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_us

  EM.error_handler{ |e|
    $WORKER_LOG.error(e.message + ' => ' + e.backtrace.join("\n"))
  }
  
  Signal.trap("CONT") do
    $WORKER_LOG.info("SIGCONT: Worker: #{@@worker_id} with PID: #{Process.pid} sleeping 15 seconds before waking up")
    puts "SIGCONT: Worker: #{@@worker_id} with PID: #{Process.pid} sleeping 15 seconds before waking up"
    
    sleep 15
  end

  EventMachine::run {

    EventMachine::connect ip, port, self
    $WORKER_LOG.info "Worker: #{@@worker_id} with PID: #{Process.pid} connected to #{ip}:#{port}"

  }

  total_seconds = Time.now_us-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
120
121
122
123
124
125
126
127
128
129
# 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
    elsif (obj== :sleep)
      $WORKER_LOG.info('Sleeping 10 secs')
      sleep 10
      send_object(:waking_up)
    else
      @@count += 1
      obj.worker_identifier=@@worker_id.to_i

      # 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
        
        obj.start_worker_time!
        
        modified_data=process_object(obj.data)
        obj.data = modified_data
        
        obj.end_worker_time!
        
        # 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



136
137
138
139
140
141
142
143
# File 'lib/scbi_mapreduce/worker.rb', line 136

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



131
132
133
134
# File 'lib/scbi_mapreduce/worker.rb', line 131

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