def get_current_user(self): try: user_id = self.session["user_id"] with sessionCM() as session: user = User.find_by_id(session, user_id) return user except Exception, e: logger.info(e.message)
def remove_job(self, job_id): self.scheduler.remove_job(job_id) with sessionCM() as session: job_record = Scheduler.find_by_scheduler_id(session, job_id) if job_record: job_record.status = CANCELLED session.add(job_record) session.commit()
def aps_callback(req_type, url, data, job_id, other_kwargs=None, retry=1): """ scheduler回调函数 TODO: 添加调用记录 :param req_type: 请求方式 post|get :param url: 请求地址 :param data: 请求数据 :param job_id: 在任务队列中的id :param other_kwargs: 其他 :param retry: 重试次数 :return: """ with sessionCM() as session: sched = Scheduler.find_by_scheduler_id(session, job_id) job = scheduler.get_job(job_id) next_run_time = job.next_run_time.strftime("%Y-%m-%d %H:%M:%S") logger.info("正在执行scheduler回调, 第%s次s请求:" % retry) logger.info(url) logger.info(data) try: if req_type == "post": res = requests.post(url=url, data=data) else: res = requests.get(url=url, params=data) res = res.json() if res["status"]: sched.update( session, **{ "status": ACTIVE if sched.trigger != "date" else END, "next_run_time": next_run_time }) else: sched.update( session, **{ "status": ERROR, "err_mess": res["message"], "next_run_time": next_run_time }) return res except Exception, e: if retry < 3: return aps_callback(req_type, url, data, job_id, other_kwargs=other_kwargs, retry=retry + 1) else: logger.info("POST fail {0}".format(e.message)) sched.update( session, **{ "status": ERROR, "err_mess": "POST fail {0}".format(e.message), "next_run_time": next_run_time }) return {"status": 0, "mess": e.message}
def get_all_current_monitor(): with sessionCM() as session: robots = Monitor.get_all_using_robot(session) for robot in robots: get_usage.delay(robot.public_ip) # # # get_all_current_monitor() # import requests # requests.post("http://monitor.kisu.top/api/all_monitor")
def get(self, *args, **kwargs): name = args[0] render_settings = self.gen_render_settings() with sessionCM() as session: gif_info = GifList.find_by_name(session, name) names = GifList.get_names(session) if not gif_info: raise HTTPError(404) render_settings["gif_name"] = name render_settings["gif_names"] = names render_settings["length"] = gif_info.length self.render("gif/sorry.html", **render_settings)
def set_info(cls, value): with redis.conn.pipeline() as pipe: with sessionCM() as session: try: pipe.watch(cls.REDIS_KEY, cls.LAST_UPDATE_TIME) pipe.multi() redis.set(cls.REDIS_KEY, value) time = Timer.now_time() redis.set(cls.LAST_UPDATE_TIME, time) pipe.execute() Jx3DailyRecord.create(session, update_time=time, info=json.dumps(cls.get_info())) except WatchError: pipe.reset()
def add_new_user(self): try: mobile = self.params.get("mobile") password = self.params.get("password") password2 = self.params.get("password2") if not mobile or not password: raise NullArgumentException if password != password2: raise ErrorArgumentError with sessionCM() as session: user = User.find_by_mobile(session, mobile) if user: return {"status": 0, "message": "用户已存在"} else: user = User.create(session, mobile, password) self.session["user_id"] = user.id return {"status": 1, "message": "注册成功!"} except NullArgumentException, e: return {"status": 0, "message": e.msg}
def log_in(self): mobile = self.params.get("mobile") pwd = self.params.get("pwd") mess = "" try: if not mobile or not pwd: raise NullArgumentException with sessionCM() as session: user = User.find_by_mobile(session, mobile) if not user: mess = {"status": 0, "message": "用户不存在!"} else: reg = user.check_password(pwd) if not reg: mess = {"status": 0, "message": "密码错误!"} else: self.session["user_id"] = user.id self.set_cookie("MY_WEB", "true") mess = {"status": 1, "message": "登陆成功!"} except NullArgumentException, e: mess = {"status": 0, "message": e.msg}
def auto_add_one(cls, force=False): last_time = redis.get(cls.LAST_UPDATE_TIME) if not force and last_time and not Timer.is_in_yesterday(last_time): send_to_master("剑网三自动更新出错", "上次更新时间错误!") return False with redis.conn.pipeline() as pipe: with sessionCM() as session: try: pipe.watch(cls.REDIS_KEY, cls.LAST_UPDATE_TIME) pipe.multi() res = redis.incr(cls.REDIS_KEY) if not res: raise RedisError time = Timer.now_time() redis.set(cls.LAST_UPDATE_TIME, Timer.now_time()) pipe.execute() current_jx3_info = json.dumps(cls.get_info()) Jx3DailyRecord.create(session, update_time=time, info=current_jx3_info) send_to_master("剑网三自动更新完成", current_jx3_info) return True except WatchError: pipe.reset()
def get_usage(public_ip): with sessionCM() as session: MonitorControl(public_ip, session).get_usage()
except Exception, e: self.session.rollback() record.error_times += 1 record.status = STATUS["OTHER_ERROR"] print traceback.format_exc(e) logger.error("获取状态信息失败:%s" % e.message) logger.error(traceback.format_exc(e)) finally: if record.error_times > 5: record.status = STATUS["STOPPED"] send_to_master("API出错", "%s机器连续五分钟获取不到信息, 设置状态为stop!" % record.name) self.session.add(record) self.session.commit() def add_usage_to_record(self): pass def exec_command(self, ssh, commannd): stdin, stdout, stderr = ssh.exec_command(commannd) str_out = stdout.read().decode() str_err = stderr.read().decode() if str_err != "": raise StdError(str_err) return str_out.strip() if __name__ == "__main__": from lib.sql.session import sessionCM with sessionCM() as session: MonitorControl("180.76.98.136", session).get_usage()
def add_my_job(self, trigger, res_type, res_url, job_id, job_name, schedule_args, schedule_type, func_args, user_id=0, remark="", job_store="default"): if trigger not in TRIGGER_LIST: raise ErrorArgumentError job_id = job_id or str(uuid.uuid1()) callback_args = [res_type, res_url, func_args, job_id] try: job = self.add_job_to_scheduler(trigger, job_id, job_name, schedule_args, callback_args, job_store) with sessionCM() as session: _scheduler = Scheduler.find_by_scheduler_id(session, job.id) info = { "request_url": res_url, "trigger": trigger, "action": job_name, "args": json.dumps(func_args), # json 序列化后的参数 "mold": schedule_type, "type": schedule_type, # sys "next_run_time": job.next_run_time.strftime("%Y-%m-%d %H:%M:%S"), "user_id": user_id, "scheduler_id": job.id, "extra": json.dumps(schedule_args), # 不同的trigger的不同参数 "remark": remark, "status": ACTIVE } if not _scheduler: Scheduler.create(session, **info) else: _scheduler.update(session, **info) mess = "job_id为%s的任务添加成功" % job_id logger.info(mess) return {"status": 1, "message": mess} except ConflictingIdError: mess = "job_id为%s的任务已经存在" % job_id logger.error(mess) return {"status": 0, "message": mess} except Exception, e: logger.error(traceback.format_exc(e)) return {"status": 0, "message": "添加任务失败"}
def get_all_current_monitor(self): with sessionCM() as session: robots = Monitor.get_all_using_robot(session) for robot in robots: get_usage.delay(robot.public_ip) return {"status": 1, "message": "已经加入任务队列"}