Closed chenluyong closed 4 years ago
Restarting the service when it is not available can also help you avoid this error.
当服务不可用时重新启动该服务,也可以帮助您降低此错误的影响.
maybe you need to monitor.py
.
#!/usr/bin/python
#coding=UTF-8
import configparser
import sys
import requests
import json
import os
import time
import logging
"""
"""
network = "mainnet"
is_success = True
def config():
cp = configparser.ConfigParser()
the_dir = sys.path[0]
cp.read(the_dir+'/db-'+network+".conf")
return cp
def rpc_query(params):
cp = config()
host = cp.get('host','via-http-rpc')
headers = {'Content-Type': 'application/json', 'Connection':'close'}
post_data = {"id":1,"method":"market.kline","params":params}
encode_json = json.dumps(post_data)
try:
P_post=requests.post(host,headers=headers,data=encode_json,timeout=5)
if P_post.status_code == 200:
rst = json.loads(P_post.text)
return ("result" in (rst))
else:
return False
except requests.RequestException as e:
logging.error(e)
return False
def restart(process):
cp = config()
cmd_list = [cp.get(process, "suicide_cmd"),cp.get(process, "start_cmd")]
# 1. kill process
# 2. start
for cmd in cmd_list:
logging.debug("exec "+ cmd)
rep = os.popen(cmd)
logging.debug(rep)
time.sleep(1)
def get_pid(process_name):
for dirname in os.listdir('/proc'):
if dirname == 'curproc':
continue
try:
with open('/proc/{}/cmdline'.format(dirname), mode='rb') as fd:
content = fd.read().decode().split('\x00')
except Exception:
continue
if process_name in content[0]:
logging.debug('{0:<12} : {1}'.format(dirname, ' '.join(content)))
return dirname
def kill9(process):
# temp
process_num = get_pid(process)
cmd = "kill -9 " + process_num
logging.debug(cmd)
logging.debug(os.popen(cmd).read())
def check_marketprice():
global is_success
if (not rpc_query(["BTCUSDT", 1556589477, 1559181537, 30])):
logging.warning("marketprice.exe service exceptions.")
if (not is_success):
kill9("marketprice.exe")
logging.warning("执行强杀")
if (is_success):
notification_letter("糟糕,K线服务蹦了")
is_success = False
restart("marketprice")
return False
else:
if (not is_success):
is_success = True
notification_letter("不要慌,一切有我!")
logging.info("ping")
return True
def monitor():
check_marketprice()
def notification_letter(msg):
cp = config()
webhook = cp.get("dingding", "webhook")
headers = {'Content-Type': 'application/json', 'Connection':'close'}
post_data = {
"msgtype": "text",
"text": {
"content": msg
}
}
encode_json = json.dumps(post_data)
response = requests.post(webhook,headers=headers,data=encode_json,timeout=5)
logging.info(response)
def main(argv):
if (len(argv) < 2):
logging.info("\tplase input network(mainnet/testnet)")
return
if argv[1]=="testnet" or argv[1] == "mainnet":
global network
network = argv[1]
while True:
time.sleep(10)
monitor()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
try :
logging.info("process start success")
notification_letter("希望你认识的我,是一个安静的美男子!")
main(sys.argv)
except Exception as e:
logging.error("process exception exit.")
notification_letter("先崩为敬!")
the monitor.py
depends on db.conf
the db.conf
# database source
[network]
mainnet = testnet # mainnet testnet
[db]
host = localhost
name = root
user = root
pass = root
port = 3306
charset = utf8
sock = /tmp/mysql.sock
[host]
via-http-rpc = http://localhost:8080
[marketprice]
path = /root/cloud/coin_exchange/marketprice/
suicide_cmd = killall -s SIGQUIT marketprice.exe
start_cmd = /root/cloud/coin_exchange/marketprice.exe /root/cloud/coin_exchange/marketprice.config.json
[dingding]
webhook = https://oapi.dingtalk.com/robot/send?access_token=you_access_token
Untidy code, I hope to help you.
FIXED
get_market_kline_month
endless loop.Because there's no data.
Maybe the same loop will happen with other functions.
I submitted
PR
, hope it helps.it's here #191 .