6 from dirtrigger import DirTrigger
7 from migrator import Migrator
10 class TriggeredSource:
11 def __init__(self, idx, trigger, migrator, cleanup):
13 self.trigger = trigger
14 self.migrator = migrator
15 self.cleanup = cleanup
17 def __init__(self, idx, cache_dir, cfg):
21 for src_dir_cfg in cfg['src_dirs']:
23 Bunch.TriggeredSource(
25 DirTrigger(src_dir_cfg['path'], src_dir_cfg['cool_time'],
26 src_dir_cfg['max_time']),
27 Migrator(src_dir_cfg['path'], cfg['dst_dir']['path'],
28 os.path.join(cache_dir, "%d.db" % idx)),
29 src_dir_cfg['cleanup']
33 logging.info("Created bunch #%d with %d sources.", self._idx, len(self._sources))
36 logging.info("Starting bunch #%d...", self._idx)
38 self._worker_thread = threading.Thread(target=self._worker)
39 self._worker_thread.start()
42 logging.info("Stopping bunch #%d...", self._idx)
44 self._worker_thread.join()
45 for src in self._sources:
47 logging.info("Stopped bunch #%d.", self._idx)
50 return self._worker_thread.is_alive()
53 for src in self._sources:
57 for src in self._sources:
59 if src.trigger.is_triggering():
60 logging.info("Got trigger for source #%d.%d.", self._idx, src.idx)
63 src.migrator.migrate(src.cleanup)
65 src.migrator.cleanup()
66 logging.info("Migration took %.1fs for source #%d.%d.",
67 time.time() - before, self._idx, src.idx)
68 except Exception as e:
69 logging.error("Error migrating source #%d.%d: %s",
70 self._idx, src.idx, str(e))
73 for src in self._sources: