2016-03-06 17:26:07 +00:00
|
|
|
import logging
|
2015-12-20 19:23:33 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
from django.conf import settings
|
2020-11-01 23:07:54 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2020-11-01 23:07:54 +01:00
|
|
|
from watchdog.observers import Observer
|
|
|
|
|
from watchdog.events import FileSystemEventHandler
|
|
|
|
|
|
|
|
|
|
from documents.consumer import Consumer
|
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
|
|
|
|
2016-01-23 02:33:29 +00:00
|
|
|
|
2020-11-01 23:07:54 +01:00
|
|
|
class Handler(FileSystemEventHandler):
|
|
|
|
|
|
|
|
|
|
def __init__(self, consumer):
|
|
|
|
|
self.consumer = consumer
|
|
|
|
|
|
|
|
|
|
def on_created(self, event):
|
|
|
|
|
self.consumer.try_consume_file(event.src_path)
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
consumption directory, and fetch any mail available.
|
2015-12-20 19:23:33 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2016-01-21 12:50:22 -05:00
|
|
|
|
2015-12-20 19:23:33 +00:00
|
|
|
self.verbosity = 0
|
2019-05-06 09:46:04 +02:00
|
|
|
self.logger = logging.getLogger(__name__)
|
2016-01-30 01:18:52 +00:00
|
|
|
|
|
|
|
|
self.file_consumer = None
|
2016-02-06 17:05:36 +00:00
|
|
|
self.mail_fetcher = None
|
2017-05-21 14:23:46 +10:00
|
|
|
self.first_iteration = True
|
2016-01-21 12:50:22 -05:00
|
|
|
|
2020-11-01 23:07:54 +01:00
|
|
|
self.consumer = Consumer()
|
|
|
|
|
|
2015-12-20 19:23:33 +00:00
|
|
|
BaseCommand.__init__(self, *args, **kwargs)
|
|
|
|
|
|
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
|
|
|
)
|
2018-02-24 20:32:19 +01:00
|
|
|
|
2015-12-20 19:23:33 +00:00
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
|
|
|
|
|
self.verbosity = options["verbosity"]
|
2018-02-25 19:20:51 +01:00
|
|
|
directory = options["directory"]
|
2016-01-30 01:18:52 +00:00
|
|
|
|
2020-10-26 00:35:24 +01:00
|
|
|
for d in (settings.ORIGINALS_DIR, settings.THUMBNAIL_DIR):
|
2018-05-28 13:08:00 +01:00
|
|
|
os.makedirs(d, exist_ok=True)
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2016-03-06 17:26:07 +00:00
|
|
|
logging.getLogger(__name__).info(
|
2020-11-01 23:07:54 +01:00
|
|
|
"Starting document consumer at {}".format(
|
|
|
|
|
directory
|
2018-05-11 14:01:21 +02:00
|
|
|
)
|
2016-03-06 17:26:07 +00:00
|
|
|
)
|
|
|
|
|
|
2020-11-01 23:07:54 +01:00
|
|
|
# Consume all files as this is not done initially by the watchdog
|
|
|
|
|
for entry in os.scandir(directory):
|
|
|
|
|
if entry.is_file():
|
|
|
|
|
self.consumer.try_consume_file(entry.path)
|
|
|
|
|
|
|
|
|
|
# Start the watchdog. Woof!
|
|
|
|
|
observer = Observer()
|
|
|
|
|
event_handler = Handler(self.consumer)
|
|
|
|
|
observer.schedule(event_handler, directory, recursive=True)
|
|
|
|
|
observer.start()
|
|
|
|
|
try:
|
|
|
|
|
while observer.is_alive():
|
|
|
|
|
observer.join(1)
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
observer.stop()
|
|
|
|
|
observer.join()
|