diff --git a/atst/queue.py b/atst/queue.py index 8c94bf8f..0b92bf91 100644 --- a/atst/queue.py +++ b/atst/queue.py @@ -1,9 +1,43 @@ from flask_rq2 import RQ from flask import current_app as app -queue = RQ() + +class ATSTQueue(RQ): + + """Internal helpers to get the queue that actually does the work. + + The RQ object always uses the "default" queue, unless we explicitly request + otherwise. These helpers allow us to use `.queue_name` to get the name of + the configured queue and `_queue_job` will use the appropriate queue. + + """ + + @property + def queue_name(self): + return self.queues[0] + + def get_queue(self, name=None): + if not name: + name = self.queue_name + return super().get_queue(name) + + def _queue_job(self, function, *args, **kwargs): + self.get_queue().enqueue(function, *args, **kwargs) + + """Instance methods to queue up application-specific jobs.""" + + def send_mail(self, to, subject, body): + self._queue_job(ATSTQueue._send_mail, to, subject, body) + + """Class methods to actually perform the work. + + Must be a class method (or a module-level function) because we being able + to pickle the class is more effort than its worth. + """ + + @classmethod + def _send_mail(self, to, subject, body): + app.mailer.send(to, subject, body) -@queue.job -def send_mail(to, subject, body): - app.mailer.send(to, subject, body) +queue = ATSTQueue() diff --git a/atst/routes/dev.py b/atst/routes/dev.py index 862d602e..0d90397f 100644 --- a/atst/routes/dev.py +++ b/atst/routes/dev.py @@ -10,7 +10,7 @@ from flask import ( from . import redirect_after_login_url from atst.domain.users import Users -from atst.queue import send_mail +from atst.queue import queue bp = Blueprint("dev", __name__) @@ -78,7 +78,7 @@ def login_dev(): @bp.route("/test-email") def test_email(): - send_mail.queue( + queue.send_mail( [request.args.get("to")], request.args.get("subject"), request.args.get("body") )