Updates pisad and __init__ to put together the log and config fixes

The setup of the logs and the parsing of the config file are closely related. The former need info from the later to be created, and needs to be setup only once
per pisa instance. In the same way, the later need to only be loaded and validated once per pisa intance and contains info to setup the logs.

Intead of setting up the logs in init and loading the config file in pisad, now both are dealt with in __init__
This commit is contained in:
Sergi Delgado Segura
2020-01-23 18:27:04 +01:00
parent 0fe5969346
commit 00a989e1b2
2 changed files with 38 additions and 73 deletions

View File

@@ -1,27 +1,37 @@
import logging import os
from pisa.utils.auth_proxy import AuthServiceProxy
import pisa.conf as conf import pisa.conf as conf
from common.tools import check_conf_fields, setup_logging, extend_paths
from pisa.utils.auth_proxy import AuthServiceProxy
HOST = "localhost" HOST = "localhost"
PORT = 9814 PORT = 9814
LOG_PREFIX = "pisa"
# Create the file logger # Load config fields
f_logger = logging.getLogger("pisa_file_log") conf_fields = {
f_logger.setLevel(logging.INFO) "BTC_RPC_USER": {"value": conf.BTC_RPC_USER, "type": str},
"BTC_RPC_PASSWD": {"value": conf.BTC_RPC_PASSWD, "type": str},
"BTC_RPC_HOST": {"value": conf.BTC_RPC_HOST, "type": str},
"BTC_RPC_PORT": {"value": conf.BTC_RPC_PORT, "type": int},
"BTC_NETWORK": {"value": conf.BTC_NETWORK, "type": str},
"FEED_PROTOCOL": {"value": conf.FEED_PROTOCOL, "type": str},
"FEED_ADDR": {"value": conf.FEED_ADDR, "type": str},
"FEED_PORT": {"value": conf.FEED_PORT, "type": int},
"DATA_FOLDER": {"value": conf.DATA_FOLDER, "type": str},
"MAX_APPOINTMENTS": {"value": conf.MAX_APPOINTMENTS, "type": int},
"EXPIRY_DELTA": {"value": conf.EXPIRY_DELTA, "type": int},
"MIN_TO_SELF_DELAY": {"value": conf.MIN_TO_SELF_DELAY, "type": int},
"SERVER_LOG_FILE": {"value": conf.SERVER_LOG_FILE, "type": str, "path": True},
"PISA_SECRET_KEY": {"value": conf.PISA_SECRET_KEY, "type": str, "path": True},
"DB_PATH": {"value": conf.DB_PATH, "type": str, "path": True},
}
fh = logging.FileHandler(conf.SERVER_LOG_FILE) # Expand user (~) if found and check fields are correct
fh.setLevel(logging.INFO) conf_fields["DATA_FOLDER"]["value"] = os.path.expanduser(conf_fields["DATA_FOLDER"]["value"])
fh_formatter = logging.Formatter("%(message)s") # Extend relative paths
fh.setFormatter(fh_formatter) conf_fields = extend_paths(conf_fields["DATA_FOLDER"]["value"], conf_fields)
f_logger.addHandler(fh)
# Create the console logger # Sanity check fields and build config dictionary
c_logger = logging.getLogger("pisa_console_log") config = check_conf_fields(conf_fields)
c_logger.setLevel(logging.INFO)
ch = logging.StreamHandler() setup_logging(config.get("SERVER_LOG_FILE"), LOG_PREFIX)
ch.setLevel(logging.INFO)
ch_formatter = logging.Formatter("%(message)s.", "%Y-%m-%d %H:%M:%S")
ch.setFormatter(ch_formatter)
c_logger.addHandler(ch)

View File

