Add support for using polling to detect changes.

Closes #4
This commit is contained in:
David Coppit
2018-08-17 21:53:30 -04:00
parent e65260ff52
commit f1d26df840
3 changed files with 23 additions and 1 deletions

View File

@@ -56,6 +56,7 @@ This example is to run a permissions-repairing utility whenever there's a change
UMASK=0000 UMASK=0000
# This is important because chmod/chown will change files in the monitored directory # This is important because chmod/chown will change files in the monitored directory
IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=1 IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=1
USE_POLLING=no
Since the `newperms` utility does an explicit "chown -R nobody:users", we need to use the UMAP and GMAP environment variables to update the user and group in the container so that it will match the host. For example: Since the `newperms` utility does an explicit "chown -R nobody:users", we need to use the UMAP and GMAP environment variables to update the user and group in the container so that it will match the host. For example:
@@ -73,5 +74,6 @@ This example tells SageTV to rescan its imported media when the media directory
GROUP_ID=0 GROUP_ID=0
UMASK=0000 UMASK=0000
IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=0 IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=0
USE_POLLING=no
We don't need to ignore events while the command is running because the wget command is a "fire and forget" asynchronous operation. We also don't need to use UMAP or GMAP. We don't need to ignore events while the command is running because the wget command is a "fire and forget" asynchronous operation. We also don't need to use UMAP or GMAP.

View File

@@ -10,6 +10,7 @@ import sys
import tempfile import tempfile
import time import time
from watchdog.observers import Observer from watchdog.observers import Observer
from watchdog.observers.polling import PollingObserver
from watchdog.events import FileSystemEventHandler from watchdog.events import FileSystemEventHandler
RUNAS = "/files/runas.sh" RUNAS = "/files/runas.sh"
@@ -124,6 +125,15 @@ def read_config(config_file):
sys.exit(1) sys.exit(1)
args.ignore_events_while_command_is_running = env["IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING"] == "1" args.ignore_events_while_command_is_running = env["IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING"] == "1"
if "USE_POLLING" in env:
if not re.match("(yes|no|true|false|0|1)", env["USE_POLLING"], re.IGNORECASE):
logging.error("Configuration error. USE_POLLING must be \"yes\" or \"no\".")
sys.exit(1)
args.use_polling = True if re.match("(yes|true|1)", env["USE_POLLING"], re.IGNORECASE) else False
else:
args.use_polling = False
logging.info("CONFIGURATION:") logging.info("CONFIGURATION:")
logging.info(" WATCH_DIR=%s", args.watch_dir) logging.info(" WATCH_DIR=%s", args.watch_dir)
logging.info("SETTLE_DURATION=%s", args.settle_duration) logging.info("SETTLE_DURATION=%s", args.settle_duration)
@@ -134,6 +144,7 @@ def read_config(config_file):
logging.info(" GROUP_ID=%s", args.group_id) logging.info(" GROUP_ID=%s", args.group_id)
logging.info(" UMASK=%s", args.umask) logging.info(" UMASK=%s", args.umask)
logging.info(" DEBUG=%s", args.debug) logging.info(" DEBUG=%s", args.debug)
logging.info(" USE_POLLING=%s", args.use_polling)
logging.info("IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=%s", args.ignore_events_while_command_is_running) logging.info("IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=%s", args.ignore_events_while_command_is_running)
return args return args
@@ -258,8 +269,14 @@ if args.debug:
logging.info("Starting monitor for %s", name) logging.info("Starting monitor for %s", name)
# Launch the watchdog # Launch the watchdog
if args.use_polling:
logging.info("Using polling to detect changes")
observer = PollingObserver()
else:
logging.info("Using native change detection to detect changes")
observer = Observer()
event_handler = ModifyHandler() event_handler = ModifyHandler()
observer = Observer()
observer.schedule(event_handler, args.watch_dir, recursive=True) observer.schedule(event_handler, args.watch_dir, recursive=True)
observer.start() observer.start()

View File

@@ -39,5 +39,8 @@ UMASK=0
# file even if it already has that mode. # file even if it already has that mode.
IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=1 IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=1
# Set to true to use polling to detect changes. For non-linux file systems, such as Windows shares, you must use polling.
USE_POLLING=no
# Set this to 1 to log all events, for debugging purposes. WARNING! This creates copious amounts of confusing logging! # Set this to 1 to log all events, for debugging purposes. WARNING! This creates copious amounts of confusing logging!
DEBUG=0 DEBUG=0