def terminate_worker(nickname=None, pid=None): logger.info("{}".format(worker_dict)) if pid is None: pid = get_pid_by_nickname(redis_cli=__redis__, nickname=nickname) os.kill(pid, signal.SIGTERM) i = 0 while worker_dict[nickname]._popen is None and i < 30: time.sleep(0.1) i += 1 worker_dict[nickname]._popen.wait(1) worker_dict.pop(nickname)
def main(what=None): try: if what: if what[0] != "HySAS": print("Please input HySAS for starting") exit(0) else: print("Welcome to HySAS! Following is the awesome!!!") logo = "◆ ◆ ◆\n"\ "◆ ◆ ◆◆◆◆◆ ◆◆ ◆◆◆◆◆\n"\ "◆ ◆ ◆◆◆◆◆ ◆◆◆ ◆◆◆\n"\ "◆ ◆ ◆◆ ◆◆ ◆◆◆◆ ◆◆◆◆ ◆◆◆ \n"\ "◆◆◆◆◆◆ ◆◆ ◆◆ ◆◆◆◆◆ ◆◆ ◆◆ ◆◆◆◆◆ \n"\ "◆ ◆ ◆◆◆◆ ◆◆◆ ◆◆◆◆◆◆ ◆◆◆ \n"\ "◆ ◆ ◆◆◆ ◆◆◆ ◆◆◆ ◆◆ ◆◆◆ ◆◆◆ ◆◆◆ \n"\ "◆ ◆ ◆◆ ◆◆◆◆◆◆ ◆◆ ◆◆◆ ◆◆◆◆◆◆ \n"\ " ◆◆ \n"\ " ◆◆ \n" print(logo) # open a thread for the Worker of Monitor start_worker(worker_name="Monitor", nickname="Monitor") logger.info("Monitor has started") # 开启Web if len(what) == 1: # 没指定http端口,不开启Tornado pass else: port = int(what[1]) start_worker(worker_name="Web", nickname="Tornado") # 绑定退出信号 bind_quit_signals() redis_conn = get_vendor("DB").get_redis() command_listener = redis_conn.pubsub() channel_name = "HySAS.Command" command_listener.subscribe([channel_name]) # 数据关联 态势分析 start_worker(worker_name="Process", nickname="Process") for item in command_listener.listen(): # 阻塞式接收 if isinstance(item['data'], bytes): __command_handler__(item["data"]) else: print("HySAS What?") except Exception as e: traceback.print_exc() logger.error("{}".format(e)) print("Hail error?")
def cache_ranks(cron_cursor): time = Timer() time.start() logger.info("Caching ranks... ") cron_cursor.execute("SELECT extID FROM users WHERE isBanned = 0 ORDER BY stars") Leaderboards = cron_cursor.fetchall() Leaderboards.reverse() Ranks.clear() UserRank = 0 for User in Leaderboards: UserRank += 1 Ranks[str(User[0])] = UserRank time.end() logger.info(f"Done! {time.ms_return()}ms")
def cache_comment_bans(cron_cursor): """Caches comment bans so a lookup doesn't have to be made.""" time = Timer() time.start() logger.info("Caching comment bans...") timestamp = round(pytime.time()) #so expired bans dont get cached cron_cursor.execute("SELECT accountID, endTimestamp, reason FROM commentbans WHERE endTimestamp > %s", (timestamp,)) comment_bans = cron_cursor.fetchall() CommentBanCache.clear() for ban in comment_bans: CommentBanCache[ban[0]] = { "end_time" : ban[1], "reason" : ban[2] } time.end() logger.info(f"Done with {len(comment_bans)} comment bans cached! {time.ms_return()}ms")
def cache_server_stats(cron_cursor): """Caches server statistics.""" time = Timer() time.start() logger.info("Caching server statictics...") cron_cursor.execute("SELECT COUNT(*) FROM accounts") ServerStatsCache["registered"] = cron_cursor.fetchone()[0] week_ago = round(pytime.time()) - 604800 cron_cursor.execute("SELECT COUNT(*) FROM accounts WHERE registerDate > %s", (week_ago,)) ServerStatsCache["registered_in_last_week"] = cron_cursor.fetchone()[0] cron_cursor.execute("SELECT COUNT(*) FROM levels WHERE uploadDate > %s", (week_ago,)) ServerStatsCache["levels_in_last_week"] = cron_cursor.fetchone()[0] time.end() logger.info(f"Done! {time.ms_return()}ms")
def max_star_count_ban(cron_cursor) -> None: """[CheatlessAC Cron] Bans people who have a star count higher than the total starcount of the server.""" # TODO : Make the same thing for usercoins and regular coins if UserConfig["CheatlessCronChecks"] and UserConfig["CheatlessAC"]: time = Timer() time.start() logger.info("Running CheatlessAC Cron Starcount Check... ") TotalStars = 187 #from RobTop levels #get all star rated levels cron_cursor.execute("SELECT starStars FROM levels WHERE starStars > 0") StarredLevels = cron_cursor.fetchall() #add em all up for Level in StarredLevels: TotalStars += Level[0] #count query cron_cursor.execute("SELECT COUNT(*) FROM users WHERE stars > %s", (TotalStars,)) BannedCount = cron_cursor.fetchone()[0] #ban em cron_cursor.execute("UPDATE users SET isBanned = 1 WHERE stars > %s", (TotalStars,)) mydb.commit() time.end() logger.info(f"Done with {BannedCount} users banned! {time.ms_return()}ms")
def calculate_cp(cron_cursor): """Cron job that calculates CP for the whole server.""" time = Timer() time.start() logger.info("Beginning to calculate CP... ") #Cronjob code cp_values = {} cron_cursor.execute("UPDATE users SET creatorPoints = 0") #set everyones cp to 0 as we wont be calculating everyone #now we get specific levels that match our criteria cron_cursor.execute("SELECT userID, starStars, starFeatured, starEpic, awarded, magic FROM levels WHERE starStars > 0 OR starFeatured > 0 OR starEpic > 0 OR awarded > 0 OR magic > 0") rated_levels = cron_cursor.fetchall() for level in rated_levels: if level[0] not in list(cp_values.keys()): cp_values[level[0]] = 0 cp_values[level[0]] += calc_cp_for_level(level) #finally apply calcs for cp in list(cp_values.keys()): cron_cursor.execute("UPDATE users SET creatorPoints = %s WHERE userID = %s LIMIT 1", (cp_values[cp], cp)) mydb.commit() time.end() logger.info(f"Done! {time.ms_return()}ms")
def __on_termination__(sig, frame): logger.info("The HySAS Server is about to terminate, pid:{}".format( os.getpid())) sys.exit(0)