# -*- coding: utf-8 -*- # 推广系统对外接口,提供与客户端之间的广告信息接口及每分钟执行一次的缓存变更操作 import tornado.ioloop import tornado.web import json from myredis.myredis import my_redis import datetime from mysql.mmysql import MysqlBase from config import mysql_promotion_config from log.mylog import define_logger import logging mydb = MysqlBase(**mysql_promotion_config) define_logger("/data/logs/make_ad_cache.log") log = logging.getLogger(__name__) BEGIN = '1999-01-01' END = '3000-01-01' def send_cache_data(): now = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S") all = [] # 添加无天数限定的记录 get_full_data = f"select id,name,ad_num,ad_title,ad_body,ad_image,ad_url,ad_sort,companyid,locationid,gameid from ad where begin_time='{BEGIN}' or end_time='{END}'" full_data = mydb.query(get_full_data) if full_data: for line in full_data: if line: item = {} try: item['id'], item['name'], item['ad_num'], item['ad_title'], item['ad_body'], item['ad_image'], item['ad_url'], \ item['ad_sort'], item['companyid'], item['locationid'], item['gameid'] = line all.append(item) except Exception: log.error("split data failed", exc_info=True) # 添加有天数限定的记录 get_data_sql = f"select id,name,ad_num,ad_title,ad_body,ad_image,ad_url,ad_sort,companyid,locationid,gameid from ad where '{now}'>begin_time and '{now}' 0 and line['ad_num'] <= num: # 该广告已播放达到次数,从缓存中清除 my_redis.srem(key, line['id']) # 清理广告详细记录 my_redis.expire(f"{line['id']}::info", 0) else: my_redis.sadd(key, line['id']) log.info(f"check {line['id']}::info") if not my_redis.exists(f"{line['id']}::info"): my_redis.hmset(f"{line['id']}::info", line) len_all = len(all) log.info(f"write {len_all} to cache!") remove_expire_data() def remove_expire_data(): now = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S") sql = f"select id,gameid,locationid from ad where '{now}'>=end_time" data = mydb.query(sql) if data: for line in data: try: id, gameid, locationid = line my_redis.srem(f"{gameid}::{locationid}", id) log.info(f"remove expire ad {id}!") except Exception: log.error("split data from db failed!", exc_info=True) class DispatchHandler(tornado.web.RequestHandler): def get(self): if self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'selfChecking': self._selfCheckingHandler() elif self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'getAdList': self._selfGetAdList() elif self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'upAdRecording': self._upAdRecording() else: self.write("pls check args!") def _upAdRecording(self): try: adid = self.get_query_argument('adid') except Exception: result = {'errcode': 2, "errmsg": f"get args failed`"} log.error(result, exc_info=True) self.write({'errcode': 1, "errmsg": 'get adid failed!'}) if adid: key = f"{adid}::num" my_redis.incr(key) self.write({'errcode': 0, "errmsg": '', "message": f"{adid} incr success!"}) def _selfCheckingHandler(self): self.write(json.dumps({ 'errcode': 0, 'errmsg': '', 'healthy': 1, 'max_rundelay': 10 }, separators=(',', ':'))) def _selfGetAdList(self): try: input = json.loads(self.get_query_argument('body')) gameid = input['gameid'] locationid = input['locationid'] except Exception as e: result = {'errcode': 2, "errmsg": f"get args failed,{str(e)}"} log.error(result) self.write_error(2) if gameid and locationid: key = f"{gameid}::{locationid}" ids = my_redis.smembers(key) info = [] try: for id in ids: temp = my_redis.hgetall(f"{id}::info") info.append(temp) result = {'errcode': 0, "errmsg": '', "message": {"totoal": len(info), "result": info}} except Exception as e: result = {'errcode': 1, "errmsg": e} finally: self.write(result) else: result = {'errcode': 2, "errmsg": f"get args failed!"} self.write(result) def make_app(): return tornado.web.Application([(r"/webapp/index[\.]php", DispatchHandler)]) if __name__ == "__main__": import pdb send_cache_data() print('start!') app = make_app() app.listen('5555') tornado.ioloop.PeriodicCallback(send_cache_data, 60000).start() # tornado.ioloop.IOLoop.current().call_at(time.time() + 60, lambda: sendNotify(conf, conf['sendtime1'])) tornado.ioloop.IOLoop.current().start()