Total rewrite to control runaway prog
This commit is contained in:
parent
7e551f8a37
commit
e38c4a236d
2 changed files with 31 additions and 47 deletions
49
src/main.py
49
src/main.py
|
|
@ -3,9 +3,9 @@ import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from threading import Thread
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
from src.watcher import Watcher
|
from src.watcher import Watcher
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
|
@ -73,40 +73,27 @@ def main(
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
threads = [
|
|
||||||
Thread(
|
watchers = [
|
||||||
daemon=True,
|
Watcher(
|
||||||
target=worker,
|
logger,
|
||||||
args=(
|
source.joinpath(subdir).resolve().absolute(),
|
||||||
logger,
|
destination.joinpath(subdir).resolve().absolute(),
|
||||||
source.joinpath(subdir).resolve().absolute(),
|
ignore_pattern,
|
||||||
destination.joinpath(subdir).resolve().absolute(),
|
|
||||||
ignore_pattern,
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
for subdir in subdirs
|
for subdir in subdirs
|
||||||
]
|
]
|
||||||
|
|
||||||
logger.info(f"Starting up {len(threads)} watcher threads")
|
[x.start() for x in watchers]
|
||||||
|
|
||||||
try:
|
while True:
|
||||||
[x.start() for x in threads]
|
try:
|
||||||
logger.info(f"{len(threads)} Threads started!")
|
time.sleep(0.01)
|
||||||
[x.join() for x in threads]
|
except KeyboardInterrupt:
|
||||||
except KeyboardInterrupt:
|
print("Quitting...")
|
||||||
print("Waiting for threads to die...")
|
[x.stop() for x in watchers]
|
||||||
logger.info("Waiting for threads to die...")
|
break
|
||||||
pass # keyboard interrupt should be caught by each thread
|
|
||||||
|
|
||||||
|
|
||||||
def worker(logger, source: Path, destination: Path, ignore_pattern: str) -> None:
|
|
||||||
try:
|
|
||||||
watcher = Watcher(logger, source, destination, ignore_pattern)
|
|
||||||
watcher.start()
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
watcher.stop()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ import os
|
||||||
import shutil
|
import shutil
|
||||||
from logging import Logger
|
from logging import Logger
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from time import sleep
|
|
||||||
|
|
||||||
from watchdog.events import (
|
from watchdog.events import (
|
||||||
FileCreatedEvent,
|
FileCreatedEvent,
|
||||||
|
|
@ -26,7 +25,6 @@ class Watcher:
|
||||||
self.dest = destination.absolute()
|
self.dest = destination.absolute()
|
||||||
self.observer = Observer()
|
self.observer = Observer()
|
||||||
self.handler = Handler(self.logger, self.source, self.dest, ignore_pattern)
|
self.handler = Handler(self.logger, self.source, self.dest, ignore_pattern)
|
||||||
self.running = False
|
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.observer.schedule(
|
self.observer.schedule(
|
||||||
|
|
@ -42,18 +40,17 @@ class Watcher:
|
||||||
)
|
)
|
||||||
self.observer.setDaemon(True)
|
self.observer.setDaemon(True)
|
||||||
self.observer.start()
|
self.observer.start()
|
||||||
self.logger.info(
|
|
||||||
f"[{current_thread().native_id}] The Observer for {self.source} has been started!"
|
self.logger.info(f"The Observer for {self.source} has been started!")
|
||||||
)
|
|
||||||
|
|
||||||
self.running = True
|
|
||||||
while self.running:
|
|
||||||
sleep(0.01)
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.running = False
|
# self.logger.info(f"The Observer for {self.source} has been started!")
|
||||||
|
print(f"Stopping observer for {self.source}")
|
||||||
self.observer.stop()
|
self.observer.stop()
|
||||||
self.observer.join()
|
print(f"Stopped observer for {self.source}")
|
||||||
|
self.observer.join(2)
|
||||||
|
print(f"Joined observer for {self.source}")
|
||||||
|
# self.observer.join(2)
|
||||||
|
|
||||||
|
|
||||||
class Handler(FileSystemEventHandler):
|
class Handler(FileSystemEventHandler):
|
||||||
|
|
@ -73,7 +70,7 @@ class Handler(FileSystemEventHandler):
|
||||||
"""
|
"""
|
||||||
dest_str = f", dest: {event.dest_path}" if event.dest_path else ""
|
dest_str = f", dest: {event.dest_path}" if event.dest_path else ""
|
||||||
self.logger.debug(
|
self.logger.debug(
|
||||||
f"[{current_thread().native_id}]: type: {event.event_type}, src: {event.src_path}{dest_str}"
|
f"type: {event.event_type}, src: {event.src_path}{dest_str}"
|
||||||
)
|
)
|
||||||
|
|
||||||
def __in_destination(self, path: Path) -> Path:
|
def __in_destination(self, path: Path) -> Path:
|
||||||
|
|
@ -132,7 +129,7 @@ class Handler(FileSystemEventHandler):
|
||||||
shutil.copy2(src, self.__in_destination(src))
|
shutil.copy2(src, self.__in_destination(src))
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.logger.warn(
|
self.logger.warn(
|
||||||
f"[{current_thread().native_id}]: Exception thrown in Handler#on_created:\n\t{exc}"
|
f"Exception thrown in Handler#on_created:\n\t{exc}"
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_modified(self, event: FileModifiedEvent) -> None:
|
def on_modified(self, event: FileModifiedEvent) -> None:
|
||||||
|
|
@ -162,7 +159,7 @@ class Handler(FileSystemEventHandler):
|
||||||
shutil.copy2(src, in_dest)
|
shutil.copy2(src, in_dest)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.logger.warn(
|
self.logger.warn(
|
||||||
f"[{current_thread().native_id}]: Exception thrown in Handler#on_modified:\n\t{exc}"
|
f"Exception thrown in Handler#on_modified:\n\t{exc}"
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_moved(self, event: FileSystemMovedEvent):
|
def on_moved(self, event: FileSystemMovedEvent):
|
||||||
|
|
@ -206,7 +203,7 @@ class Handler(FileSystemEventHandler):
|
||||||
self.__recursively_clean_dirs_upwards(in_dest_before.parent.absolute())
|
self.__recursively_clean_dirs_upwards(in_dest_before.parent.absolute())
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.logger.warn(
|
self.logger.warn(
|
||||||
f"[{current_thread().native_id}]: Exception thrown in Handler#on_moved:\n\t{exc}"
|
f"Exception thrown in Handler#on_moved:\n\t{exc}"
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_deleted(self, event: FileDeletedEvent) -> None:
|
def on_deleted(self, event: FileDeletedEvent) -> None:
|
||||||
|
|
@ -232,5 +229,5 @@ class Handler(FileSystemEventHandler):
|
||||||
self.__recursively_clean_dirs_upwards(in_dest.parent.absolute())
|
self.__recursively_clean_dirs_upwards(in_dest.parent.absolute())
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.logger.warn(
|
self.logger.warn(
|
||||||
f"[{current_thread().native_id}]: Exception thrown in Handler#on_deleted:\n\t{exc}"
|
f"Exception thrown in Handler#on_deleted:\n\t{exc}"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue