async def notify_start_handle(): from utils.app import Application app = Application.current() async def _f(): f = await _notify_start_handle() print(f) app.timers["notice"] = run_async(_f())
def notify_stop_handle(): from utils.app import Application app = Application.current() try: notice = app.timers["notice"] del app.timers["notice"] notice.cancel() except Exception as e: print(e)
def init_url(): from utils.app import Application app = Application.current() from kservices.handlers import notice_add, notify_start, notify_stop, ok app.route("/api/notice", methods=["POST"])(notice_add) app.route("/api/notify", methods=["POST"])(notify_start) app.route("/api/notify", methods=["DELETE"])(notify_stop) app.route("/service/status", methods=["GET"])(ok) app.route("/", methods=["GET"])(ok)
async def __log_post(data): """ data:{ "cnt": "内容: text", "name": "服务名字: text" } :param data: :return: """ name = data.get("name", "test") cnt = data.get("cnt", {}) from utils.app import Application app = Application.current() st, col = app.mongo.col(name) result = await col.insert_one(cnt) print('result %s' % repr(result.inserted_id))
async def _notify_start_handle(): from utils.app import Application app = Application.current() r = app.redis st, data = await r.execute("rpop", "notice") if not st: print(data) return if not data: return data = json.loads(data) msgtype = data.get("msgtype") if msgtype == 'notice': dingding_post(dingding_msg(data.get("notice"))) elif msgtype == 'log': from bussiness.log import log_post log_post(data.get("log")) else: print(data)
async def ok(req): """ 检测服务以及外部接口 :param req: :return: """ app = Application.current() r = app.redis try: st, res = await r.execute('config_get') if st: redis_status = "ok" print("redis config:\n", res) else: print("redis exception\n", res) redis_status = res except Exception as e: redis_status = str(e) print(e) return rep({"redis_status": redis_status, "timers": app.timers.keys()})
def init_config(): from utils.app import Application app = Application.current() if not hasattr(app, "timers"): app.timers = {} import os from kservices.const import Env _e = os.environ.get("app_env", Env.local) if _e == Env.local: _f = LocalConfig() elif _e == Env.dev: _f = DevConfig() elif _e == Env.uat: _f = UatConfig() elif _e == Env.production: _f = ProConfig() else: _f = LocalConfig() app.config.from_object(_f) app.debug = _f.DEBUG
def init_rabbitmq(): from utils.app import Application app = Application.current() app.redis = RabbitMQManager(config=app.config.get("REDIS"))
from utils.app import Application from tkinter import Tk if __name__ == '__main__': root = Tk() root.geometry("600x500+600+600") app = Application(root) root.mainloop()
from kservices.config import init_config from kservices.urls import init_url from utils.app import Application from utils.mongo_util import init_mongodb from utils.redis_util import init_redis app = Application.instance() init_config() init_url() init_redis() init_mongodb() if __name__ == "__main__": import sys from os.path import abspath as ap, dirname as dn sys.path.append(dn(dn(ap(__file__)))) app.run(host="0.0.0.0", port=8001, workers=1, debug=True)
def init_mongodb(): from utils.app import Application app = Application.current() app.mongo = MongoManager(app.config.get("MONGODB_DB"), uri=app.config.get("MONGODB_URI"))