Class: ScbiMapreduce::Worker
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- ScbiMapreduce::Worker
- Includes:
- EM::P::ObjectProtocol
- Defined in:
- lib/scbi_mapreduce/worker.rb
Class Method Summary collapse
Instance Method Summary collapse
- #closing_worker ⇒ Object
-
#initialize(*args) ⇒ Worker
constructor
A new instance of Worker.
- #post_init ⇒ Object
- #process_object(obj) ⇒ Object
- #receive_initial_config(obj) ⇒ Object
- #receive_object(obj) ⇒ Object
- #starting_worker ⇒ Object
- #stop_worker ⇒ Object
- #unbind ⇒ Object
- #worker_connected ⇒ Object
Constructor Details
#initialize(*args) ⇒ Worker
Returns a new instance of Worker.
41 42 43 44 |
# File 'lib/scbi_mapreduce/worker.rb', line 41 def initialize(*args) super end |
Class Method Details
.start_worker(worker_id, ip, port, log_file = nil) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/scbi_mapreduce/worker.rb', line 122 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 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. + ' => ' + 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_worker ⇒ Object
35 36 37 38 |
# File 'lib/scbi_mapreduce/worker.rb', line 35 def closing_worker end |
#post_init ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/scbi_mapreduce/worker.rb', line 46 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.+"\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
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 |
# File 'lib/scbi_mapreduce/worker.rb', line 55 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 if obj == :quit 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 send_object(obj) rescue Exception => e $WORKER_LOG.error("Error processing object\n" + e. + ":\n" + e.backtrace.join("\n")) exception= WorkerError.new('Message',e,@@worker_id,obj) send_object(exception) end end end end |
#starting_worker ⇒ Object
26 27 28 29 |
# File 'lib/scbi_mapreduce/worker.rb', line 26 def starting_worker end |
#stop_worker ⇒ Object
116 117 118 119 120 |
# File 'lib/scbi_mapreduce/worker.rb', line 116 def stop_worker close_connection EventMachine::stop_event_loop closing_worker end |
#unbind ⇒ Object
111 112 113 114 |
# File 'lib/scbi_mapreduce/worker.rb', line 111 def unbind $WORKER_LOG.info "EXITING WORKER" EventMachine::stop_event_loop end |
#worker_connected ⇒ Object
31 32 33 |
# File 'lib/scbi_mapreduce/worker.rb', line 31 def worker_connected end |