完成
This commit is contained in:
parent
645165bb32
commit
22c65d605f
6
.gitmodules
vendored
6
.gitmodules
vendored
@ -1,3 +1,9 @@
|
||||
[submodule "third_party/phpcommon"]
|
||||
path = third_party/phpcommon
|
||||
url = git@git.kingsome.cn:server_common/phpcommon.git
|
||||
[submodule "third_party/q7"]
|
||||
path = third_party/q7
|
||||
url = git@git.kingsome.cn:server_common/q7.git
|
||||
[submodule "third_party/f7"]
|
||||
path = third_party/f7
|
||||
url = git@git.kingsome.cn:server_common/f7.git
|
||||
|
1
third_party/f7
vendored
Submodule
1
third_party/f7
vendored
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 8b29369ca65d5d474a78085c3e5f93be6be9376a
|
1
third_party/q7
vendored
Submodule
1
third_party/q7
vendored
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit b4ea03b447e075ecfeefa1b22d0a0497ad80c0eb
|
@ -1,31 +1,23 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
sys.path.append('../local_packages')
|
||||
import q7
|
||||
import f7
|
||||
|
||||
import pymysql
|
||||
import hashlib
|
||||
import json
|
||||
import urllib.request
|
||||
import base64
|
||||
import tornado.ioloop
|
||||
import tornado.web
|
||||
import time
|
||||
import datetime
|
||||
import redis
|
||||
import os
|
||||
import functools
|
||||
|
||||
CONFIG_DIR = ''
|
||||
|
||||
def IsOnlineEnv():
|
||||
return os.getenv("SERVER_ENV");
|
||||
|
||||
if (IsOnlineEnv()):
|
||||
CONFIG_DIR = '/var/data/conf_test/game2003api_rankserver/config'
|
||||
else:
|
||||
CONFIG_DIR = '../config'
|
||||
|
||||
def info(msg):
|
||||
print(str(datetime.datetime.now()) + '[INFO] ' + msg)
|
||||
CONFIG_DIR = '../config' if f7.isOnlineEnv() else '/var/data/conf_test/game2003api_rankserver/config'
|
||||
|
||||
def take_pass(elem):
|
||||
return elem[3]
|
||||
@ -39,15 +31,8 @@ def safeDiv(a, b):
|
||||
else:
|
||||
return a / b
|
||||
|
||||
#获取channel
|
||||
def getChannel(a):
|
||||
str_list = a.split('_')
|
||||
if len(str_list) < 3:
|
||||
return 0
|
||||
return str_list[0]
|
||||
|
||||
def getRedis():
|
||||
redis_conf = json.loadsmysql_conf = json.loads(open(CONFIG_DIR + '/rankserver.redis.cluster.json', 'r').read())
|
||||
redis_conf = json.loads(open(CONFIG_DIR + '/rankserver.redis.cluster.json', 'r').read())
|
||||
for conf in redis_conf:
|
||||
r = redis.Redis(host = conf['host'],
|
||||
port = conf['port'],
|
||||
@ -60,11 +45,6 @@ def getRedisConf():
|
||||
redis_conf = json.loads(open(CONFIG_DIR + '/rankserver.redis.cluster.json', 'r').read())
|
||||
return redis_conf;
|
||||
|
||||
def getDaySeconds(time_val, incdays):
|
||||
time_zone = 8
|
||||
dayseconds = int((time_val + time_zone * 3600)/3600/24 + incdays) * 3600 * 24 - 3600 * time_zone;
|
||||
return dayseconds
|
||||
|
||||
#数据去重
|
||||
def delRepeatData(row, data_list):
|
||||
temp_list = []
|
||||
@ -110,7 +90,7 @@ def internalDayReadMysqlData():
|
||||
for row in cursor:
|
||||
has_data = True
|
||||
#更新通关榜
|
||||
channel = getChannel(row[0])
|
||||
channel = f7.getChannelByAccountId(row[0])
|
||||
if channel not in rank_hash:
|
||||
rank_hash[channel] = []
|
||||
refreshData(row, rank_hash[channel])
|
||||
@ -125,9 +105,8 @@ def internalDayReadMysqlData():
|
||||
#每日定时读取mysql里的数据生成排行榜写入redis后php读取redis返回客户端显示
|
||||
def dayReadMysqlData(rushtime):
|
||||
internalDayReadMysqlData()
|
||||
tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + rushtime,
|
||||
lambda : dayReadMysqlData(rushtime)
|
||||
)
|
||||
f7.timer.callAt(q7.getDaySeconds(time.time(), 1) + rushtime,
|
||||
lambda : dayReadMysqlData(rushtime))
|
||||
|
||||
#每5分钟读取mysql里发生改变过的数据更新排行榜
|
||||
def readMysqlData(rushtime):
|
||||
@ -151,7 +130,7 @@ def readMysqlData(rushtime):
|
||||
has_data = False
|
||||
for row in cursor:
|
||||
has_data = True
|
||||
channel = getChannel(row[0])
|
||||
channel = f7.getChannelByAccountId(row[0])
|
||||
if channel not in rank_hash:
|
||||
rank_list = r.get('game2003api:pass_rank_' + channel)
|
||||
rank_hash[channel] = [] if not rank_list else json.loads(rank_list)
|
||||
@ -168,37 +147,25 @@ def readMysqlData(rushtime):
|
||||
for channel in rank_hash:
|
||||
updateRank(r, channel, rank_hash[channel])
|
||||
|
||||
tornado.ioloop.IOLoop.current().call_later(rushtime,
|
||||
lambda : readMysqlData(rushtime)
|
||||
)
|
||||
|
||||
class SelfCheckingHandler(tornado.web.RequestHandler):
|
||||
|
||||
def get(self):
|
||||
self.write(json.dumps({
|
||||
'errcode': 0,
|
||||
'errmsg': '',
|
||||
'healthy': 1,
|
||||
'max_rundelay': 10
|
||||
}))
|
||||
|
||||
def make_app():
|
||||
return tornado.web.Application([
|
||||
(r"/webapp/index[\.]php", SelfCheckingHandler),
|
||||
])
|
||||
f7.timer.callLater(rushtime,
|
||||
lambda : readMysqlData(rushtime)
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
f7.app.init('/data/logs/game2003_rankserver/logs')
|
||||
f7.udplog.info('rankserver start pid:' + str(os.getpid()))
|
||||
|
||||
conf = json.loads(open(CONFIG_DIR + '/rankserver.json', 'r').read())
|
||||
|
||||
app = make_app()
|
||||
app.listen(conf['listen_port'])
|
||||
conf['rushtime'] = 5
|
||||
tornado.ioloop.IOLoop.current().call_later(conf['rushtime'],
|
||||
lambda : readMysqlData(conf['rushtime'])
|
||||
)
|
||||
conf['rushtime'] = 300
|
||||
f7.timer.callLater(conf['rushtime'],
|
||||
lambda : readMysqlData(conf['rushtime']))
|
||||
|
||||
conf['day_rushtime'] = 5 * 3600
|
||||
tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + conf['day_rushtime'],
|
||||
lambda : dayReadMysqlData(conf['day_rushtime'])
|
||||
)
|
||||
tornado.ioloop.IOLoop.current().start()
|
||||
f7.timer.callAt(q7.getDaySeconds(time.time(), 1) + conf['day_rushtime'],
|
||||
lambda : dayReadMysqlData(conf['day_rushtime']))
|
||||
|
||||
f7.app.listen(conf['listen_port'])
|
||||
f7.app.start()
|
||||
|
@ -2,6 +2,10 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
sys.path.append('../local_packages')
|
||||
import q7
|
||||
import f7
|
||||
|
||||
import pymysql
|
||||
import hashlib
|
||||
import json
|
||||
@ -17,20 +21,6 @@ import functools
|
||||
|
||||
import game2003rank
|
||||
|
||||
def scanRedisKey(conn, pattern):
|
||||
result = {}
|
||||
cursor, keys = conn.scan(0, pattern, 1000)
|
||||
while cursor != 0 or len(keys) > 0:
|
||||
for key in keys:
|
||||
if key in result:
|
||||
result[key] += result[key]
|
||||
else:
|
||||
result[key] = 0
|
||||
keys = []
|
||||
if cursor != 0:
|
||||
cursor, keys = conn.scan(cursor, pattern, 1000)
|
||||
return result
|
||||
|
||||
def _updateRank_cmd(debug_info):
|
||||
game2003rank.internalDayReadMysqlData()
|
||||
|
||||
@ -42,7 +32,7 @@ def _clearRank_cmd(debug_info):
|
||||
charset = 'utf8'
|
||||
)
|
||||
pass_list = []
|
||||
scan_keys = scanRedisKey(r, "game2003api:pass_rank_*")
|
||||
scan_keys = f7.scanRedisKey(r, "game2003api:pass_rank_*")
|
||||
for key in scan_keys :
|
||||
r.set(key, json.dumps(pass_list))
|
||||
|
||||
@ -73,4 +63,6 @@ if __name__ == "__main__":
|
||||
if len(sys.argv) <= 1:
|
||||
pass
|
||||
else:
|
||||
f7.app.init('/data/logs/game2003_rankserver_cmd/logs')
|
||||
f7.udplog.info('game2003_rankserver_cmd start pid:' + str(os.getpid()))
|
||||
processCmdLine(sys.argv[1])
|
||||
|
Loading…
x
Reference in New Issue
Block a user