fix some error

This commit is contained in:
pengtao 2019-07-15 14:14:38 +08:00
parent b92c3f1414
commit ec42a1560d
2 changed files with 68 additions and 28 deletions

View File

@ -11,6 +11,17 @@ import tornado.web
import json
from myredis import myredis
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'
class Adlist(tornado.web.RequestHandler):
@ -23,8 +34,13 @@ def adlist_workflow(input):
gameid = input['gameid']
localid = input['localtionid']
key = f"{gameid}::{localid}"
result = myredis.hmgetall(key)
return result
ids = myredis.hmgetall(key)
info = []
for id in ids:
temp = myredis.hgetall[f"{id}::info"]
info.append(temp)
return info
def getDaySeconds(time_val, incdays):
@ -34,26 +50,56 @@ def getDaySeconds(time_val, incdays):
def send_cache_data():
now = datetime.datetime.today().strftime("%Y-%m-%d")
get_data_sql = "select "
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)
def sendNotify(conf, sendtime):
try:
print('sendNotify start')
params = {'key': 'kingsome'}
secret = 'fc38349c5d084e920925e614c420be9f'
timestamp = time.time()
md5signstr = md5Sign(params, secret, timestamp)
url = conf['notify_url'] + '&timestamp=' + str(timestamp) + '&sign=' + md5signstr
req = urllib.request.Request(url)
data = urllib.request.urlopen(req).read()
print('sendNotify end')
except Exception as e:
print('sendNotifu error: ' + str(e))
# 添加有天数限定的记录
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}<end_time ;"
# 进入下一次循环
tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + sendtime,
lambda: sendNotify(conf, sendtime))
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[
'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)
# 检查ID是否存在播放列表中以及播放次数是否完毕
pdb.set_trace()
if all:
for line in all:
key = f"{line.get('gameid', 0)}::{line.get('locationid', 0)}"
if myredis.exists(key):
# 检查num是否已达到设定数值
num = myredis.get(f"{key}:num") or 0
if line['num'] != -1 and line['num'] <= num:
# 该广告已播放达到次数,从缓存中清除
myredis.srem(key, line['id'])
# 清理广告详细记录
myredis.expire(f"{line['id']}::info", 0)
else:
myredis.sadd(key, line['id'])
if not myredis.exists(f"{key}::info"):
myredis.hmset(f"{key}::info", line)
class SelfCheckingHandler(tornado.web.RequestHandler):
@ -71,13 +117,6 @@ def make_app():
if __name__ == "__main__":
print('start!')
conf = json.loads(open('../config/kefu_robot.json', 'r').read())
app = make_app()
app.listen(conf['listen_port'])
import pdb
tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + conf['sendtime1'],
lambda: sendNotify(conf, conf['sendtime1']))
tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + conf['sendtime2'],
lambda: sendNotify(conf, conf['sendtime2']))
tornado.ioloop.IOLoop.current().start()
send_cache_data() # print('start!') # conf = json.loads(open('../config/kefu_robot.json', 'r').read()) # app = make_app() # app.listen(conf['listen_port']) # # tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + conf['sendtime1'], # lambda: sendNotify(conf, conf['sendtime1'])) # tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + conf['sendtime2'], # lambda: sendNotify(conf, conf['sendtime2'])) # tornado.ioloop.IOLoop.current().start()

View File

@ -20,6 +20,7 @@ parser.add_argument('in_used')
class Location(Resource):
# remove gameid字段
def __init__(self):
self.args = parser.parse_args()