Skip to content

Commit

Permalink
[update] launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jhao104 committed Mar 26, 2021
1 parent 5b5303a commit 3aeac7c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 97 deletions.
74 changes: 0 additions & 74 deletions db/MongodbClient.py

This file was deleted.

16 changes: 3 additions & 13 deletions db/dbClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ def __init__(self, db_conn):
init
:return:
"""
self.db_conn = db_conn
self.parseDbConn(db_conn)
self.__printConfig()
self.__initDbClient()

@classmethod
Expand All @@ -79,8 +77,6 @@ def __initDbClient(self):
__type = "ssdbClient"
elif "REDIS" == self.db_type:
__type = "redisClient"
elif "MONGODB" == self.db_type:
__type = "mongodbClient"
else:
pass
assert __type, 'type error, Not support DB type: {}'.format(self.db_type)
Expand All @@ -90,15 +86,6 @@ def __initDbClient(self):
password=self.db_pwd,
db=self.db_name)

def __printConfig(self):
print("============ DATABASE CONFIGURE =========================")
print("DB_TYPE: %s" % self.db_type)
print("DB_HOST: %s" % self.db_host)
print("DB_PORT: %s" % self.db_port)
print("DB_NAME: %s" % self.db_name)
print("DB_USER: %s" % self.db_user)
print("=========================================================")

def get(self, **kwargs):
return self.client.get(**kwargs)

Expand Down Expand Up @@ -128,3 +115,6 @@ def changeTable(self, name):

def getCount(self):
return self.client.getCount()

def test(self):
return self.client.test()
21 changes: 20 additions & 1 deletion db/redisClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"""
__author__ = 'JHao'

from redis.exceptions import TimeoutError, ConnectionError, ResponseError
from redis.connection import BlockingConnectionPool
from handler.logHandler import LogHandler
from random import choice
from redis import Redis

Expand All @@ -38,7 +40,10 @@ def __init__(self, **kwargs):
"""
self.name = ""
kwargs.pop("username")
self.__conn = Redis(connection_pool=BlockingConnectionPool(decode_responses=True, **kwargs))
self.__conn = Redis(connection_pool=BlockingConnectionPool(decode_responses=True,
timeout=5,
socket_timeout=5,
**kwargs))

def get(self):
"""
Expand Down Expand Up @@ -127,3 +132,17 @@ def changeTable(self, name):
:return:
"""
self.name = name

def test(self):
log = LogHandler('redis_client')
try:
self.getCount()
except TimeoutError as e:
log.error('redis connection time out: %s' % str(e), exc_info=True)
return e
except ConnectionError as e:
log.error('redis connection error: %s' % str(e), exc_info=True)
return e
except ResponseError as e:
log.error('redis connection error: %s' % str(e), exc_info=True)
return e
22 changes: 20 additions & 2 deletions db/ssdbClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
-------------------------------------------------
"""
__author__ = 'JHao'

from redis.exceptions import TimeoutError, ConnectionError, ResponseError
from redis.connection import BlockingConnectionPool
from handler.logHandler import LogHandler
from random import choice
from redis import Redis

Expand All @@ -39,7 +40,10 @@ def __init__(self, **kwargs):
"""
self.name = ""
kwargs.pop("username")
self.__conn = Redis(connection_pool=BlockingConnectionPool(decode_responses=True, **kwargs))
self.__conn = Redis(connection_pool=BlockingConnectionPool(decode_responses=True,
timeout=5,
socket_timeout=5,
**kwargs))

def get(self):
"""
Expand Down Expand Up @@ -128,3 +132,17 @@ def changeTable(self, name):
:return:
"""
self.name = name

def test(self):
log = LogHandler('ssdb_client')
try:
self.getCount()
except TimeoutError as e:
log.error('ssdb connection time out: %s' % str(e), exc_info=True)
return e
except ConnectionError as e:
log.error('ssdb connection error: %s' % str(e), exc_info=True)
return e
except ResponseError as e:
log.error('ssdb connection error: %s' % str(e), exc_info=True)
return e
66 changes: 66 additions & 0 deletions helper/launcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: launcher
Description : 启动器
Author : JHao
date: 2021/3/26
-------------------------------------------------
Change Activity:
2021/3/26: 启动器
-------------------------------------------------
"""
__author__ = 'JHao'

import sys
from db.dbClient import DbClient
from handler.logHandler import LogHandler
from handler.configHandler import ConfigHandler

log = LogHandler('launcher')


def startServer():
__beforeStart()
from api.proxyApi import runFlask
runFlask()


def startScheduler():
__beforeStart()
from helper.scheduler import runScheduler
runScheduler()


def __beforeStart():
__showVersion()
__showConfigure()
if __checkDBConfig():
log.info('exit!')
sys.exit()


def __showVersion():
from setting import VERSION
log.info("ProxyPool Version: %s" % VERSION)


def __showConfigure():
conf = ConfigHandler()
log.info("ProxyPool configure HOST: %s" % conf.serverHost)
log.info("ProxyPool configure PORT: %s" % conf.serverPort)
log.info("ProxyPool configure DB_CONN: %s" % conf.dbConn)
log.info("ProxyPool configure PROXY_FETCHER: %s" % conf.fetchers)


def __checkDBConfig():
conf = ConfigHandler()
db = DbClient(conf.dbConn)
log.info("============ DATABASE CONFIGURE ================")
log.info("DB_TYPE: %s" % db.db_type)
log.info("DB_HOST: %s" % db.db_host)
log.info("DB_PORT: %s" % db.db_port)
log.info("DB_NAME: %s" % db.db_name)
log.info("DB_USER: %s" % db.db_user)
log.info("=================================================")
return db.test()
10 changes: 3 additions & 7 deletions proxyPool.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
__author__ = 'JHao'

import click

from helper.launcher import startServer, startScheduler
from setting import BANNER, VERSION

CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
Expand All @@ -29,18 +29,14 @@ def cli():
def schedule():
""" 启动调度程序 """
click.echo(BANNER)
click.echo("VERSION: %s\n" % VERSION)
from helper.scheduler import runScheduler
runScheduler()
startScheduler()


@cli.command(name="server")
def server():
""" 启动api服务 """
click.echo(BANNER)
click.echo("VERSION: %s\n" % VERSION)
from api.proxyApi import runFlask
runFlask()
startServer()


if __name__ == '__main__':
Expand Down

0 comments on commit 3aeac7c

Please sign in to comment.