def __init__(self, manager_handler, stopper, worker_id): FragmentedStringReceiverWorker.__init__(self, manager_handler, stopper) self.thread_tag = "[Worker ] [API-R ]" self.worker_id = worker_id # send request to the worker self.worker_thread_id = self.manager_handler.process_response( self.manager_handler.isocket_handler.send_request( self.worker_id, "dapi_res"))
def process_request(self, command, data): logger.debug("[Worker ] [Request-R ]: '{0}'.".format(command)) if command == 'echo-m': return 'ok-m ', data.decode() elif command == 'sync_m_c': cmf_thread = WorkerProcessMasterFiles(manager_handler=self, filename=data, stopper=self.stopper) cmf_thread.start() return 'ack', self.set_worker_thread(command, cmf_thread, data) elif command == 'sync_m_c_ok': logger.info( "[Worker ] [Integrity ]: The master has verified that the integrity is right." ) self.integrity_received_and_processed.set() return 'ack', "Thanks2!" elif command == 'sync_m_c_err': logger.info( "[Worker ] [Integrity ]: The master was not able to verify the integrity." ) self.integrity_received_and_processed.set() return 'ack', "Thanks!" elif command == 'file_status': master_files = get_files_status('master', get_md5=True) worker_files = get_files_status('worker', get_md5=True) files = master_files files.update(worker_files) return 'json', json.dumps(files) elif command == 'string': string_sender_thread = FragmentedStringReceiverWorker( manager_handler=self, stopper=self.stopper) string_sender_thread.start() return 'ack', self.set_worker_thread(command, string_sender_thread) elif command == 'dapi': self.manager.add_api_request('None ' + data.decode()) return 'ack', 'Request is being processed' elif command == "dapi_res": string_receiver = FragmentedAPIResponseReceiver( manager_handler=self, stopper=self.stopper, worker_id=data.decode()) string_receiver.start() return 'ack', self.set_worker_thread(command, string_receiver) elif command == 'err-is': worker_id, err_msg = data.decode().split(' ', 1) self.isocket_handler.send_request(command=command, data=err_msg, worker_name=worker_id) return 'ack', 'thanks' else: return ClientHandler.process_request(self, command, data)
def process_cmd(self, command, data): requests = {'fwd_new':'new_f_r', 'fwd_upd':'update_f_r', 'fwd_end':'end_f_r'} data = data.decode() if data is not None else data if command == 'fwd_new': self.start_time = time.time() return self.forward_msg(requests[command], data) elif command == 'fwd_upd': self.update(data) return self.forward_msg(requests[command], data) elif command == 'fwd_end': self.close_reception(data) self.end_time = time.time() self.total_time = self.end_time - self.start_time return self.forward_msg(requests[command], data) else: return FragmentedStringReceiverWorker.process_cmd(self, command, data)
def unlock_and_stop(self, reason, send_err_request=None): if reason == 'error': self.manager_handler.isocket_handler.send_request(self.worker_id, 'err-is', send_err_request) FragmentedStringReceiverWorker.unlock_and_stop(self, reason, None)