hot load config

support userapiconfig
This commit is contained in:
BreakWa11 2016-06-08 16:15:26 +08:00
parent 4d43e12139
commit 569aaa5248
9 changed files with 63 additions and 27 deletions

View file

@ -1,6 +1,7 @@
# Config # Config
TRANSFER_MUL = 1.0 TRANSFER_MUL = 1.0
API_INTERFACE = 'mysql' NODE_ID = 1
API_INTERFACE = 'sspanelv2' #sspanelv2, muapiv2
# Mysql # Mysql
MYSQL_HOST = 'mdss.mengsky.net' MYSQL_HOST = 'mdss.mengsky.net'
@ -11,11 +12,10 @@ MYSQL_DB = 'shadowsocks'
MYSQL_UPDATE_TIME = 60 MYSQL_UPDATE_TIME = 60
# API # API
API_HOST = 'breakwa11.org' API_HOST = 'breakwa11.moe'
API_PORT = 80 API_PORT = 80
API_PATH = '/mu/v2/' API_PATH = '/mu/v2/'
API_TOKEN = 'abcdef' API_TOKEN = 'abcdef'
API_NODE_ID = 'id001'
API_UPDATE_TIME = 60 API_UPDATE_TIME = 60
# Manager (ignore this) # Manager (ignore this)

View file

@ -16,5 +16,4 @@
"connect_verbose_info": 0, "connect_verbose_info": 0,
"redirect": "", "redirect": "",
"fast_open": false, "fast_open": false,
"workers": 1
} }

27
configloader.py Normal file
View file

@ -0,0 +1,27 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
config = None
def load_config():
global config
try:
import userapiconfig
reload(userapiconfig)
config = userapiconfig
return
except:
pass
try:
import apiconfig
reload(apiconfig)
config = apiconfig
except:
pass
def get_config():
global config
return config
load_config()

View file

@ -6,9 +6,9 @@ import cymysql
import time import time
import sys import sys
from server_pool import ServerPool from server_pool import ServerPool
import Config
import traceback import traceback
from shadowsocks import common from shadowsocks import common
from configloader import load_config, get_config
class DbTransfer(object): class DbTransfer(object):
@ -39,16 +39,16 @@ class DbTransfer(object):
continue continue
elif last_transfer[id][0] <= curr_transfer[id][0] and \ elif last_transfer[id][0] <= curr_transfer[id][0] and \
last_transfer[id][1] <= curr_transfer[id][1]: last_transfer[id][1] <= curr_transfer[id][1]:
dt_transfer[id] = [int((curr_transfer[id][0] - last_transfer[id][0]) * Config.TRANSFER_MUL), dt_transfer[id] = [int((curr_transfer[id][0] - last_transfer[id][0]) * get_config().TRANSFER_MUL),
int((curr_transfer[id][1] - last_transfer[id][1]) * Config.TRANSFER_MUL)] int((curr_transfer[id][1] - last_transfer[id][1]) * get_config().TRANSFER_MUL)]
else: else:
dt_transfer[id] = [int(curr_transfer[id][0] * Config.TRANSFER_MUL), dt_transfer[id] = [int(curr_transfer[id][0] * get_config().TRANSFER_MUL),
int(curr_transfer[id][1] * Config.TRANSFER_MUL)] int(curr_transfer[id][1] * get_config().TRANSFER_MUL)]
else: else:
if curr_transfer[id][0] == 0 and curr_transfer[id][1] == 0: if curr_transfer[id][0] == 0 and curr_transfer[id][1] == 0:
continue continue
dt_transfer[id] = [int(curr_transfer[id][0] * Config.TRANSFER_MUL), dt_transfer[id] = [int(curr_transfer[id][0] * get_config().TRANSFER_MUL),
int(curr_transfer[id][1] * Config.TRANSFER_MUL)] int(curr_transfer[id][1] * get_config().TRANSFER_MUL)]
query_head = 'UPDATE user' query_head = 'UPDATE user'
query_sub_when = '' query_sub_when = ''
@ -71,8 +71,8 @@ class DbTransfer(object):
' END, t = ' + str(int(last_time)) + \ ' END, t = ' + str(int(last_time)) + \
' WHERE port IN (%s)' % query_sub_in ' WHERE port IN (%s)' % query_sub_in
#print query_sql #print query_sql
conn = cymysql.connect(host=Config.MYSQL_HOST, port=Config.MYSQL_PORT, user=Config.MYSQL_USER, conn = cymysql.connect(host=get_config().MYSQL_HOST, port=get_config().MYSQL_PORT, user=get_config().MYSQL_USER,
passwd=Config.MYSQL_PASS, db=Config.MYSQL_DB, charset='utf8') passwd=get_config().MYSQL_PASS, db=get_config().MYSQL_DB, charset='utf8')
cur = conn.cursor() cur = conn.cursor()
cur.execute(query_sql) cur.execute(query_sql)
cur.close() cur.close()
@ -90,8 +90,8 @@ class DbTransfer(object):
except Exception as e: except Exception as e:
keys = ['port', 'u', 'd', 'transfer_enable', 'passwd', 'enable' ] keys = ['port', 'u', 'd', 'transfer_enable', 'passwd', 'enable' ]
reload(cymysql) reload(cymysql)
conn = cymysql.connect(host=Config.MYSQL_HOST, port=Config.MYSQL_PORT, user=Config.MYSQL_USER, conn = cymysql.connect(host=get_config().MYSQL_HOST, port=get_config().MYSQL_PORT, user=get_config().MYSQL_USER,
passwd=Config.MYSQL_PASS, db=Config.MYSQL_DB, charset='utf8') passwd=get_config().MYSQL_PASS, db=get_config().MYSQL_DB, charset='utf8')
cur = conn.cursor() cur = conn.cursor()
cur.execute("SELECT " + ','.join(keys) + " FROM user") cur.execute("SELECT " + ','.join(keys) + " FROM user")
rows = [] rows = []
@ -180,7 +180,7 @@ class DbTransfer(object):
last_rows = [] last_rows = []
try: try:
while True: while True:
reload(Config) load_config()
try: try:
DbTransfer.get_instance().push_db_all_user() DbTransfer.get_instance().push_db_all_user()
rows = DbTransfer.get_instance().pull_db_all_user() rows = DbTransfer.get_instance().pull_db_all_user()
@ -190,7 +190,7 @@ class DbTransfer(object):
trace = traceback.format_exc() trace = traceback.format_exc()
logging.error(trace) logging.error(trace)
#logging.warn('db thread except:%s' % e) #logging.warn('db thread except:%s' % e)
if DbTransfer.get_instance().event.wait(Config.MYSQL_UPDATE_TIME) or not ServerPool.get_instance().thread.is_alive(): if DbTransfer.get_instance().event.wait(get_config().MYSQL_UPDATE_TIME) or not ServerPool.get_instance().thread.is_alive():
break break
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
pass pass

