From 07734295e4fdeff0dbb3cee2cfaabc4f0a92d497 Mon Sep 17 00:00:00 2001 From: Tamas Gal <tgal@km3net.de> Date: Tue, 24 Sep 2019 15:28:09 +0200 Subject: [PATCH] Add log cycle for MSG dumps --- scripts/msg_dumper.py | 57 ++++++++++++++++++++++++++++----------- supervisord_template.conf | 2 +- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/scripts/msg_dumper.py b/scripts/msg_dumper.py index 036647c..190fb3e 100755 --- a/scripts/msg_dumper.py +++ b/scripts/msg_dumper.py @@ -13,25 +13,49 @@ Usage: Options: -l LIGIER_IP The IP of the ligier [default: 127.0.0.1]. -p LIGIER_PORT The port of the ligier [default: 5553]. - -f LOG_FILE Log file to dump the messages [default: MSG.log]. + -o LOG_DIR Directory to dump the messages [default: logs]. + -x PREFIX Prefix for the log files [default: MSG]. -h --help Show this screen. """ +import datetime import os -import time +from shutil import copyfile from km3pipe import Pipeline, Module from km3pipe.io import CHPump +def current_date_str(fmt="%Y-%m-%d"): + """Return the current datetime string""" + return datetime.datetime.now().strftime(fmt) + + class MSGDumper(Module): def configure(self): - self.filename = self.get('filename', default='MSG.log') - self.fobj = open(os.path.abspath(self.filename), 'a') + self.path = os.path.abspath(self.require('path')) + self.prefix = self.require('prefix') + self.current_date = current_date_str() + self.filename = self.prefix + ".log" + self.filepath = os.path.join(self.path, self.filename) + self.fobj = open(self.filepath, 'a') + + def update_file_descriptor(self): + current_date = current_date_str() + if self.current_date != current_date: + archived_name = "{}_{}.log".format(self.prefix, self.current_date) + self.print("Cycling the log file: {} -> {}".format( + self.filename, archived_name)) + copyfile(self.filepath, os.path.join(self.path, archived_name)) + self.fobj.close() + self.fobj = open(self.filepath, 'w') + self.current_date = current_date def process(self, blob): data = blob['CHData'].decode() source = "Other" + if " A0" in data: + source = "AcousticDataFilter" if " F0" in data: source = "DataFilter" if " Q0" in data: @@ -39,14 +63,15 @@ class MSGDumper(Module): if " W0" in data: source = "DataWriter" - entry = "{} [{}]: {}\n".format( - os.path.basename(self.filename), source, data) + entry = "{} [{}]: {}\n".format(self.filename, source, data) + self.update_file_descriptor() self.fobj.write(entry) self.fobj.flush() return blob def finish(self): - self.fobj.close() + if self.fobj is not None: + self.fobj.close() def main(): @@ -55,17 +80,17 @@ def main(): ligier_ip = args['-l'] ligier_port = int(args['-p']) - filename = args['-f'] + path = args['-o'] + prefix = args['-x'] pipe = Pipeline() - pipe.attach( - CHPump, - host=ligier_ip, - port=ligier_port, - tags='MSG', - timeout=7 * 60 * 60 * 24, - max_queue=500) - pipe.attach(MSGDumper, filename=filename) + pipe.attach(CHPump, + host=ligier_ip, + port=ligier_port, + tags='MSG', + timeout=7 * 60 * 60 * 24, + max_queue=500) + pipe.attach(MSGDumper, prefix=prefix, path=path) pipe.drain() diff --git a/supervisord_template.conf b/supervisord_template.conf index 897c2c4..8fabcee 100644 --- a/supervisord_template.conf +++ b/supervisord_template.conf @@ -146,7 +146,7 @@ stderr_logfile=logs/%(program_name)s.err.log ;stderr_logfile=logs/%(program_name)s.err.log [program:msg_dumper] -command=python -u scripts/msg_dumper.py -l %(ENV_LOG_LIGIER_IP)s -p %(ENV_LOG_LIGIER_PORT)s -f logs/MSG.log +command=python -u scripts/msg_dumper.py -l %(ENV_LOG_LIGIER_IP)s -p %(ENV_LOG_LIGIER_PORT)s -o logs priority=200 stdout_logfile=logs/%(program_name)s.out.log stderr_logfile=logs/%(program_name)s.err.log -- GitLab