async def update_user_info(self, user_id, data, refer=""): try: rockutils.pprint( f"{f'[Refer: {refer}] ' if refer != '' else ''}Updating information for {guild_id}", prefix="update_user_info", prefix_colour="light green") await r.table("users").get(int(user_id) ).update(data).run(self.bot.connection) return True except: return False
async def submit(job_key,cluster): global activejobs cluster = int(cluster) data = await request.get_json(force=True) rockutils.pprint(f"Recieved job results from port {cluster} with key {job_key[:10]}...", prefix=">", prefix_colour="yellow", text_colour="light yellow") if data and "internal" in job_key: clusterstatus[str(cluster)].update(data) return jsonify({"success": True}) else: if data and (not cluster is None) and (job_key in activejobs): activejobs[job_key][cluster] = data return jsonify({"success": True}) else: return jsonify({"success": True, "error": "InvalidArgument"})
async def slave(cluster): cluster = int(cluster) global clusterjobs if not cluster in clusterjobs: clusterjobs[cluster] = [] rockutils.pprint(f"Added cluster {cluster}", prefix="IPC-Slave", prefix_colour="light blue", text_colour="cyan") last_pong = time.time() pongjob = { "op": "details", "args": "", "key": f"internal.{cluster}" } clusterjobs[cluster].append(pongjob) while True: jobs = clusterjobs[cluster] if time.time()-last_pong > 15: jobs.append(pongjob) last_pong = time.time() if len(jobs) > 0: json_resp = json.dumps(jobs) clusterjobs[cluster] = [] await websocket.send(json_resp) await asyncio.sleep(0.05)
async def get_user_info(self, user_id, refer=""): rockutils.pprint( f"{f'[Refer: {refer}] ' if refer != '' else ''}Getting information for {user_id}", prefix="get_user_info", prefix_colour="light green") user_data = await r.table("users").get(int(user_id) ).run(self.bot.connection) update = False new = False user = self.bot.get_user(self, int(user_id)) if user_data is None and user: user_data = copy.deepcopy( rockutils.load_json("data/default_user.json")) user_data['id'] = int(user_id) user_data['details']['user_addition'] = math.ceil(time.time()) user_data['previous_names'].append( [user.name, math.ceil(time.time())]) new = True if user: if not self.bot.cluster_id in user_data['last_mutual']: user_data['last_mutual'][self.bot.cluster_id] = 0 update = True if time.time() - user_data['last_mutual'][ self.bot.cluster_id] > 600: user_data['mutual'][self.bot.cluster_id] = self.mutual_guilds( self, user) user_data['last_mutual'][self.bot.cluster_id] = time.time() update = True if time.time() - user_data['details'] > 600: user_data['details']['avatar'] = [ user.avatar_url_as(format="jpeg", size=64), user.avatar_url_as(format="png", size=256) ] if user.name != user_data['details']['name']: user_data['previous_names'].append( [user.name, math.ceil(time.time())]) user_data['details']['name'] = user.name user_data['details']['discriminator'] = user.discriminator user_data['details']['user_creation'] = math.ceil( user.created_at.timestamp()) user_data['details']['update'] = time.time() update = True if update or new: if new: rockutils.pprint(f"Creating information for {user_id}", prefix="get_user_info", prefix_colour="light green") await r.table("users").insert(user_data).run( self.bot.connection) else: await update_user_info(self, user_id, user_data, refer="get_user_info") return user_data
async def get_guild_info(self, guild_id, refer=""): rockutils.pprint( f"{f'[Refer: {refer}] ' if refer != '' else ''}Getting information for {guild_id}", prefix="get_guild_info", prefix_colour="light green") guild_data = await r.table("guilds").get(int(guild_id) ).run(self.bot.connection) update = False new = False guild = self.bot.get_guild(self, int(guild_id)) if guild_data is None and guild: guild_data = copy.deepcopy( rockutils.load_json("data/default_guild.json")) guild_data['id'] = int(guild_id) guild_data['details']['guild_addition'] = math.ceil(time.time()) mew = True guild_data['cluster'] = self.bot.cluster_id if guild: if time.time() - guild_data['details']['update'] > 600: guild_data['details']['splash'] = guild.splash guild_data['details']['region'] = str(guild.region) guild_data['details']['icon'] = [ guild.icon_url_as(format="jpeg", size=64), guild.icon_url_as(format="png", size=256) ] guild_data['details']['name'] = guild.name guild_data['details']['guild_creation'] = math.ceil( guild.created_at.timestamp()) guild_data['details']['owner'] = str(guild.owner.id) guild_data['details']['update'] = time.time() update = True if time.time() - guild_data['details']['detailed']['update'] > 300: detailed = self.extract_guild_detailed(self, guild) guild_data['details']['detailed'] = detailed guild_data['details']['detailed']['update'] = time.time() guild_data['details']['members']['bots'] = detailed['bots'] guild_data['details']['members']['members'] = detailed[ 'members'] guild_data['details']['members'][ 'all'] = detailed['bots'] + detailed['members'] del guild_data['details']['detailed']['bots'] del guild_data['details']['detailed']['members'] update = True if time.time( ) - guild_data['details']['guild']['channels']['update'] > 1800: channels = self.extract_channels(self, guild) guild_data['guild']['channels'] = channels guild_data['guild']['channels']['update'] = time.time() update = True if update or new: if new: rockutils.pprint(f"Creating information for {guild_id}", prefix="get_guild_info", prefix_colour="light green") await r.table("guilds").insert(guild_data).run( self.bot.connection) else: await update_guild_info(self, guild_id, guild_data, refer="get_guild_info") return guild_data
async def create_job(request, op="", args="", recep=""): global activejobs global clusterjobs if request: head = request.headers reqtimeout = head.get("timeout",10) if op == "": op = head.get("op") if args == "": args = head.get("args") if recep == "": recep = head.get("recep") job_key = "".join(random.choices(string.ascii_letters,k=32)) rockutils.pprint(f"Recieved job results with key {job_key[:10]}...", prefix=">", prefix_colour="red", text_colour="light red") rockutils.pprint(f"- recepients: {recep}", prefix=">", prefix_colour="red", text_colour="light red") rockutils.pprint(f"- opcode: {op}", prefix=">", prefix_colour="red", text_colour="light red") recepients = [] if recep == "*": recepients = list(range(0,config['sharding']['clusters'])) else: try: recepients = [int(r) for r in json.loads(recep)] except: recepients = [int(recep)] activejobs[job_key] = {} headers = { "op": op, "args": args, "key": job_key } for recep in recepients: if not recep in clusterjobs: clusterjobs[recep] = [] clusterjobs[recep].append(headers) time_start = time.time() wait = time_start + reqtimeout while len(activejobs[job_key]) != len(recepients) and time.time() < wait: if len(activejobs[job_key]) == len(recepients): break await asyncio.sleep(0.05) responce = { "key": job_key, "recep": recep, "op": op, "args": args, "job_start": time_start, "data": {} } for cluster,data in activejobs[job_key].items(): responce['data'][cluster] = data time_end = time.time() responce['job_duration'] = time_end - time_start del activejobs[job_key] rockutils.pprint(f"Finished job with key {job_key[:10]}...", prefix="<", prefix_colour="green", text_colour="light green") rockutils.pprint(f"- recepients: {recepients}", prefix="<", prefix_colour="green", text_colour="light green") rockutils.pprint(f"- opcode: {op}", prefix="<", prefix_colour="green", text_colour="light green") rockutils.pprint(f"- time taken: {math.ceil(responce['job_duration']*1000)}ms", prefix="<", prefix_colour="green", text_colour="light green") return responce
exitcode = { "restart": 1, "hang": 2, "exit": 0 } os._exit(exitcode[opcode]) else: return jsonify({"success":False, "error":"InvalidArgument: No such cluster"}) else: return jsonify({"success":False, "error":"InvalidArgument: No such opcode"}) @app.route("/api/request/", methods=['GET','POST']) async def requestjob(): head = request.headers op = head.get("op") args = head.get("args") recep = head.get("recep") return jsonify(await create_job(request, op, args, recep)) @app.route("/admin/dashboard", methods=['GET','POST']) async def apidashboard(): global clusterstatus if request.method == "POST": get_info() return jsonify(clusterstatus) else: return await render_template("admin_dashboard.html", clusters=clusterstatus.keys()) rockutils.pprint("Starting up", prefix="IPC-Server") get_info() app.run(host="0.0.0.0", port=config['ipc_port'])