]> git.treefish.org Git - photosort.git/commitdiff
refactoring
authorAlexander Schmidt <alex@treefish.org>
Mon, 19 Oct 2020 17:37:44 +0000 (19:37 +0200)
committerAlexander Schmidt <alex@treefish.org>
Mon, 19 Oct 2020 17:37:44 +0000 (19:37 +0200)
doc/photosort-config.json
src/bunch.py
src/photosort-daemon.py

index 63683a4753d1b8adad5a8ecb0ee27578c0e50024..edd4a51581a4b0e5aa77a01c7cdbb378ee52780d 100644 (file)
@@ -1,7 +1,6 @@
 {
     "bunches": [
         {
-            "id": "mbk",
             "dst_dir": {
                 "path": "/var/local/photosort/sorted/mbk"
             },
index 2ac33ca7547521737b47d2c0bf17944063a2ed0e..228131a9e1ec5e6b8157f86813b9fbffceb15634 100644 (file)
@@ -7,56 +7,64 @@ from migrator import Migrator
 
 class Bunch:
     class TriggeredSource:
-        def __init__(self, trigger, migrator, cleanup):
+        def __init__(self, idx, trigger, migrator, cleanup):
+            self.idx = idx
             self.trigger = trigger
             self.migrator = migrator
             self.cleanup = cleanup
 
-    def __init__(self, cfg):
-        self._id = cfg['id']
+    def __init__(self, idx, cfg):
+        self._idx = idx
+        source_idx = 1
         self._sources = []
         for src_dir_cfg in cfg['src_dirs']:
             self._sources.append(
                 Bunch.TriggeredSource(
-                    DirTrigger(src_dir_cfg['path'], src_dir_cfg['cool_time'], src_dir_cfg['max_time']),
+                    source_idx,
+                    DirTrigger(src_dir_cfg['path'], src_dir_cfg['cool_time'],
+                               src_dir_cfg['max_time']),
                     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))
+            source_idx += 1
+        logging.info("Created bunch #%d with %d sources.", self._idx, len(self._sources))
 
     def start(self):
-        logging.info("Starting bunch %s...", self._id)
+        logging.info("Starting bunch #%d...", self._idx)
         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)
+        logging.info("Stopping bunch #%d...", self._idx)
         self._stop = True
         self._worker_thread.join()
-        logging.info("Stopped bunch %s.", self._id)
+        logging.info("Stopped bunch #%d.", self._idx)
 
     def is_running(self):
         return self._worker_thread.is_alive()
 
     def _worker(self):
-        for source in self._sources:
-            source.trigger.start()
+        for src in self._sources:
+            src.trigger.start()
 
         while not self._stop:
-            for source in self._sources:
+            for src 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(source.cleanup)
-                        if source.cleanup:
-                            source.migrator.cleanup()
+                    if src.trigger.is_triggering():
+                        logging.info("Got trigger for source #%d.%d.", self._idx, src.idx)
+                        before = time.time()
+                        src.trigger.reset()
+                        src.migrator.migrate(src.cleanup)
+                        if src.cleanup:
+                            src.migrator.cleanup()
+                        logging.info("Migration took %.1fs for source #%d.%d.",
+                                     time.time() - before, self._idx, src.idx)
                 except Exception as e:
-                    logging.error("Error migrating source for bunch %s: %s",
-                                  self._id, str(e))
+                    logging.error("Error migrating source #%d.%d: %s",
+                                  self._idx, src.idx, str(e))
             time.sleep(10.0)
 
-        for source in self._sources:
-            source.trigger.stop()
+        for src in self._sources:
+            src.trigger.stop()
index 7e91b32f2490787a280e56319f3bdcaba586a68b..e345b837188b3fe5fed584630753c530a3a8bc59 100755 (executable)
@@ -36,9 +36,11 @@ status = 0
 with open(args.config_file) as f:
     cfg = json.load(f)
 
+bunch_idx = 1
 bunches = []
 for bunch_cfg in cfg['bunches']:
-    bunches.append( Bunch(bunch_cfg) )
+    bunches.append( Bunch(bunch_idx, bunch_cfg) )
+    bunch_idx += 1
 
 for bunch in bunches:
     bunch.start()