def __init__(self, code, buyPriceList=[], salePriceList=[], notifyUrl='', conf=None): self.code = code self.buyPriceList = buyPriceList self.salePriceList = salePriceList self.notifyUrl = notifyUrl self.status = Watcher.STATUS_STOP self.logger = Log('./' + self.code + '.log') self.commonLogger = None self.globalConf = conf
def get_logger(logger_name): global logger if logger == None: logger = {} if logger_name in logger: return logger.get(logger_name) if logger_name not in CONF.get('log'): raise Exception('logger_name not found') if 'file' not in CONF.get('log').get(logger_name): raise Exception('file node not found') logger[logger_name] = Log(CONF.get('log').get(logger_name).get('file')) return logger.get(logger_name)
class Watcher: STATUS_RUN = 1 STATUS_STOP = 2 def __init__(self, code, buyPriceList=[], salePriceList=[], notifyUrl='', conf=None): self.code = code self.buyPriceList = buyPriceList self.salePriceList = salePriceList self.notifyUrl = notifyUrl self.status = Watcher.STATUS_STOP self.logger = Log('./' + self.code + '.log') self.commonLogger = None self.globalConf = conf def isDisable(self): if self.globalConf == None: return False findStock = None for stock in self.globalConf.reload().data['stockList']: if stock['code'] == self.code: findStock = stock break if findStock == None: return True if 'enable' in findStock and findStock['enable'] == False: return True else: return False def setCommonLogger(self, logger): self.commonLogger = logger def start(self): self.status = Watcher.STATUS_RUN while 1: gevent.sleep(1) if self.status == Watcher.STATUS_STOP: #print('watcher stopped') return if self.isDisable(): #print('%s warcher disable' % self.code ) continue self.watchOnce() def stop(self): self.status = Watcher.STATUS_STOP def watchOnce(self): if not Point.isStcokTime(): print('is not stock time, current time:' + str(dt.current_time())) return try: point = Point.getNow(self.code) except: return self.onNewPoint(point) def onBuyEvent(self, nowPrice, buyPrice, point): #print('on buy event', 'now:', nowPrice, 'buy:', buyPrice) msg = NotifyTpl.genNotify(point.name, nowPrice, NotifyTpl.ACTION_BUY, '(<=%s)' % buyPrice) UniMemQueue.getInstance().push(msg) def onSaleEvent(self, nowPrice, salePrice, point): #print('on sale event', 'now:', nowPrice, 'sale:', salePrice) msg = NotifyTpl.genNotify(point.name, nowPrice, NotifyTpl.ACTION_SALE, '(>=%s)' % salePrice) UniMemQueue.getInstance().push(msg) def onNewPoint(self, point): self.logger.info("code:%s, name:%s, now:%s, time:%s" % (point.code, point.name, point.now, point.time)) if self.commonLogger: self.commonLogger.info( "code:%s, name:%s, now:%s, time:%s" % (point.code, point.name, point.now, point.time)) now = point.now for p in self.buyPriceList: if now <= p: self.onBuyEvent(now, p, point) for p in self.salePriceList: if now >= p: self.onSaleEvent(now, p, point)
#coding=utf-8 from decouple import config from agileutil.log import Log output_tag = False if config('LOG_OUTPUT').lower() == 'true': output_tag = True if config('LOG_OUTPUT').lower() == 'false': output_tag = False logger = Log(config('LOG_FILE')) logger.setOutput(output_tag) def info(log_info): logger.info(log_info) def warning(log_info): logger.warning(log_info) def error(log_info): logger.error(log_info)
def initLog(self): if self.logger == None and self.log != '': self.logger = Log(self.log)
def run_high_prob_role_strategy(logger=None): if logger == None: logger = Log('./logs/' + sys._getframe().f_code.co_name + '.log') HighProbRoseStrategy.logger = logger HighProbRoseStrategy.run()
#from gevent import monkey #monkey.patch_all() from lib.config import Config #from lib.watcher import Watcher #from lib.point import Point #import lib.notify import sys import os #from agileutil.queue import UniMemQueue from agileutil.log import Log #from lib.stock import StockList import lib.strategy as strategy #import agileutil.wrap as awrap #from lib.spawn import spawn_process os.environ['TZ'] = 'Asia/Shanghai' commonLogger = Log('./common.log') strategyLogger = Log("./stragegy.log") ''' def init(): lib.notify.init() StockList.getInstance().getAllStock() def run(stock, notifyUrl, conf): watcher = Watcher(stock['code'], buyPriceList=stock['buyPriceList'], salePriceList=stock['salePriceList'], notifyUrl=notifyUrl, conf=conf) watcher.setCommonLogger(commonLogger) watcher.start()
import requests import cache as cache import sys sys.path.append('../') sys.path.append('../../') from multiprocessing import Process from decouple import config from agileutil.log import Log import time from model.group import GroupModel from model.project import ProjectModel from model.environment import EnvironmentModel from model.upstream import UpstreamModel log = Log(config('UPSTREAM_CHECK_LOG_FILE')) def set_resty_upstream(openresty, upstream, oper): if openresty == None or upstream == None or oper == None: return log.info('ready to call openresty api set upstream %s:%s to %s' % (upstream['host'], upstream['port'], upstream['status'])) host = openresty['host'] port = openresty['port'] uri = config('UPSTREAT_RESTY_UP_DOWN_URI') url = 'http://%s:%s/%s' % (host, port, uri) upstream_host = upstream['host'] upstream_port = upstream['port'] params = {'host': upstream_host, 'port': upstream_port, 'oper': oper} if oper == 'down': params['ttl'] = config('UPSTREAM_DISABLE_TTL', cast=int)