def run(self): if 'timer' in self.mission.trigger.keys(): interval = self.mission.trigger['timer'] while not self.lock.wait(timeout=interval): m_id = self.mission.get_id() time = datetime.datetime.now(datetime.timezone.utc).isoformat() works = [] for sid, w in self.leader.work_cache: if w.purpose != m_id: continue works.append({ "time": w.time, "place": self.leader.subordinates[sid].place, "values": w.values }) report = Report(time, self.leader.place, self.mission.purpose, works) url = "{0}subordinates/{1}/report".\ format(self.leader.superior_ep, self.leader.id) res, err = rest.post(url, json=report.to_dict()) if err is not None: # TODO: エラー処理ちゃんとやる # 本当に接続先がダウンしてる場合、ただのDoSになってしまう logger.error('In WorkingThread, failed to post report') logger.error('> err: {0}', err) self.leader.work_cache = \ [(sid, w) for sid, w in self.leader.work_cache if w.purpose != m_id] else: pass
def shutdown(self): url = "{0}commanders/{1}".format(self.recruiter_ep, self.id) res, err = rest.delete(url) # type: Response, str if err is not None: logger.error("Removing commander info from recruiter is failed.") return False return True
def run(self): while not self.lock.wait(timeout=self.interval): url = self.soldier.superior_ep + "subordinates/" + self.soldier.id res, err = rest.get(url, etag=self.etag) if err is not None: # TODO: エラー処理ちゃんとやる logger.error('In HeartBeat, failed to post report') logger.error('> err: {0}', err) break if res.status_code == 304: continue self.etag = res.headers['ETag'] info = SoldierInfo.make(res.json()['info']) logger.info([str(m) for m in info.orders]) # 以前に受理されているorderにはあるが新規のorderリストには無い # (消された)orderを消す oid_list = [o.get_id() for o in info.orders] for old_oid in self.soldier.orders.keys(): if old_oid not in oid_list: self.soldier.remove_order(old_oid) for m in info.orders: self.soldier.accept_order(m) self.soldier.shutdown()
def run(self): while not self.lock.wait(timeout=self.interval): url = self.soldier.superior_ep + "subordinates/" + self.soldier.id res, err = rest.get(url, etag=self.etag) if err is not None: # TODO: エラー処理ちゃんとやる logger.error('In HeartBeat, failed to post report') logger.error('> err: {0}', err) break if res.status_code == 304: continue self.etag = res.headers['ETag'] info = SoldierInfo.make(res.json()['info']) logger.info([str(m) for m in info.orders]) # 以前に受理されているorderにはあるが新規のorderリストには無い # (消された)orderを消す oid_list = [o.get_id() for o in info.orders] for old_oid in self.soldier.orders.keys(): if old_oid not in oid_list: self.soldier.remove_order(old_oid) for m in info.orders: self.soldier.accept_order(m) self.soldier.shutdown()
def run(self): if 'timer' in self.mission.trigger.keys(): interval = self.mission.trigger['timer'] while not self.lock.wait(timeout=interval): m_id = self.mission.get_id() time = datetime.datetime.now(datetime.timezone.utc).isoformat() works = [] for sid, w in self.leader.work_cache: if w.purpose != m_id: continue works.append({"time": w.time, "place": self.leader.subordinates[sid].place, "values": w.values}) report = Report(time, self.leader.place, self.mission.purpose, works) url = "{0}subordinates/{1}/report".\ format(self.leader.superior_ep, self.leader.id) res, err = rest.post(url, json=report.to_dict()) if err is not None: # TODO: エラー処理ちゃんとやる # 本当に接続先がダウンしてる場合、ただのDoSになってしまう logger.error('In WorkingThread, failed to post report') logger.error('> err: {0}', err) self.leader.work_cache = \ [(sid, w) for sid, w in self.leader.work_cache if w.purpose != m_id] else: pass
def shutdown(self): url = "{0}commanders/{1}".format(self.recruiter_ep, self.id) res, err = rest.delete(url) # type: Response, str if err is not None: logger.error("Removing commander info from recruiter is failed.") return False return True
def _heart_watch(self, sid): while self.sub_heart_waits[sid].wait(timeout=180): # timeoutまでにevent.setされたら待ち続行 # timeoutしたらK.I.A. self.sub_heart_waits[sid].clear() if self.remove_subordinate(sid): # removeが失敗すれば(そもそも削除済であれば)実行しない logger.error("へんじがない ただのしかばねのようだ :{0}".format(sid))
def _heart_watch(self, sid): while self.sub_heart_waits[sid].wait(timeout=120): # timeoutまでにevent.setされたら待ち続行 # timeoutしたらK.I.A. self.sub_heart_waits[sid].clear() if self.remove_subordinate(sid): # removeが失敗すれば(そもそも削除済であれば)実行しない logger.error("へんじがない ただのしかばねのようだ :{0}".format(sid))
def __init__(self, uri): import pymongo import re match_result = re.match(r"mongodb://(.*?)/(.*?)/(.*)", uri) if match_result is None: logger.error("uri is not mongodb-uri: {0}".format(uri)) return host = match_result.group(1) db_name = match_result.group(2) col_name = match_result.group(3) self.col = pymongo.MongoClient(host)[db_name][col_name]
def __init__(self, uri): import pymongo import re match_result = re.match(r"mongodb://(.*?)/(.*?)/(.*)", uri) if match_result is None: logger.error("uri is not mongodb-uri: {0}".format(uri)) return host = match_result.group(1) db_name = match_result.group(2) col_name = match_result.group(3) self.col = pymongo.MongoClient(host)[db_name][col_name]
def shutdown(self): self.heartbeat_thread.lock.set() [w.lock.set() for w in self.working_threads] if self.superior_ep == "": return True url = "{0}subordinates/{1}".format(self.superior_ep, self.id) res, err = rest.delete(url) if err is not None: logger.error("Removing soldier info from leader is failed.") return False return True
def shutdown(self): self.heartbeat_thread.lock.set() [w.lock.set() for w in self.working_threads] if self.superior_ep == "": return True url = "{0}subordinates/{1}".format(self.superior_ep, self.id) res, err = rest.delete(url) if err is not None: logger.error("Removing soldier info from leader is failed.") return False return True
def raise_exception(ex): name = type(ex).__name__ cause = str(ex) logger.error(name, cause) exit()