def run_job(dbname, job): print("run_job dbname=%s pid=%s job='%s'" % (dbname, os.getpid(), job["name"])) database.connections.clear() set_active_user(1) database.set_active_db(dbname) db = database.get_connection() db.begin() clear_cache() m = get_model(job["model"]) f = getattr(m, job["method"]) if job["args"]: args = json.loads(job["args"]) else: args = [] db.execute("UPDATE cron_job SET state='running' WHERE id=%s", job["id"]) db.commit() print("starting job '%s'" % job["name"]) try: with utils.timeout(seconds=job["timeout"]): f(*args) db.commit() print("job '%s' success" % job["name"]) except Exception as e: print("WARNING: job '%s' failed: %s" % (job["name"], e)) db.rollback() t = time.strftime("%Y-%m-%d %H:%M:%S") msg = traceback.format_exc() db.execute( "UPDATE cron_job SET last_error_time=%s,error_message=%s WHERE id=%s", t, msg, job["id"]) db.commit() t1 = time.strftime("%Y-%m-%s %H:%M:%S") if job["interval_num"]: if job["interval_type"] == "second": dt = timedelta(seconds=job["interval_num"]) elif job["interval_type"] == "minute": dt = timedelta(minutes=job["interval_num"]) elif job["interval_type"] == "hour": dt = timedelta(hours=job["interval_num"]) elif job["interval_type"] == "day": dt = timedelta(days=job["interval_num"]) else: raise Exception("Missing interval unit") next_date = datetime.strptime(job["date"], "%Y-%m-%d %H:%M:%S") now = datetime.now() while next_date <= now: # TODO: make this faster next_date += dt if next_date < _check_times[dbname]: _check_times[dbname] = next_date db.execute("UPDATE cron_job SET date=%s,state='waiting' WHERE id=%s", next_date.strftime("%Y-%m-%d %H:%M:%S"), job["id"]) else: db.execute("UPDATE cron_job SET state='done' WHERE id=%s", job["id"]) db.commit()
def run_job(dbname, job): print("run_job dbname=%s pid=%s job='%s'"%(dbname, os.getpid(), job["name"])) database.connections.clear() set_active_user(1) database.set_active_db(dbname) db = database.get_connection() db.begin() clear_cache() m = get_model(job["model"]) f = getattr(m, job["method"]) if job["args"]: args = json.loads(job["args"]) else: args = [] db.execute("UPDATE cron_job SET state='running' WHERE id=%s", job["id"]) db.commit() print("starting job '%s'"%job["name"]) try: with utils.timeout(seconds=job["timeout"]): f(*args) db.commit() print("job '%s' success" % job["name"]) except Exception as e: print("WARNING: job '%s' failed: %s"%(job["name"],e)) db.rollback() t=time.strftime("%Y-%m-%d %H:%M:%S") msg=traceback.format_exc() db.execute("UPDATE cron_job SET last_error_time=%s,error_message=%s WHERE id=%s", t, msg, job["id"]) db.commit() t1 = time.strftime("%Y-%m-%s %H:%M:%S") if job["interval_num"]: if job["interval_type"] == "second": dt = timedelta(seconds=job["interval_num"]) elif job["interval_type"] == "minute": dt = timedelta(minutes=job["interval_num"]) elif job["interval_type"] == "hour": dt = timedelta(hours=job["interval_num"]) elif job["interval_type"] == "day": dt = timedelta(days=job["interval_num"]) else: raise Exception("Missing interval unit") next_date = datetime.strptime(job["date"], "%Y-%m-%d %H:%M:%S") now = datetime.now() while next_date <= now: # TODO: make this faster next_date += dt if next_date < _check_times[dbname]: _check_times[dbname] = next_date db.execute("UPDATE cron_job SET date=%s,state='waiting' WHERE id=%s", next_date.strftime("%Y-%m-%d %H:%M:%S"), job["id"]) else: db.execute("UPDATE cron_job SET state='done' WHERE id=%s", job["id"]) db.commit()
def post(self): req = json.loads(self.request.body.decode()) # open("/tmp/json_rpc.log","a").write(self.request.body.decode()+"\n###############################################################\n") db = database.get_connection() if db: db.begin() try: clear_cache() method = req["method"] params = req["params"] if method == "execute": model = params[0] method = params[1] if method.startswith("_"): raise Exception("Invalid method") args = params[2] if len(params) >= 4: opts = params[3] or {} else: opts = {} user_id = access.get_active_user() rpc_log.info( "EXECUTE db=%s model=%s method=%s user=%s" % (database.get_active_db(), model, method, user_id) ) m = get_model(model) f = getattr(m, method) ctx = {"request_handler": self, "request": self.request} ctx.update(self.get_cookies()) opts.setdefault("context", {}).update(ctx) with timeout(seconds=300): # XXX: can make this faster? (less signal sys handler overhead) t0 = time.time() res = f(*args, **opts) t1 = time.time() dt = (t1 - t0) * 1000 rpc_log.info("<<< %d ms" % dt) resp = {"result": res, "error": None, "id": req["id"]} else: raise Exception("Invalid method: %s" % method) if db: db.commit() except Exception as e: try: msg = translate(str(e)) except: print("WARNING: Failed to translate error message") msg = str(e) rpc_log.error(msg) if db: db.rollback() rpc_log.error(traceback.format_exc()) err = {"message": msg} error_fields = getattr(e, "error_fields", None) if error_fields: err["error_fields"] = error_fields resp = {"result": None, "error": err, "id": req["id"]} access.clear_active_user() try: data = json_dumps(resp) self.add_header("Access-Control-Allow-Origin", "*") self.write(data) except: print("JSONRPC ERROR: invalid response") from pprint import pprint pprint(resp) traceback.print_exc()
def post(self): req = json.loads(urllib.parse.unquote(self.get_argument("request"))) db = database.get_connection() if db: db.begin() try: clear_cache() method = req["method"] params = req["params"] if method == "execute": model = params[0] method = params[1] if method.startswith("_"): raise Exception("Invalid method") args = params[2] if len(params) >= 4: opts = params[3] or {} else: opts = {} user_id = access.get_active_user() rpc_log.info("EXECUTE db=%s model=%s method=%s user=%s" % (database.get_active_db(), model, method, user_id)) m = get_model(model) f = getattr(m, method) ctx = { "request_handler": self, "request": self.request, } ctx.update(self.get_cookies()) opts.setdefault("context", {}).update(ctx) with timeout(seconds=300): # XXX: can make this faster? (less signal sys handler overhead) t0 = time.time() res = f(*args, **opts) t1 = time.time() dt = (t1 - t0) * 1000 rpc_log.info("<<< %d ms" % dt) resp = { "result": res, "error": None, "id": req["id"], } else: raise Exception("Invalid method: %s" % method) db = database.get_connection() if db: db.commit() except Exception as e: try: msg = translate(str(e)) except: print("WARNING: Failed to translate error message") msg = str(e) rpc_log.error(msg) db = database.get_connection() if db: db.rollback() rpc_log.error(traceback.format_exc()) err = { "message": msg, } error_fields = getattr(e, "error_fields", None) if error_fields: err["error_fields"] = error_fields resp = { "result": None, "error": err, "id": req["id"], } access.clear_active_user() try: data = urllib.parse.quote(json_dumps(resp)) except: print("JSONRPC ERROR: invalid response") from pprint import pprint pprint(resp) print("json_rpc_iframe result", data) self.write(data)
def get(self): db = database.get_connection() if db: db.begin() try: clear_cache() print(self) method = self.get_argument("method") model = self.get_argument("model") if method.startswith("_"): raise Exception("Invalid method") args = self.get_argument("args",None) if args: args=json.loads(args) else: args=[] opts = self.get_argument("opts",None) if opts: opts=json.loads(opts) else: opts={} cookies = self.get_argument("cookies",None) if cookies: cookies = json.loads(cookies) else: cookies = {} if "locale" in cookies: set_active_locale(cookies["locale"]) user_id = access.get_active_user() rpc_log.info("EXECUTE db=%s model=%s method=%s user=%s" % (database.get_active_db(), model, method, user_id)) m = get_model(model) f = getattr(m, method) ctx = { "request_handler": self, "request": self.request, } ctx.update(self.get_cookies()) opts.setdefault("context", {}).update(ctx) with timeout(seconds=900): # XXX: can make this faster? (less signal sys handler overhead) t0 = time.time() res = f(*args, **opts) t1 = time.time() dt = (t1 - t0) * 1000 rpc_log.info("<<< %d ms" % dt) resp = { "result": res, "error": None, "id": self.get_argument("id"), } if db: db.commit() except Exception as e: try: msg = translate(str(e)) except: print("WARNING: Failed to translate error message") msg = str(e) rpc_log.error(msg) if db: db.rollback() rpc_log.error(traceback.format_exc()) err = { "message": msg, } error_fields = getattr(e, "error_fields", None) if error_fields: err["error_fields"] = error_fields resp = { "result": None, "error": err, "id": self.get_argument("id",None), } access.clear_active_user() try: data = json_dumps(resp) self.add_header("Access-Control-Allow-Origin","*") self.add_header("Last-Modified",datetime.utcnow()) self.write(data) except: print("JSONRPC ERROR: invalid response") from pprint import pprint pprint(resp) traceback.print_exc()
def post(self): req = json.loads(self.request.body.decode()) # open("/tmp/json_rpc.log","a").write(self.request.body.decode()+"\n###############################################################\n") db = database.get_connection() if db: db.begin() try: clear_cache() method = req["method"] params = req["params"] if method == "execute": model = params[0] method = params[1] if method.startswith("_"): raise Exception("Invalid method") args = params[2] if len(params) >= 4: opts = params[3] or {} else: opts = {} if len(params) >= 5: cookies = params[4] or {} else: cookies = {} if "locale" in cookies: set_active_locale(cookies["locale"]) if "user_id" in cookies: user_id = int(cookies["user_id"]) token = cookies.get("token") dbname = database.get_active_db() schema = database.get_active_schema() if check_token(dbname, user_id, token, schema=schema): access.set_active_user(user_id) user_id = access.get_active_user() rpc_log.info( "EXECUTE db=%s model=%s method=%s user=%s" % (database.get_active_db(), model, method, user_id)) m = get_model(model) f = getattr(m, method) ctx = { "request_handler": self, "request": self.request, } ctx.update(self.get_cookies()) ctx.update(cookies) ctx.update(opts.get("context", {})) opts["context"] = ctx with timeout( seconds=900 ): # XXX: can make this faster? (less signal sys handler overhead) t0 = time.time() res = f(*args, **opts) t1 = time.time() dt = (t1 - t0) * 1000 rpc_log.info("<<< %d ms" % dt) resp = { "result": res, "error": None, "id": req["id"], } else: raise Exception("Invalid method: %s" % method) if db: db.commit() except Exception as e: try: msg = translate(str(e)) except: print("WARNING: Failed to translate error message") msg = str(e) rpc_log.error(msg) if db: db.rollback() rpc_log.error(traceback.format_exc()) err = { "message": msg, } error_fields = getattr(e, "error_fields", None) if error_fields: err["error_fields"] = error_fields resp = { "result": None, "error": err, "id": req["id"], } access.clear_active_user() try: data = json_dumps(resp) self.add_header("Access-Control-Allow-Origin", "*") self.write(data) except: print("JSONRPC ERROR: invalid response") from pprint import pprint pprint(resp) traceback.print_exc()
def get(self): db = database.get_connection() if db: db.begin() try: clear_cache() print(self) method = self.get_argument("method") model = self.get_argument("model") if method.startswith("_"): raise Exception("Invalid method") args = self.get_argument("args", None) if args: args = json.loads(args) else: args = [] opts = self.get_argument("opts", None) if opts: opts = json.loads(opts) else: opts = {} cookies = self.get_argument("cookies", None) if cookies: cookies = json.loads(cookies) else: cookies = {} if "locale" in cookies: set_active_locale(cookies["locale"]) user_id = access.get_active_user() rpc_log.info("EXECUTE db=%s model=%s method=%s user=%s" % (database.get_active_db(), model, method, user_id)) m = get_model(model) f = getattr(m, method) ctx = { "request_handler": self, "request": self.request, } ctx.update(self.get_cookies()) opts.setdefault("context", {}).update(ctx) with timeout( seconds=900 ): # XXX: can make this faster? (less signal sys handler overhead) t0 = time.time() res = f(*args, **opts) t1 = time.time() dt = (t1 - t0) * 1000 rpc_log.info("<<< %d ms" % dt) resp = { "result": res, "error": None, "id": self.get_argument("id"), } if db: db.commit() except Exception as e: try: msg = translate(str(e)) except: print("WARNING: Failed to translate error message") msg = str(e) rpc_log.error(msg) if db: db.rollback() rpc_log.error(traceback.format_exc()) err = { "message": msg, } error_fields = getattr(e, "error_fields", None) if error_fields: err["error_fields"] = error_fields resp = { "result": None, "error": err, "id": self.get_argument("id", None), } access.clear_active_user() try: data = json_dumps(resp) self.add_header("Access-Control-Allow-Origin", "*") self.add_header("Last-Modified", datetime.utcnow()) self.write(data) except: print("JSONRPC ERROR: invalid response") from pprint import pprint pprint(resp) traceback.print_exc()