async def specific_config_setter(self, cmd: Cmd, filename: str, is_monitor: bool): success, missing = cmd.has_valid_args(self.setter_args) if success: payload = cast(Dict[str, Any], cmd.payload) cp = os.path.sep.join(( self.config["GlobalConfig"]["config_path"], "monitors" if is_monitor else "scrapers", filename, )) with open( cp, "r", ) as rf: f = json.load(rf) f[payload["name"]] = payload["payload"] with open( cp, "w", ) as wf: json.dump(f, wf) else: r = badResponse() r.error = ERRORS.MISSING_PAYLOAD_ARGS r.info = f"{missing}" return r
async def on_stop_scraper(self, cmd: Cmd) -> Response: r = badResponse() success, missing = cmd.has_valid_args(self.stop_args) if success: payload = cast(Dict[str, Any], cmd.payload) socket = f"{self.config['GlobalConfig']['socket_path']}/Scraper.{payload['name']}" command = Cmd() command.cmd = COMMANDS.STOP self.general_logger.debug(f"Sending STOP to {socket}...") r = await self.make_request(socket, command) self.general_logger.debug(f"Sent STOP to {socket}") else: r.error = ERRORS.MISSING_PAYLOAD_ARGS r.info = f"Missing arguments: {missing}" return r
async def on_get_monitor_shoes(self, cmd: Cmd) -> Response: success, missing = cmd.has_valid_args(self.getter_args) if success: payload = cast(Dict[str, Any], cmd.payload) c = Cmd() c.cmd = COMMANDS.SET_SHOES r = await self.make_request( f"{self.config['GlobalConfig']['socket_path']}/Monitor.{payload['name']}", c, ) return r else: r = badResponse() r.error = ERRORS.MISSING_PAYLOAD_ARGS r.info = f"{missing}" return r
async def specific_config_getter(self, cmd: Cmd, command: COMMANDS, is_monitor: bool): success, missing = cmd.has_valid_args(self.getter_args) if success: payload = cast(Dict[str, Any], cmd.payload) c = Cmd() c.cmd = command r = await self.make_request( f"{self.config['GlobalConfig']['socket_path']}/{'Monitor' if is_monitor else 'Scraper'}.{payload['name']}", c, ) return r else: r = badResponse() r.error = ERRORS.MISSING_PAYLOAD_ARGS r.info = f"{missing}" return r
async def on_stop_monitor_scraper(self, cmd: Cmd) -> Response: r = badResponse() success, missing = cmd.has_valid_args(self.stop_args) if success: r1, r2 = await asyncio.gather(self.on_stop_monitor(cmd), self.on_stop_scraper(cmd)) r.error = (ERRORS.OK if not r1.error.value and not r2.error.value else ERRORS.MM_COULDNT_STOP_MONITOR_SCRAPER) r.info = f"Monitor: {r1.error.name}, Scraper: {r2.error.name}" if r.error.value and r.error: self.general_logger.warning( f"Couldn't stop monitor and scraper") kekmonitors.utils.tools.dump_error(self.general_logger, r) else: r.error = ERRORS.MISSING_PAYLOAD_ARGS r.info = f"Missing arguments: {missing}" return r
async def on_add_monitor(self, cmd: Cmd) -> Response: r = badResponse() success, missing = cmd.has_valid_args(self.add_args) if success: payload = cast(Dict[str, Any], cmd.payload) db_monitor = self.register_db["monitors"].find_one( {"name": payload["name"]}) if db_monitor: success, reason = await self.add_monitor( db_monitor["path"], payload) if success: r = okResponse() else: r.error = ERRORS.MM_COULDNT_ADD_MONITOR r.info = reason else: r.error = ERRORS.MONITOR_NOT_REGISTERED r.info = f"Tried to add monitor {payload['name']} but it was not found in the db. Did you start it at least once manually?" else: r.error = ERRORS.MISSING_PAYLOAD_ARGS r.info = f"Missing arguments: {missing}" return r