From cf7adb015a947dad677aaca03254fbab3b5242e1 Mon Sep 17 00:00:00 2001 From: lamter Date: Wed, 26 Apr 2017 22:47:59 +0800 Subject: [PATCH] =?UTF-8?q?=20-=20change=20:=20=E6=B7=BB=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=85=B3=E9=97=AD=E6=9C=8D=E5=8A=A1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/vtFunction.py | 61 +++++++++++++++++++++++++++++++++++++---- vn.trader/vtServer.py | 2 ++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/vn.trader/vtFunction.py b/vn.trader/vtFunction.py index c299d327..a8aaf6de 100644 --- a/vn.trader/vtFunction.py +++ b/vn.trader/vtFunction.py @@ -4,9 +4,12 @@ 包含一些开发中常用的函数 """ +import vtGlobal import os import decimal import json +import time +import datetime as dt from datetime import datetime MAX_NUMBER = 10000000000000 @@ -31,13 +34,14 @@ def safeUnicode(value): #---------------------------------------------------------------------- def loadMongoSetting(): """载入MongoDB数据库的配置""" - fileName = 'VT_setting.json' - path = os.path.abspath(os.path.dirname(__file__)) - fileName = os.path.join(path, fileName) - + # fileName = 'VT_setting.json' + # path = os.path.abspath(os.path.dirname(__file__)) + # fileName = os.path.join(path, fileName) + setting = vtGlobal.VT_setting.copy() + try: - f = file(fileName) - setting = json.load(f) + # f = file(fileName) + # setting = json.load(f) host = setting['mongoHost'] port = setting['mongoPort'] logging = setting['mongoLogging'] @@ -54,3 +58,48 @@ def todayDate(): return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) +def autoshutdown(clocks=None): + """ + 根据当前启动时间,设置自动关闭时间 + 2:31 之前启动服务器,那么在2:31 会自动关闭; + 2:31 ~ 8:39 之间启动,会在8:39关闭,以此类推; + 20:39 之后关闭,会在次日 2:31 自动关闭 + + + >>> autoshutdown().closeTime == todayDate().replace(hour=15, minute=39) + True + + :return: + """ + + clocks = clocks or [ + dt.time(2, 31), # 夜盘后关闭 + dt.time(8, 39), # 日盘前关闭 + dt.time(15, 39), # 日盘后关闭 + dt.time(20, 39), # 夜盘前关闭 + ] + from threading import Thread + + now = datetime.now() + today = todayDate() + tomorrow = today + dt.timedelta(days=1) + + for t in clocks: + if now.time() < t: + # 关闭时间在当日 + closeTime = datetime.combine(today, t) + break + else: + # 关闭时间在次日 + closeTime = datetime.combine(tomorrow, clocks[0]) + + def shutdown(closeTime): + # 子线程阻塞直到到达关闭时间 + while datetime.now() < closeTime: + time.sleep(1) + + t = Thread(target=shutdown, args=(closeTime,)) + t.closeTime = closeTime + t.start() + return t + diff --git a/vn.trader/vtServer.py b/vn.trader/vtServer.py index f2d877cd..4742f193 100644 --- a/vn.trader/vtServer.py +++ b/vn.trader/vtServer.py @@ -7,6 +7,7 @@ from argparse import ArgumentParser from datetime import datetime +from vtFunction import autoshutdown from vnrpc import RpcServer from vtEngine import MainEngine @@ -86,6 +87,7 @@ def runServer(): # 自动建立MongoDB数据库 printLog(u'MongoDB connect... ') server.engine.dbConnect() + printLog(u'MongoDB OK... ') if VT_setting.get('autoctp'): # 自动建立CTP链接