From 9ea1074b03c2e49106a882586ae6f74c97a7fbba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 4 Mar 2019 17:29:00 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Process=20watched=20folder=20on=20s?= =?UTF-8?q?tartup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements #2462 --- src/octoprint/server/__init__.py | 8 +++- src/octoprint/server/util/watchdog.py | 57 ++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index f64eec2f5..35efadda6 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -712,13 +712,17 @@ class Server(object): self._logger.exception("Something went wrong while attempting to automatically connect to the printer") # start up watchdogs + watched = self._settings.getBaseFolder("watched") + watchdog_handler = util.watchdog.GcodeWatchdogHandler(fileManager, printer) + watchdog_handler.initial_scan(watched) + if self._settings.getBoolean(["feature", "pollWatched"]): - # use less performant polling observer if explicitely configured + # use less performant polling observer if explicitly configured observer = PollingObserver() else: # use os default observer = Observer() - observer.schedule(util.watchdog.GcodeWatchdogHandler(fileManager, printer), self._settings.getBaseFolder("watched")) + observer.schedule(watchdog_handler, watched) observer.start() # run our startup plugins diff --git a/src/octoprint/server/util/watchdog.py b/src/octoprint/server/util/watchdog.py index 1db1299e7..8200783db 100644 --- a/src/octoprint/server/util/watchdog.py +++ b/src/octoprint/server/util/watchdog.py @@ -7,6 +7,7 @@ __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms import logging import os +import shutil import time import threading import watchdog.events @@ -30,7 +31,39 @@ class GcodeWatchdogHandler(watchdog.events.PatternMatchingEventHandler): self._file_manager = file_manager self._printer = printer + def initial_scan(self, folder): + def run_scan(): + try: + from os import scandir + except ImportError: + from scandir import scandir + + self._logger.info("Running initial scan on watched folder...") + for entry in scandir(folder): + path = entry.path + + if not self._valid_path(path): + continue + + self._logger.info("Found {}, trying to add it".format(path)) + self._upload(path) + self._logger.info("... initial scan done.") + + thread = threading.Thread(target=run_scan) + thread.daemon = True + thread.start() + + def on_created(self, event): + path = event.src_path + if not self._valid_path(path): + return + + thread = threading.Thread(target=self._repeatedly_check, args=(path,)) + thread.daemon = True + thread.start() + def _upload(self, path): + # noinspection PyBroadException try: file_wrapper = octoprint.filemanager.util.DiskFileWrapper(os.path.basename(path), path) @@ -67,17 +100,17 @@ class GcodeWatchdogHandler(watchdog.events.PatternMatchingEventHandler): self._printer.select_file(self._file_manager.path_on_disk(octoprint.filemanager.FileDestinations.LOCAL, added_file), False) - except: + except Exception: self._logger.exception("There was an error while processing the file {} in the watched folder".format(path)) - - def on_created(self, event): - path = event.src_path - if octoprint.util.is_hidden_path(path): - return - - thread = threading.Thread(target=self._repeatedly_check, args=(path,)) - thread.daemon = True - thread.start() + finally: + if os.path.exists(path): + # file is still there - that should only happen if something went wrong, so mark it as failed + # noinspection PyBroadException + try: + shutil.move(path, "{}.failed".format(path)) + except: + # something went really wrong here.... but we can't do anything about it, so just log it + self._logger.exception("There was an error while trying to mark {} as failed in the watched folder".format(path)) def _repeatedly_check(self, path, interval=1, stable=5): try: @@ -107,3 +140,7 @@ class GcodeWatchdogHandler(watchdog.events.PatternMatchingEventHandler): self._logger.debug("File at {} is stable, moving it".format(path)) self._upload(path) + + def _valid_path(self, path): + _, ext = os.path.splitext(path) + return octoprint.filemanager.valid_file_type(path) and not octoprint.util.is_hidden_path(path) and ext != "failed"