6
logrun.sh Normal file
View file

@ -0,0 +1,6 @@
#!/bin/bash
cd `dirname $0`
eval $(ps -ef | grep "[0-9] python server\\.py m" | awk '{print "kill "$2}')
ulimit -n 512000
nohup python server.py m>> ssserver.log 2>&1 &

2
run.sh
View file

@ -2,5 +2,5 @@
cd `dirname $0` cd `dirname $0`
eval $(ps -ef | grep "[0-9] python server\\.py m" | awk '{print "kill "$2}') eval $(ps -ef | grep "[0-9] python server\\.py m" | awk '{print "kill "$2}')
ulimit -n 512000 ulimit -n 512000
nohup python server.py m>> ssserver.log 2>&1 & nohup python server.py m>> /dev/null 2>&1 &

View file

@ -24,16 +24,11 @@
import os import os
import logging import logging
import time import time
from shadowsocks import shell from shadowsocks import shell, eventloop, tcprelay, udprelay, asyncdns
from shadowsocks import eventloop
from shadowsocks import tcprelay
from shadowsocks import udprelay
from shadowsocks import asyncdns
import threading import threading
import sys import sys
import asyncmgr
import Config
from socket import * from socket import *
from configloader import load_config, get_config
class MainThread(threading.Thread): class MainThread(threading.Thread):
def __init__(self, params): def __init__(self, params):
@ -127,6 +122,7 @@ class ServerPool(object):
a_config['server'] = a_config['server_ipv6'] a_config['server'] = a_config['server_ipv6']
a_config['server_port'] = port a_config['server_port'] = port
a_config['password'] = password a_config['password'] = password
a_config['max_connect'] = 128
try: try:
logging.info("starting server at [%s]:%d" % (a_config['server'], port)) logging.info("starting server at [%s]:%d" % (a_config['server'], port))
@ -151,6 +147,7 @@ class ServerPool(object):
a_config = self.config.copy() a_config = self.config.copy()
a_config['server_port'] = port a_config['server_port'] = port
a_config['password'] = password a_config['password'] = password
a_config['max_connect'] = 128
try: try:
logging.info("starting server at %s:%d" % (a_config['server'], port)) logging.info("starting server at %s:%d" % (a_config['server'], port))
@ -173,7 +170,7 @@ class ServerPool(object):
logging.info("del server at %d" % port) logging.info("del server at %d" % port)
try: try:
udpsock = socket(AF_INET, SOCK_DGRAM) udpsock = socket(AF_INET, SOCK_DGRAM)
udpsock.sendto('%s:%s:0:0' % (Config.MANAGE_PASS, port), (Config.MANAGE_BIND_IP, Config.MANAGE_PORT)) udpsock.sendto('%s:%s:0:0' % (get_config().MANAGE_PASS, port), (get_config().MANAGE_BIND_IP, get_config().MANAGE_PORT))
udpsock.close() udpsock.close()
except Exception as e: except Exception as e:
logging.warn(e) logging.warn(e)

6
shadowsocks/logrun.sh Normal file
View file

@ -0,0 +1,6 @@
#!/bin/bash
cd `dirname $0`
eval $(ps -ef | grep "[0-9] python server\\.py a" | awk '{print "kill "$2}')
ulimit -n 4096
nohup python server.py a >> ssserver.log 2>&1 &

View file

@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
cd `dirname $0` cd `dirname $0`
eval $(ps -ef | grep "[0-9] python server\\.py a" | awk '{print "kill "$2}') eval $(ps -ef | grep "[0-9] python server\\.py a" | awk '{print "kill "$2}')
nohup python server.py a >> ssserver.log 2>&1 & ulimit -n 4096
nohup python server.py a >> /dev/null 2>&1 &