2015-12-26 13:21:33 +00:00
|
|
|
import datetime
|
2016-03-06 17:26:07 +00:00
|
|
|
import logging
|
2015-12-20 19:23:33 +00:00
|
|
|
import os
|
2015-12-21 02:44:24 +00:00
|
|
|
import time
|
2015-12-20 19:23:33 +00:00
|
|
|
|
|
|
|
|
from django.conf import settings
|
2016-01-14 19:47:57 +00:00
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2016-02-06 17:05:36 +00:00
|
|
|
from ...consumer import Consumer, ConsumerError
|
|
|
|
|
from ...mail import MailFetcher, MailFetcherError
|
2016-01-23 02:33:29 +00: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
|
|
|
|
|
consumption directory, and fetch any mail available.
|
2015-12-20 19:23:33 +00:00
|
|
|
"""
|
|
|
|
|
|
2017-01-01 18:41:06 +00:00
|
|
|
LOOP_TIME = settings.CONSUMER_LOOP_TIME
|
2016-01-30 01:18:52 +00:00
|
|
|
MAIL_DELTA = datetime.timedelta(minutes=10)
|
2015-12-26 13:21:33 +00:00
|
|
|
|
2017-01-01 18:39:34 +00:00
|
|
|
ORIGINAL_DOCS = os.path.join(settings.MEDIA_ROOT, "documents", "originals")
|
|
|
|
|
THUMB_DOCS = os.path.join(settings.MEDIA_ROOT, "documents", "thumbnails")
|
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
|
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
|
|
|
|
2015-12-20 19:23:33 +00:00
|
|
|
BaseCommand.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
|
|
|
|
|
self.verbosity = options["verbosity"]
|
|
|
|
|
|
2016-01-30 01:18:52 +00:00
|
|
|
try:
|
2016-02-28 00:39:40 +00:00
|
|
|
self.file_consumer = Consumer()
|
2016-02-06 17:05:36 +00:00
|
|
|
self.mail_fetcher = MailFetcher()
|
|
|
|
|
except (ConsumerError, MailFetcherError) as e:
|
2016-01-30 01:18:52 +00:00
|
|
|
raise CommandError(e)
|
|
|
|
|
|
2017-01-01 18:39:34 +00:00
|
|
|
for path in (self.ORIGINAL_DOCS, self.THUMB_DOCS):
|
|
|
|
|
try:
|
|
|
|
|
os.makedirs(path)
|
|
|
|
|
except FileExistsError:
|
|
|
|
|
pass
|
2015-12-20 19:23:33 +00:00
|
|
|
|
2016-03-06 17:26:07 +00:00
|
|
|
logging.getLogger(__name__).info(
|
2016-03-28 11:11:15 +01:00
|
|
|
"Starting document consumer at {}".format(settings.CONSUMPTION_DIR)
|
2016-03-06 17:26:07 +00:00
|
|
|
)
|
|
|
|
|
|
2015-12-21 02:44:24 +00:00
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
self.loop()
|
2015-12-26 13:21:33 +00:00
|
|
|
time.sleep(self.LOOP_TIME)
|
|
|
|
|
if self.verbosity > 1:
|
|
|
|
|
print(".")
|
2015-12-21 02:44:24 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
print("Exiting")
|
|
|
|
|
|
|
|
|
|
def loop(self):
|
|
|
|
|
|
2016-02-06 17:05:36 +00:00
|
|
|
# Consume whatever files we can
|
2016-01-30 01:18:52 +00:00
|
|
|
self.file_consumer.consume()
|
2016-01-01 16:13:59 +00:00
|
|
|
|
2016-02-06 17:05:36 +00:00
|
|
|
# Occasionally fetch mail and store it to be consumed on the next loop
|
2017-05-21 14:23:46 +10:00
|
|
|
# We fetch email when we first start up so that it is not necessary to
|
|
|
|
|
# wait for 10 minutes after making changes to the config file.
|
2016-02-06 17:05:36 +00:00
|
|
|
delta = self.mail_fetcher.last_checked + self.MAIL_DELTA
|
2017-05-21 14:23:46 +10:00
|
|
|
if self.first_iteration or delta < datetime.datetime.now():
|
|
|
|
|
self.first_iteration = False
|
2016-02-06 17:05:36 +00:00
|
|
|
self.mail_fetcher.pull()
|