2016-03-06 17:26:07 +00:00
|
|
|
import logging
|
2015-12-20 19:23:33 +00:00
|
|
|
import os
|
2020-11-26 17:41:50 +01:00
|
|
|
from time import sleep
|
2015-12-20 19:23:33 +00:00
|
|
|
|
|
|
|
|
from django.conf import settings
|
2020-11-27 13:12:13 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2020-11-16 18:26:54 +01:00
|
|
|
from django_q.tasks import async_task
|
2020-11-01 23:07:54 +01:00
|
|
|
from watchdog.events import FileSystemEventHandler
|
2020-11-16 18:52:13 +01:00
|
|
|
from watchdog.observers.polling import PollingObserver
|
2016-01-23 02:33:29 +00:00
|
|
|
|
2018-05-11 14:01:21 +02:00
|
|
|
try:
|
|
|
|
|
from inotify_simple import INotify, flags
|
|
|
|
|
except ImportError:
|
2018-09-02 20:33:49 +01:00
|
|
|
INotify = flags = None
|
2018-05-11 14:01:21 +02:00
|
|
|
|
2020-11-26 17:41:50 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2016-01-23 02:33:29 +00:00
|
|
|
|
2020-11-01 23:07:54 +01:00
|
|
|
|
2020-11-26 17:41:50 +01:00
|
|
|
def _consume(file):
|
|
|
|
|
try:
|
2020-11-12 09:30:04 +01:00
|
|
|
if os.path.isfile(file):
|
2020-11-26 17:41:50 +01:00
|
|
|
async_task("documents.tasks.consume_file",
|
|
|
|
|
file,
|
|
|
|
|
task_name=os.path.basename(file)[:100])
|
|
|
|
|
else:
|
|
|
|
|
logger.debug(
|
|
|
|
|
f"Not consuming file {file}: File has moved.")
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
# Catch all so that the consumer won't crash.
|
|
|
|
|
# This is also what the test case is listening for to check for
|
|
|
|
|
# errors.
|
|
|
|
|
logger.error(
|
|
|
|
|
"Error while consuming document: {}".format(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _consume_wait_unmodified(file, num_tries=20, wait_time=1):
|
|
|
|
|
mtime = -1
|
|
|
|
|
current_try = 0
|
|
|
|
|
while current_try < num_tries:
|
|
|
|
|
try:
|
|
|
|
|
new_mtime = os.stat(file).st_mtime
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
logger.debug(f"File {file} moved while waiting for it to remain "
|
|
|
|
|
f"unmodified.")
|
|
|
|
|
return
|
|
|
|
|
if new_mtime == mtime:
|
|
|
|
|
_consume(file)
|
|
|
|
|
return
|
|
|
|
|
mtime = new_mtime
|
|
|
|
|
sleep(wait_time)
|
|
|
|
|
current_try += 1
|
|
|
|
|
|
|
|
|
|
logger.error(f"Timeout while waiting on file {file} to remain unmodified.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Handler(FileSystemEventHandler):
|
2020-11-12 09:30:04 +01:00
|
|
|
|
2020-11-01 23:07:54 +01:00
|
|
|
def on_created(self, event):
|
2020-11-26 17:41:50 +01:00
|
|
|
_consume_wait_unmodified(event.src_path)
|
2020-11-12 09:30:04 +01:00
|
|
|
|
|
|
|
|
def on_moved(self, event):
|
2020-11-26 17:41:50 +01:00
|
|
|
_consume_wait_unmodified(event.dest_path)
|
2020-11-01 23:07:54 +01:00
|
|
|
|
|
|
|
|
|
2016-02-14 16:09:52 +00:00
|
|
|
class Command(BaseCommand):
|
2015-12-20 19:23:33 +00:00
|
|
|
"""
|
2016-02-06 17:05:36 +00:00
|
|
|
On every iteration of an infinite loop, consume what we can from the
|
2020-11-15 23:56:08 +01:00
|
|
|
consumption directory.
|
2015-12-20 19:23:33 +00:00
|
|
|
"""
|
|
|
|
|
|
2020-11-26 17:41:50 +01:00
|
|
|
# This is here primarily for the tests and is irrelevant in production.
|
|
|
|
|
stop_flag = False
|
|
|
|
|
|
2015-12-20 19:23:33 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2016-01-21 12:50:22 -05:00
|
|
|
|
2019-05-06 09:46:04 +02:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2016-01-30 01:18:52 +00:00
|
|
|
|
2015-12-20 19:23:33 +00:00
|
|
|
BaseCommand.__init__(self, *args, **kwargs)
|
2020-11-26 17:41:50 +01:00
|
|
|
self.observer = None
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2018-02-24 20:32:19 +01:00
|
|
|
def add_arguments(self, parser):
|
2018-02-25 19:20:51 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
"directory",
|
|
|
|
|
default=settings.CONSUMPTION_DIR,
|
2018-02-26 18:52:46 +01:00
|
|
|
nargs="?",
|
|
|
|
|
help="The consumption directory."
|
2018-02-25 19:20:51 +01:00
|
|
|
)
|
2020-11-26 17:41:50 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--oneshot",
|
|
|
|
|
action="store_true",
|
|
|
|
|
help="Run only once."
|
|
|
|
|
)
|
2018-02-24 20:32:19 +01:00
|
|
|
|
2015-12-20 19:23:33 +00:00
|
|
|
def handle(self, *args, **options):
|
2018-02-25 19:20:51 +01:00
|
|
|
directory = options["directory"]
|
2016-01-30 01:18:52 +00:00
|
|
|
|
2020-11-27 13:12:13 +01:00
|
|
|
if not directory:
|
|
|
|
|
raise CommandError(
|
|
|
|
|
"CONSUMPTION_DIR does not appear to be set."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not os.path.isdir(directory):
|
|
|
|
|
raise CommandError(
|
|
|
|
|
f"Consumption directory {directory} does not exist")
|
|
|
|
|
|
2020-11-01 23:07:54 +01:00
|
|
|
for entry in os.scandir(directory):
|
2020-11-26 18:55:05 +01:00
|
|
|
_consume(entry.path)
|
2020-11-01 23:07:54 +01:00
|
|
|
|
2020-11-26 17:41:50 +01:00
|
|
|
if options["oneshot"]:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if settings.CONSUMER_POLLING == 0 and INotify:
|
|
|
|
|
self.handle_inotify(directory)
|
2020-11-16 18:52:13 +01:00
|
|
|
else:
|
2020-11-26 17:41:50 +01:00
|
|
|
self.handle_polling(directory)
|
|
|
|
|
|
|
|
|
|
logger.debug("Consumer exiting.")
|
|
|
|
|
|
|
|
|
|
def handle_polling(self, directory):
|
|
|
|
|
logging.getLogger(__name__).info(
|
|
|
|
|
f"Polling directory for changes: {directory}")
|
|
|
|
|
self.observer = PollingObserver(timeout=settings.CONSUMER_POLLING)
|
|
|
|
|
self.observer.schedule(Handler(), directory, recursive=False)
|
|
|
|
|
self.observer.start()
|
|
|
|
|
try:
|
|
|
|
|
while self.observer.is_alive():
|
|
|
|
|
self.observer.join(1)
|
|
|
|
|
if self.stop_flag:
|
|
|
|
|
self.observer.stop()
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
self.observer.stop()
|
|
|
|
|
self.observer.join()
|
|
|
|
|
|
|
|
|
|
def handle_inotify(self, directory):
|
|
|
|
|
logging.getLogger(__name__).info(
|
|
|
|
|
f"Using inotify to watch directory for changes: {directory}")
|
|
|
|
|
|
|
|
|
|
inotify = INotify()
|
2020-11-27 13:19:58 +01:00
|
|
|
descriptor = inotify.add_watch(
|
|
|
|
|
directory, flags.CLOSE_WRITE | flags.MOVED_TO)
|
2020-11-01 23:07:54 +01:00
|
|
|
try:
|
2020-11-26 17:41:50 +01:00
|
|
|
while not self.stop_flag:
|
|
|
|
|
for event in inotify.read(timeout=1000, read_delay=1000):
|
|
|
|
|
file = os.path.join(directory, event.name)
|
2020-11-27 13:12:34 +01:00
|
|
|
_consume(file)
|
2020-11-01 23:07:54 +01:00
|
|
|
except KeyboardInterrupt:
|
2020-11-26 17:41:50 +01:00
|
|
|
pass
|
2020-11-27 13:12:34 +01:00
|
|
|
|
|
|
|
|
inotify.rm_watch(descriptor)
|
2020-11-27 13:19:58 +01:00
|
|
|
inotify.close()
|