coins-demo/todo/management/commands/mail_worker.py
multun c7ad961ef3 Implement mail tracker system
* Implement mail tracking

Signed-off-by: Victor "multun" Collod <victor.collod@prologin.org>

* Implement task merging

* Add a mail tracker title format pattern

* Autocomplete task names

* Fix comment display

* Track notification answers

* Add a socket timeout for the mail worker

A mail worker is a long running application. And sometimes, the IMAP server
just hangs for hours for no apparent reason. imaplib doesn't enable setting
a timeout, and setting it globally seems fine.

* Only validate the merge form when submitted

* Redirect to the new form when merging

* Prettier task edit UI

* Make task merging optional

* Test mail tracking

* Update documentation for mail tracking

* Update dependencies

* Add the TODO_COMMENT_CLASSES setting

* Fix dependencies install order

* Remove debug leftovers, improve documentation

* Fail on missing from_address
2019-03-11 00:04:19 -07:00

44 lines
1.2 KiB
Python

import logging
import socket
import sys
from django.core.management.base import BaseCommand
from django.conf import settings
logger = logging.getLogger(__name__)
DEFAULT_IMAP_TIMEOUT = 20
class Command(BaseCommand):
help = "Starts a mail worker"
def add_arguments(self, parser):
parser.add_argument("--imap_timeout", type=int, default=30)
parser.add_argument("worker_name")
def handle(self, *args, **options):
if not hasattr(settings, "TODO_MAIL_TRACKERS"):
logger.error("missing TODO_MAIL_TRACKERS setting")
sys.exit(1)
worker_name = options["worker_name"]
tracker = settings.TODO_MAIL_TRACKERS.get(worker_name, None)
if tracker is None:
logger.error(
"couldn't find configuration for %r in TODO_MAIL_TRACKERS",
worker_name
)
sys.exit(1)
# set the default socket timeout (imaplib doesn't enable configuring it)
timeout = options["imap_timeout"]
if timeout:
socket.setdefaulttimeout(timeout)
# run the mail polling loop
producer = tracker["producer"]
consumer = tracker["consumer"]
consumer(producer())