diff --git a/handler/ad.py b/handler/ad.py index 584cf41..fae0a77 100644 --- a/handler/ad.py +++ b/handler/ad.py @@ -13,6 +13,7 @@ delete(删除定义的广告)params()output(1,0) from flask import Flask, jsonify from flask_restful import reqparse, abort, Api, Resource import logging +from ops.reflush_ad import rebuild_ad # from myredis.myredis import ad_redis, expire_time from mysql.mmysql import MysqlBase from prod_config import mysql_promotion_config @@ -206,7 +207,10 @@ class Ad(Resource): if data: condition = f"id='{self.args['id']}'" self.mydb.update("ad", ad, condition) - return jsonify({'code': 200, 'message': 'update adid={id} success!'}) + if rebuild_ad(): + return jsonify({'code': 200, 'message': 'update adid={id} success!'}) + else: + return jsonify({'code': 500, 'message': 'rebuild cache failed!'}) else: return jsonify({'code': 404, 'message': f"{self.args['id']} not found in mysql!"}) except Exception: @@ -222,7 +226,10 @@ class Ad(Resource): del_sql = f"update ad set in_used=0 where id={id};" try: self.mydb.change(del_sql) - return jsonify({'code': 200, 'message': f'remove adid={id} success!'}) + if rebuild_ad(): + return jsonify({'code': 200, 'message': f'remove adid={id} success!'}) + else: + return jsonify({'code': 500, 'message': 'rebuild cache failed!'}) except Exception: log.error("remove id from ad failed!", exc_info=True) diff --git a/handler/location.py b/handler/location.py index 4f1c092..1ff45ba 100644 --- a/handler/location.py +++ b/handler/location.py @@ -6,7 +6,7 @@ import logging from mysql.mmysql import MysqlBase from prod_config import mysql_promotion_config - +from ops.reflush_ad import rebuild_ad import pdb log = logging.getLogger(__name__) @@ -138,11 +138,10 @@ class Location(Resource): if data: condition = f"id='{self.args['id']}'" self.mydb.update("location", location, condition) - locationids = location['id'].replace('[', "").replace(']', "").replace('"', "").split(',') - for id in locationids: - log.info(f"reflush locationid={id}") - self.reflush_location(id) - return jsonify({'code': 200}) + if rebuild_ad(): + return jsonify({'code': 200}) + else: + return jsonify({'code': 500, 'message': "rebuild cache failed!"}) else: return jsonify({'code': 404, 'message': f"{self.args['id']} not found in mysql!"}) else: @@ -159,17 +158,10 @@ class Location(Resource): try: del_sql = f'update location set in_used=0 where id={id};' self.mydb.change(del_sql) + if rebuild_ad(): + return jsonify({'code': 200}) + else: + return jsonify({'code': 500}) except Exception: log.error(f"location remove {id} failed!", exc_info=True) return jsonify({'code': 500}) - return jsonify({'code': 200}) - - - def reflush_location(self, locationid): - import requests - url = f"http://127.0.0.1:6015/interface/reflush_adinfo?locationid={locationid}" - r = requests.get(url) - if r.status_code == 200: - return True - else: - return False diff --git a/ops/reflush_ad.py b/ops/reflush_ad.py index 04dad5d..aa25e30 100644 --- a/ops/reflush_ad.py +++ b/ops/reflush_ad.py @@ -1,143 +1,28 @@ # -*- coding: utf-8 -*- -from myredis.myredis import my_redis -from mysql.mmysql import MysqlBase -from prod_config import mysql_promotion_config +from prod_config import priv_i_port from log.mylog import define_logger import logging -import json - +import requests define_logger("/data/logs/reflush_ad.log") log = logging.getLogger(__name__) +def rebuild_ad(): + ra = Reflush_AdInfo() + if ra.reflush(): + return True + else: + return False + + +# http://154.8.214.202:6015/interface/reflush_adinfo class Reflush_AdInfo(): def __init__(self): - self.mysql = MysqlBase(**mysql_promotion_config) + self.url = f"http://127.0.0.1:{priv_i_port}/interface/reflush_adinfo" - def reflush_ad_via_localtionid(self, locationid): - all = my_redis.keys() - change_key = [] - if all: - for one_key in all: - if len(one_key.split("::")) > 3 and one_key.split("::")[-1] == locationid: - change_key.append(one_key) + def reflush(self): + r = requests.get(self.url) + if r.status_code == 200: + return True else: - raise Exception("Redis is null!") - ad_list = [] - for item in change_key: - temp = my_redis.smembers(item) - for one in temp: - ad_list.append(one) - self.reflush_ad(ad_list) - - - def reflush_ad(self, adids): - # expirt key via localtionid - for item in adids: - key = f"ad::{item}::info" - my_redis.expire(key, 1) - - all_data = [] - # add new key - sql = f"""select - a.id, - a.name, - a.ad_num, - a.ad_title, - a.ad_body, - a.ad_image, - a.jump_param, - a.ad_sort, - a.companyid, - a.gameid, - a.channelid, - a.jump_status, - a.ad_property, - b.area, - b.type, - b.mode, - a.locationid - from - (select - id, - name, - ad_num, - ad_title, - ad_body, - ad_image, - jump_param, - ad_sort, - companyid, - gameid, - channelid, - jump_status, - locationid , - ad_property - from - ad - where - status=1 - and in_used=1 - and id={item})a , - (select - id, - area, - type, - mode - from - location) b - where - a.locationid=b.id - """ - data = self.mysql.query(sql) - if data: - for line in data: - item = {} - try: - item['id'], item['name'], item['ad_num'], item['ad_title'], item['ad_body'], item['ad_image'], \ - item['jump_param'], item['ad_sort'], item['companyid'], item['gameid'], item['channelid'], \ - item['jump_status'], item['ad_property'], item['area'], item['type'], item['mode'], item[ - 'locationid'] = line - try: - item['x_offset'] = item['area'].split(',')[-2].strip() - except: - item['x_offset'] = 0 - try: - item['y_offset'] = item['area'].split(',')[-1].strip().strip(')') - except: - item['y_offset'] = 0 - - if item.get('jump_param', "") and (item['ad_property'].find("jump_param") == -1): - try: - temp = {} - temp['jump_param'] = item.get('jump_param', "") - if not item['ad_property']: - item['ad_property'] = {} - if not isinstance(item.get('ad_property', {}), dict): - if not item.get('ad_property', {}): - item['ad_property'] = {}.update(temp) - else: - item['ad_property'] = json.loads( - item.get('ad_property', {}).replace("‘", '"').replace("’", '"')) - item['ad_property'].update(temp) - item['ad_property'] = json.dumps(item['ad_property']) - except Exception: - log.error(f"write {item}", exc_info=True) - - all_data.append(item) - except Exception: - log.error("split data failed", exc_info=True) - - for item in all_data: - my_redis.hmset(f"ad::{item['id']}::info", item) - my_redis.expire(f"ad::{item['id']}::info", 3600 * 24) - - - -def main(): - cc = Reflush_AdInfo() - cc.reflush_ad_via_localtionid('1016') - - -if __name__ == "__main__": - main() + return False