为广告添加字段 ad_property 作为json存储广告的相关信息
This commit is contained in:
parent
5d5a47e240
commit
db4d37f12b
@ -330,7 +330,7 @@ http://154.8.214.202:5015/interface/prometion
|
||||
|
||||
##### 请求示范
|
||||
|
||||
http://154.8.214.202:5015/interface/promotion?name=abc&gameid=1003&locationid=1&ad_title=hello&ad_body=hi world&ad_image=http://1&ad_url=http://2&companyid=2
|
||||
http://154.8.214.202:5015/interface/promotion?name=abc&gameid=1003&locationid=1007&ad_title=hello&ad_body=hi world&ad_image=http://1&ad_url=http://2&companyid=2&ad_property={‘is_shake’:1}&jump_status=1
|
||||
|
||||
##### 请求参数说明
|
||||
|
||||
|
160
ad_tasks.py
Normal file
160
ad_tasks.py
Normal file
@ -0,0 +1,160 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
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
|
||||
from config import BEGIN, END, ad_list_interface_port
|
||||
import pdb
|
||||
|
||||
define_logger("/data/logs/ad_tasks.log")
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
limit = 10
|
||||
|
||||
|
||||
def send_cache_data():
|
||||
mydb = MysqlBase(**mysql_promotion_config)
|
||||
log.info("start update cache !")
|
||||
now = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
|
||||
all = []
|
||||
|
||||
get_data_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
|
||||
from
|
||||
ad
|
||||
where
|
||||
status=1
|
||||
and in_used=1
|
||||
and '{now}'> begin_time
|
||||
and '{now}'<end_time ) a ,
|
||||
(select
|
||||
id,
|
||||
area,
|
||||
type,
|
||||
mode
|
||||
from
|
||||
location) b
|
||||
where
|
||||
a.locationid=b.id"""
|
||||
pdb.set_trace()
|
||||
data = mydb.query(get_data_sql)
|
||||
if data:
|
||||
for line in data:
|
||||
|
||||
if line:
|
||||
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
|
||||
all.append(item)
|
||||
except Exception:
|
||||
log.error("split data failed", exc_info=True)
|
||||
# 检查ID是否存在播放列表中,以及播放次数是否完毕
|
||||
if all:
|
||||
# log.info(f"get data was {all}!\n")
|
||||
for line in all:
|
||||
key = f"ad::{line.get('gameid', 0)}::{line.get('channelid', 0)}::{line.get('area').split(',')[
|
||||
0].strip()}::{line.get('locationid', 0)}"
|
||||
if int(line['ad_num']) > 0:
|
||||
num = my_redis.get(f"ad::{line['id']}::num")
|
||||
if not num:
|
||||
num = 0
|
||||
if int(line['ad_num']) > int(num):
|
||||
my_redis.sadd(key, line['id'])
|
||||
my_redis.expire(key, 120)
|
||||
n = int(line['ad_num']) - int(num)
|
||||
log.info(f"add {line['id']} to {key} ,num was {line['ad_num']},limit was {n}!")
|
||||
else:
|
||||
if my_redis.sismember(key, line['id']):
|
||||
my_redis.srem(key, line['id'])
|
||||
log.info(f"remove {line['id']} from {key}!")
|
||||
elif int(line['ad_num']) == 0:
|
||||
my_redis.sadd(key, line['id'])
|
||||
my_redis.expire(key, 120)
|
||||
log.info(f"add {line['id']} to {key} ,num was unlimit !")
|
||||
else:
|
||||
log.error(f"get ad_num from mysql failed! ad_num={line['ad_num']}")
|
||||
|
||||
if not my_redis.exists(f"ad::{line['id']}::info"):
|
||||
# remove some filed from adinfo
|
||||
remove_list = ('ad_num', 'gameid')
|
||||
for item in remove_list:
|
||||
line.pop(item)
|
||||
my_redis.hmset(f"ad::{line['id']}::info", line)
|
||||
my_redis.expire(f"ad::{line['id']}::info", 120)
|
||||
# my_redis.expire(f"ad::{line['id']}::info", 3600 * 24 * 7)
|
||||
log.info(f"add ad::{line['id']}::info to redis!")
|
||||
|
||||
# 删除过期的数据
|
||||
log.info("remove expire data from cache!")
|
||||
|
||||
expire_sql = f"""select
|
||||
a.id,a.gameid,a.channelid,a.locationid,a.status,b.area
|
||||
from
|
||||
(select
|
||||
id,gameid,channelid,locationid,status
|
||||
from
|
||||
ad
|
||||
where
|
||||
'{now}'> end_time
|
||||
or status in (3,4) ) a ,
|
||||
(select
|
||||
id,
|
||||
area
|
||||
from
|
||||
location) b
|
||||
where
|
||||
a.locationid=b.id"""
|
||||
remove_data = mydb.query(expire_sql)
|
||||
if remove_data:
|
||||
for line in remove_data:
|
||||
try:
|
||||
id, gameid, channelid, locationid, _, area = line
|
||||
key = f"ad::{gameid}::{channelid}::{area.split(',')[0].strip()}::{locationid}"
|
||||
# key = f"{gameid}:{locationid}"
|
||||
if my_redis.sismember(key, id):
|
||||
my_redis.srem(key, id)
|
||||
my_redis.expire(f"ad::{id}::info", 1)
|
||||
log.info(f"remove {id} from {key} success!")
|
||||
except Exception:
|
||||
log.error("拆解过期数据出错!", exc_info=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
log.info('start!')
|
||||
send_cache_data()
|
Loading…
x
Reference in New Issue
Block a user