150 lines
5.2 KiB
Python
150 lines
5.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
#!/usr/bin/python
|
|
|
|
import sys
|
|
sys.path.append('../local_packages')
|
|
import q7
|
|
import f7
|
|
|
|
import os
|
|
import json
|
|
import time
|
|
import urllib.parse
|
|
import urllib.request
|
|
|
|
CONFIG_DIR = '../config' if f7.isOnlineEnv() else '/var/data/conf_test/mailsender/config'
|
|
|
|
db_idx_hash = {}
|
|
|
|
def httpGet(url, params):
|
|
param = urllib.parse.urlencode(params)
|
|
url = url + param
|
|
response = urllib.request.urlopen(url=url)
|
|
|
|
def getMailConf(mailid):
|
|
mail_conf = json.loads(open(CONFIG_DIR + '/6000@game2005.mail.json', 'r').read())
|
|
for conf in mail_conf:
|
|
if conf['mailid'] == mailid:
|
|
return conf
|
|
return None
|
|
|
|
def getMailAttachmentsByVip(mail_conf, vip_lv):
|
|
items = mail_conf['attachments'].split('|')[vip_lv - 1]
|
|
return items.replace('$', '|')
|
|
|
|
def getMailAttachmentsByPriv(mail_conf, itemid):
|
|
items = mail_conf['attachments'].split('|')[vip_lv - 1]
|
|
return items.replace('$', '|')
|
|
|
|
def sendVipLvUpMail(account_id, old_vip_lv, new_vip_lv):
|
|
url = 'https://gamemail.kingsome.cn/webapp/index.php?' if f7.isOnlineEnv() else 'https://gamemail-test.kingsome.cn/webapp/index.php?'
|
|
for vip_lv in range(old_vip_lv + 1, new_vip_lv):
|
|
mail_conf = getMailConf(1)
|
|
params = {
|
|
'c': 'MailMgr',
|
|
'a': 'sendMail',
|
|
'gameid': 2005,
|
|
'to': account_id,
|
|
'mailtype': 1,
|
|
'mailsubtype': -2,
|
|
'usertype': 2,
|
|
'content': mail_conf['content'],
|
|
'subject': mail_conf['subject'],
|
|
'sendtime': time.time(),
|
|
'expiretime': time.time() + 3600 * 24 * 30,
|
|
'attachments': getMailAttachmentsByVip(mail_conf, vip_lv)
|
|
}
|
|
httpGet(url, params)
|
|
|
|
def fetchEvent():
|
|
global db_idx_hash
|
|
mysql_cluster_conf = json.loads(open(CONFIG_DIR + '/mailsender.mysql.cluster.json', 'r').read())
|
|
for conf in mysql_cluster_conf:
|
|
conn = pymysql.connect(host = conf['host'],
|
|
port = conf['port'],
|
|
user = conf['user'],
|
|
passwd = conf['passwd'],
|
|
db = conf['dbname'],
|
|
charset = 'utf8'
|
|
)
|
|
cursor = conn.cursor()
|
|
last_idx = db_idx_hash.get(conf['instance_id'], 0)
|
|
while True:
|
|
cursor.execute('SELECT idx, sender_id, event_name, param1, param2 '
|
|
'FROM `event` '
|
|
'WHERE idx > %d AND status = 0 AND event_name = "vip_level_up" '
|
|
'LIMIT 0, 1000'
|
|
% (last_idx))
|
|
for row in cursor:
|
|
last_idx = max(row[0], last_idx)
|
|
time.sleep(0.001);
|
|
else:
|
|
break
|
|
#end while
|
|
db_idx_hash[conf['instance_id']] = last_idx
|
|
|
|
def fetchEventTimerFunc(refresh_time):
|
|
def done_callback():
|
|
f7.timer.callLater(refresh_time,
|
|
lambda : fetchEventTimerFunc(refresh_time))
|
|
f7.app.createAsyncTask(done_callback, fetchEvent, ())
|
|
|
|
def dailyMail():
|
|
mysql_cluster_conf = json.loads(open(CONFIG_DIR + '/mailsender.mysql.cluster.json', 'r').read())
|
|
for conf in mysql_cluster_conf:
|
|
conn = pymysql.connect(host = conf['host'],
|
|
port = conf['port'],
|
|
user = conf['user'],
|
|
passwd = conf['passwd'],
|
|
db = conf['dbname'],
|
|
charset = 'utf8'
|
|
)
|
|
cursor = conn.cursor()
|
|
last_idx = db_idx_hash.get(conf['instance_id'], 0)
|
|
while True:
|
|
cursor.execute('SELECT idx, account_id, vip_lv, privileges, createtime, modifytime '
|
|
'FROM `vip_user` '
|
|
'WHERE idx > %d '
|
|
'LIMIT 0, 1000'
|
|
% (last_idx))
|
|
for row in cursor:
|
|
last_idx = max(row[0], last_idx)
|
|
time.sleep(0.001);
|
|
else:
|
|
break
|
|
#end while
|
|
|
|
def dailyMailTimerFunc(refresh_time):
|
|
def done_callback():
|
|
f7.timer.callAt(refresh_time,
|
|
lambda : dailyMailTimerFunc(refresh_time))
|
|
f7.app.createAsyncTask(done_callback, dailyMail, ())
|
|
|
|
def getConf():
|
|
cluster_conf = json.loads(open(CONFIG_DIR + '/mailsender.cluster.json', 'r').read())
|
|
for conf in cluster_conf:
|
|
if conf['instance_id'] == f7.app.getInstanceId():
|
|
return conf
|
|
return None
|
|
|
|
if __name__ == "__main__":
|
|
q7.xPrint('pid %d' % os.getpid())
|
|
f7.app.init('/data/logs/mailsender/logs')
|
|
f7.udplog.info('mailsender start pid:%d node_id:%d instance_id:%d' % (
|
|
os.getpid(),
|
|
f7.app.getNodeId(),
|
|
f7.app.getInstanceId())
|
|
)
|
|
|
|
conf = getConf()
|
|
# f7.app.registerHandler('Login', 'auth', __loginAuth)
|
|
conf['refresh_time'] = 10
|
|
f7.timer.callLater(conf['refresh_time'],
|
|
lambda : fetchEventTimerFunc(conf['refresh_time']))
|
|
conf['daily_mail_sendtime'] = 10
|
|
f7.timer.callAt(conf['daily_mail_sendtime'],
|
|
lambda : dailyMailTimerFunc(conf['daily_mail_sendtime']))
|
|
|
|
f7.app.listen(conf['listen_port'])
|
|
f7.app.start()
|