@@ -1,21 +1,20 @@
import os
from getopt import getopt from getopt import getopt
from sys import argv, exit from sys import argv, exit
from signal import signal, SIGINT, SIGQUIT, SIGTERM from signal import signal, SIGINT, SIGQUIT, SIGTERM
from common.logger import Logger from common.logger import Logger
from common.tools import check_conf_fields, setup_data_folder from common.tools import setup_data_folder
from pisa import config, LOG_PREFIX
from pisa.api import API from pisa.api import API
from pisa.watcher import Watcher from pisa.watcher import Watcher
from pisa.builder import Builder from pisa.builder import Builder
import pisa.conf as conf
from pisa.db_manager import DBManager from pisa.db_manager import DBManager
from pisa.chain_monitor import ChainMonitor from pisa.chain_monitor import ChainMonitor
from pisa.block_processor import BlockProcessor from pisa.block_processor import BlockProcessor
from pisa.tools import can_connect_to_bitcoind, in_correct_network from pisa.tools import can_connect_to_bitcoind, in_correct_network
logger = Logger("Daemon") logger = Logger(actor="Daemon", log_name_prefix=LOG_PREFIX)
def handle_signals(signal_received, frame): def handle_signals(signal_received, frame):
@@ -27,49 +26,6 @@ def handle_signals(signal_received, frame):
exit(0) exit(0)
def load_config(config):
"""
Looks through all of the config options to make sure they contain the right type of data and builds a config
dictionary.
Args:
config (:obj:`module`): It takes in a config module object.
Returns:
:obj:`dict` A dictionary containing the config values.
"""
conf_dict = {}
data_folder = config.DATA_FOLDER
if isinstance(data_folder, str):
data_folder = os.path.expanduser(data_folder)
else:
raise ValueError("The provided user folder is invalid.")
conf_fields = {
"BTC_RPC_USER": {"value": config.BTC_RPC_USER, "type": str},
"BTC_RPC_PASSWD": {"value": config.BTC_RPC_PASSWD, "type": str},
"BTC_RPC_HOST": {"value": config.BTC_RPC_HOST, "type": str},
"BTC_RPC_PORT": {"value": config.BTC_RPC_PORT, "type": int},
"BTC_NETWORK": {"value": config.BTC_NETWORK, "type": str},
"FEED_PROTOCOL": {"value": config.FEED_PROTOCOL, "type": str},
"FEED_ADDR": {"value": config.FEED_ADDR, "type": str},
"FEED_PORT": {"value": config.FEED_PORT, "type": int},
"DATA_FOLDER": {"value": data_folder, "type": str},
"MAX_APPOINTMENTS": {"value": config.MAX_APPOINTMENTS, "type": int},
"EXPIRY_DELTA": {"value": config.EXPIRY_DELTA, "type": int},
"MIN_TO_SELF_DELAY": {"value": config.MIN_TO_SELF_DELAY, "type": int},
"SERVER_LOG_FILE": {"value": data_folder, "type": str},
"PISA_SECRET_KEY": {"value": data_folder + config.PISA_SECRET_KEY, "type": str},
"DB_PATH": {"value": data_folder + config.DB_PATH, "type": str},
}
check_conf_fields(conf_fields, logger)
return conf_dict
def main(): def main():
global db_manager, chain_monitor global db_manager, chain_monitor
@@ -77,16 +33,15 @@ def main():
signal(SIGTERM, handle_signals) signal(SIGTERM, handle_signals)
signal(SIGQUIT, handle_signals) signal(SIGQUIT, handle_signals)
pisa_config = load_config(conf)
logger.info("Starting PISA") logger.info("Starting PISA")
setup_data_folder(pisa_config.get("DATA_FOLDER"), logger) setup_data_folder(config.get("DATA_FOLDER"), logger)
db_manager = DBManager(pisa_config.get("DB_PATH")) db_manager = DBManager(config.get("DB_PATH"))
if not can_connect_to_bitcoind(): if not can_connect_to_bitcoind():
logger.error("Can't connect to bitcoind. Shutting down") logger.error("Can't connect to bitcoind. Shutting down")
elif not in_correct_network(pisa_config.get("BTC_NETWORK")): elif not in_correct_network(config.get("BTC_NETWORK")):
logger.error("bitcoind is running on a different network, check conf.py and bitcoin.conf. Shutting down") logger.error("bitcoind is running on a different network, check conf.py and bitcoin.conf. Shutting down")
else: else:
@@ -98,10 +53,10 @@ def main():
watcher_appointments_data = db_manager.load_watcher_appointments() watcher_appointments_data = db_manager.load_watcher_appointments()
responder_trackers_data = db_manager.load_responder_trackers() responder_trackers_data = db_manager.load_responder_trackers()
with open(pisa_config.get("PISA_SECRET_KEY"), "rb") as key_file: with open(config.get("PISA_SECRET_KEY"), "rb") as key_file:
secret_key_der = key_file.read() secret_key_der = key_file.read()
watcher = Watcher(db_manager, chain_monitor, secret_key_der, pisa_config) watcher = Watcher(db_manager, chain_monitor, secret_key_der, config)
chain_monitor.attach_watcher(watcher.block_queue, watcher.asleep) chain_monitor.attach_watcher(watcher.block_queue, watcher.asleep)
chain_monitor.attach_responder(watcher.responder.block_queue, watcher.responder.asleep) chain_monitor.attach_responder(watcher.responder.block_queue, watcher.responder.asleep)
@@ -147,7 +102,7 @@ def main():
watcher.block_queue = Builder.build_block_queue(missed_blocks_watcher) watcher.block_queue = Builder.build_block_queue(missed_blocks_watcher)
# Fire the API # Fire the API
API(watcher, config=pisa_config).start() API(watcher, config=config).start()
except Exception as e: except Exception as e:
logger.error("An error occurred: {}. Shutting down".format(e)) logger.error("An error occurred: {}. Shutting down".format(e))