--- /dev/null
+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
--- /dev/null
+#!/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()