]> git.treefish.org Git - photosort.git/commitdiff
implementing daemon
authorAlexander Schmidt <alex@treefish.org>
Mon, 19 Oct 2020 11:43:24 +0000 (13:43 +0200)
committerAlexander Schmidt <alex@treefish.org>
Mon, 19 Oct 2020 11:43:24 +0000 (13:43 +0200)
src/bunch.py [new file with mode: 0644]
src/dirtrigger.py [new file with mode: 0644]
src/photosort-daemon.py [new file with mode: 0755]

diff --git a/src/bunch.py b/src/bunch.py
new file mode 100644 (file)
index 0000000..713a618
--- /dev/null
@@ -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 (file)
index 0000000..f8f67cf
--- /dev/null
@@ -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 (executable)
index 0000000..9bb7213
--- /dev/null
@@ -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()