From ef745f5c8e8191f8ff341cb6fcf39179e34eb2bd Mon Sep 17 00:00:00 2001 From: Alexander Schmidt Date: Mon, 19 Oct 2020 13:43:24 +0200 Subject: [PATCH] implementing daemon --- src/bunch.py | 57 +++++++++++++++++++++++++++++++++++++++++ src/dirtrigger.py | 6 +++++ src/photosort-daemon.py | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 src/bunch.py create mode 100644 src/dirtrigger.py create mode 100755 src/photosort-daemon.py diff --git a/src/bunch.py b/src/bunch.py new file mode 100644 index 0000000..713a618 --- /dev/null +++ b/src/bunch.py @@ -0,0 +1,57 @@ +import logging +import time +import threading + +from dirtrigger import DirTrigger +from migrator import Migrator + +class Bunch: + class TriggeredSource: + def __init__(self, trigger, migrator, cleanup): + self.trigger = trigger + self.migrator = migrator + self.cleanup = cleanup + + def __init__(self, cfg): + self._id = cfg['id'] + self._sources = [] + for src_dir_cfg in cfg['src_dirs']: + self._sources.append( + Bunch.TriggeredSource( + DirTrigger(src_dir_cfg['path']), + Migrator(src_dir_cfg['path'], cfg['dst_dir']['path']), + src_dir_cfg['cleanup'] + ) + ) + logging.info("Created bunch %s with %d sources.", self._id, len(self._sources)) + + def start(self): + logging.info("Starting bunch %s...", self._id) + self._stop = False + self._worker_thread = threading.Thread(target=self._worker) + self._worker_thread.start() + + def stop(self): + logging.info("Stopping bunch %s...", self._id) + self._stop = True + self._worker_thread.join() + logging.info("Stopped bunch %s.", self._id) + + def _worker(self): + # start triggers + + while not self._stop: + for source in self._sources: + try: + if source.trigger.is_triggering(): + logging.info("Got source trigger for bunch %s.", self._id) + source.trigger.reset() + source.migrator.migrate() + if source.cleanup: + source.migrator.cleanup() + except Exception as e: + logging.error("Error migrating source for bunch %s: %s", + self._id, str(e)) + time.sleep(10.0) + + # stop triggers diff --git a/src/dirtrigger.py b/src/dirtrigger.py new file mode 100644 index 0000000..f8f67cf --- /dev/null +++ b/src/dirtrigger.py @@ -0,0 +1,6 @@ +class DirTrigger: + def __init__(self, dir_path): + pass + + def is_triggering(self): + return False diff --git a/src/photosort-daemon.py b/src/photosort-daemon.py new file mode 100755 index 0000000..9bb7213 --- /dev/null +++ b/src/photosort-daemon.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 + +import argparse +import json +import logging +import os +import signal +import time + +from bunch import Bunch + +def handle_interrupt(sig, frame): + global stop + if os.getpid() == main_pid: + logging.info( "Got stop signal." ) + stop = True + +main_pid = os.getpid() +signal.signal(signal.SIGINT, handle_interrupt) +stop = False + +parser = argparse.ArgumentParser(description='Photo Sort Daemon.') +parser.add_argument('config_file', type=str, help='path to config file') +parser.add_argument('-l', '--log-level', type=str, default='INFO', dest='log_lvl', + choices=['DEBUG', 'INFO', 'WARNING'], help='select log level') + +args = parser.parse_args() + +logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s', + level=logging.getLevelName(args.log_lvl), + datefmt='%m/%d/%Y %H:%M:%S') + +with open(args.config_file) as f: + cfg = json.load(f) + +bunches = [] +for bunch_cfg in cfg['bunches']: + bunches.append( Bunch(bunch_cfg) ) + + +for bunch in bunches: + bunch.start() + +while not stop: + time.sleep(2.0) + +for bunch in bunches: + bunch.stop() -- 2.39.5