Index: cs.py =================================================================== diff -u -r029aec0d4746d8a2897e46516cf9d39629d684b5 -r99d35f3d5898d50372e1745b96b393db40cd8394 --- cs.py (.../cs.py) (revision 029aec0d4746d8a2897e46516cf9d39629d684b5) +++ cs.py (.../cs.py) (revision 99d35f3d5898d50372e1745b96b393db40cd8394) @@ -10,8 +10,13 @@ from subprocess import Popen from cloudsync.utils.globals import EXEC_MODE_UPGRADE, EXEC_MODE_UPDATE, EXEC_MODE_NORMAL, EXEC_MODE +logging.basicConfig(format='%(message)s', level=logging.INFO) +logger = logging.getLogger(__name__) + DELAY = 0.5 -USAGE_FORMAT = "Usage: ./cs.py [debug|info|warning|error] [upgrade|update|]" +SENTINEL_FILE = "/tmp/cloudsync_restart_sentinel" +SENTINEL_CHECK_INTERVAL = 5 +USAGE_FORMAT = "Usage: ./cs.py [debug|info|warning|error] [upgrade|update|]" arguments = sys.argv logging_level = logging.INFO @@ -28,34 +33,51 @@ def start(): cs_proc = get_pid() if cs_proc: - print("CloudSync app already running") + logger.info("CloudSync app already running") else: - print("Starting CloudSync app with logging level {0} in {1} mode".format(logging_level,EXEC_MODE)) + logger.info("Starting CloudSync app with logging level %s in %s mode", logging_level, EXEC_MODE) time.sleep(DELAY) Popen(['python3', 'cloud_sync.py', str(logging_level),EXEC_MODE]) def stop(): cs_proc_pid = get_pid() if cs_proc_pid: - print("Stopping CloudSync app...") + logger.info("Stopping CloudSync app...") time.sleep(DELAY) - os.kill(int(cs_proc_pid), signal.SIGKILL) + os.kill(int(cs_proc_pid), signal.SIGTERM) else: - print("CloudSync app is not running.") + logger.info("CloudSync app is not running.") +def monitor(): + """Monitor sentinel file and restart CloudSync when detected.""" + logger.info("Monitoring sentinel file: %s (interval %ds)", SENTINEL_FILE, SENTINEL_CHECK_INTERVAL) + start() + while True: + if os.path.isfile(SENTINEL_FILE): + logger.info("Sentinel file detected — restarting CloudSync") + try: + os.remove(SENTINEL_FILE) + except OSError: + pass + stop() + time.sleep(1) + start() + time.sleep(SENTINEL_CHECK_INTERVAL) + + if len(arguments) <= 1: - print(USAGE_FORMAT) + logger.error(USAGE_FORMAT) sys.exit(101) if len(arguments) >= 4: argument=arguments[3] if argument in ( EXEC_MODE_UPGRADE, EXEC_MODE_UPDATE, EXEC_MODE_NORMAL ): EXEC_MODE=argument else: - print("incorrect {0} argument".format(argument)) - print(USAGE_FORMAT) + logger.error("incorrect %s argument", argument) + logger.error(USAGE_FORMAT) sys.exit(104) if len(arguments) >= 3: @@ -69,8 +91,8 @@ elif argument== "error": logging_level = logging.ERROR else: - print("incorrect {0} argument".format(argument)) - print(USAGE_FORMAT) + logger.error("incorrect %s argument", argument) + logger.error(USAGE_FORMAT) sys.exit(103) if len(arguments) >= 2: @@ -80,17 +102,19 @@ elif argument == "stop": stop() elif argument == "restart": - print("Restarting CloudSync app...") + logger.info("Restarting CloudSync app...") time.sleep(DELAY) stop() start() elif argument == "status": cs_proc = get_pid() if cs_proc: - print("CloudSync app IS running") + logger.info("CloudSync app IS running") else: - print("CloudSync app IS NOT running") + logger.info("CloudSync app IS NOT running") + elif argument == "monitor": + monitor() else: - print("incorrect {0} argument".format(argument)) - print(USAGE_FORMAT) + logger.error("incorrect %s argument", argument) + logger.error(USAGE_FORMAT) sys.exit(102) \ No newline at end of file