From a89138ebdb67e404f724e5ee425cc5c49664498c Mon Sep 17 00:00:00 2001 From: pengtao Date: Fri, 20 Aug 2021 16:15:59 +0800 Subject: [PATCH] add new func with wjtx close service --- .gitignore | 2 ++ main.py | 2 -- ops/wjtx/route.py | 7 ++++++ requests.txt | 3 ++- scripts/wjtx/tools.py | 57 +++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 66 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 6b081fe..0d0a9a3 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ __pycache__/ # Distribution / packaging .Python +.idea build/ develop-eggs/ dist/ @@ -278,3 +279,4 @@ dmypy.json # Cython debug symbols cython_debug/ +.vscode/settings.json diff --git a/main.py b/main.py index c5d90dc..f48a519 100644 --- a/main.py +++ b/main.py @@ -89,11 +89,9 @@ def get_query(background_tasks: BackgroundTasks, q: Optional[str] = None): @app.post("/send-notification/{email}") async def send_notification(request: Request, email: str, background_tasks: BackgroundTasks, q: str = Depends(get_query)): - # pdb.set_trace() redis_client = request.app.state.redis keys = await redis_client.get("online_devices") logger.info("get keys = {}".format(keys)) - # print(keys) message = f"message to {email} \n" background_tasks.add_task(write_log, message) return {"message": "Message sent"} diff --git a/ops/wjtx/route.py b/ops/wjtx/route.py index 5a4e356..ec885ed 100644 --- a/ops/wjtx/route.py +++ b/ops/wjtx/route.py @@ -19,3 +19,10 @@ async def change_worldboss(request: Request, serverid: int, values: int) -> JSON wj = WjtxTools() await wj.change_worldboss(serverid, values) return JSONResponse(status_code=200, content={"message": "change {0} worldboss to {1}!".format(serverid, values)}) + + +@router.get("/stop_serivce", tags=["wjtx"], summary="stop wjtx service") +async def stop_service(request: Request, serverid: int) -> JSONResponse: + wj = WjtxTools() + await wj.stop_service(serverid) + return JSONResponse(status_code=200, content={"message": "stop {0} services!".format(serverid)}) diff --git a/requests.txt b/requests.txt index b15df9a..4f032d1 100644 --- a/requests.txt +++ b/requests.txt @@ -3,4 +3,5 @@ fastapi==0.65.2 fastapi-mail==0.3.7 GitPython==3.1.18 pydantic==1.8.2 -redis==3.2.1 \ No newline at end of file +redis==3.2.1 +uvicorn==0.15.0 \ No newline at end of file diff --git a/scripts/wjtx/tools.py b/scripts/wjtx/tools.py index 79d1717..f912232 100644 --- a/scripts/wjtx/tools.py +++ b/scripts/wjtx/tools.py @@ -86,7 +86,9 @@ class WjtxTools: return False def _restart_service(self): - global_redis = redis.Redis(host=self.redis_conf['host'], port=self.redis_conf['port'], charset='utf8', + global_redis = redis.Redis(host=self.redis_conf['host'], + port=self.redis_conf['port'], + charset='utf8', db=self.redis_conf.get('db') or 0) host_info = json.loads(global_redis.hget('game_info', self.serverid)) hostip = host_info.get('innerIp', None) @@ -109,7 +111,9 @@ class WjtxTools: return False def check_serverid(self): - global_redis = redis.Redis(host=self.redis_conf['host'], port=self.redis_conf['port'], charset='utf8', + global_redis = redis.Redis(host=self.redis_conf['host'], + port=self.redis_conf['port'], + charset='utf8', db=self.redis_conf.get('db') or 0) server_state = global_redis.hget('server_state', self.serverid) if int(server_state) != 12: @@ -128,3 +132,52 @@ class WjtxTools: return False else: return False + + async def _get_alive_host(self) -> list: + alive_serverids = list() + global_redis = redis.Redis(host=self.redis_conf['host'], + port=self.redis_conf['port'], + charset='utf8', + db=self.redis_conf.get('db') or 0) + + data = global_redis.hgetall('game_info') + for line in data.values(): + recode = json.loads(line) + # all_serverids.append(recode['areaId']) + if (int(recode['areaId']) % 100 + 9000) == int( + recode['tcpPort']) and recode['status'] == 1: + alive_serverids.append(recode['areaId']) + all_serverids = list(set(alive_serverids)) + return all_serverids.sort() + + def _get_hostip_via_serverid(self, serverids) -> list: + global_redis = redis.Redis(host=self.redis_conf['host'], + port=self.redis_conf['port'], + charset='utf8', + db=self.redis_conf.get('db') or 0) + all_hosts = list() + for serverid in serverids: + hostip = json.loads(global_redis.hget('game_info', serverid)).get( + 'innerIp', None) + if hostip: + all_hosts.append(hostip) + return all_hosts + + async def stop_service(self, serverids: list) -> bool: + if not serverids: + all_services = await self._get_alive_host() + else: + all_services = serverids + hosts = self._get_hostip_via_serverid(all_services) + hostfilename = write_host(hosts) + stop_cmd = "/usr/bin/pkill java" + time.sleep(1) + myansible = AnsiInterface(hostfilename) + resule = myansible.exec_command(stop_cmd) + log.debug(resule) + if not (resule['failed'] or resule['unreachable']): + log.info(f"stop {self.serverid} success!") + return True + else: + log.error(f"stop {self.serverid} failed,output was {resule}") + return False