From 1355c6322ba05320eb060da5b42904f5a1583e93 Mon Sep 17 00:00:00 2001 From: lionel Date: Fri, 26 Aug 2016 14:56:21 +0200 Subject: [PATCH 1/1] Initial commit --- doc/README | 1 + log/.gitignore | 4 +++ pid/.gitignore | 4 +++ test.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 doc/README create mode 100644 log/.gitignore create mode 100644 pid/.gitignore create mode 100644 test.py diff --git a/doc/README b/doc/README new file mode 100644 index 0000000..fde8d0b --- /dev/null +++ b/doc/README @@ -0,0 +1 @@ +For the moment ? nothing diff --git a/log/.gitignore b/log/.gitignore new file mode 100644 index 0000000..5e7d273 --- /dev/null +++ b/log/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore diff --git a/pid/.gitignore b/pid/.gitignore new file mode 100644 index 0000000..5e7d273 --- /dev/null +++ b/pid/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore diff --git a/test.py b/test.py new file mode 100644 index 0000000..5f9c5f8 --- /dev/null +++ b/test.py @@ -0,0 +1,88 @@ +#!/usr/bin/python3 +# -*- coding:Utf-8 -*- + +import logging # Logs +import time # sleep +import string # split strings +import os,select + +from daemon import runner # daemonization + +from multiprocessing import Process +#import subprocess +#import shlex + +# logger.debug("Debug message") +# logger.info("Info message") +# logger.warn("Warning message") +# logger.error("Error message") + +class App(): + + def __init__(self): + self.stdin_path = "/dev/null" + self.stdout_path = "/home/lionel/work/python/rome/log/stdout.log" + self.stderr_path = "/home/lionel/work/python/rome/log/stderr.log" + self.pidfile_path = "/home/lionel/work/python/rome/pid/horusd.pid" + self.pidfile_timeout = 5 + + def run(self): +# We must put all functions before the 'if __name__ blabla' + + def heartbeat(min): + seconds = int(min) * 60 + while True: + logger.info("HB : alive") + time.sleep(seconds) + return False + + +# All functions definitions took place above + if __name__ == '__main__': + logger.info("Rome test started") + + ''' + Launching the heartbeat function + ''' + p = Process(target=heartbeat, args=(2,)) + p.daemon = True + p.start() + heartbeat_pid = p.pid + logger.info("heartbeat loop launched (pid " + str(heartbeat_pid) + ")") + + ''' + Entering infinite loop + ''' + + try: + while True: + time.sleep(30) + except: + logger.error("While loop interrupted : aborting") + # Faut arrêter les Process ... + +# Invocation of the daemon part +# Means : if it's working : DON'T TOUCH ANYTHING +if __name__ == "__main__": + app = App() + logger = logging.getLogger("horusd") + loglevel = "debug" + if (loglevel=="DEBUG") or (loglevel=="debug"): + logger.setLevel(logging.DEBUG) + elif (loglevel=="INFO") or (loglevel=="info"): + logger.setLevel(logging.INFO) + elif (loglevel=="WARN") or (loglevel=="warn"): + logger.setLevel(logging.WARN) + elif (loglevel=="ERROR") or (loglevel=="error"): + logger.setLevel(logging.ERROR) + else: + logger.setLevel(logging.DEBUG) + formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + handler = logging.FileHandler("log/horusd.log") + handler.setFormatter(formatter) + logger.addHandler(handler) + + daemon_runner = runner.DaemonRunner(app) + #This ensures that the logger file handle does not get closed during daemonization + daemon_runner.daemon_context.files_preserve=[handler.stream] + daemon_runner.do_action() -- 2.39.2