aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2024-03-01 15:00:01 +0200
committerJan Tuomi <jan@jantuomi.fi>2024-12-21 19:43:13 +0200
commit6c2c945d192c707293aedba29ac94b8b677f7b3e (patch)
tree752e90f05f81aef5cdd971230a026625eb2948ea
parentf778686fece43cad8c2b08293e505ceeebe1890a (diff)
Send emails in a background thread
-rw-r--r--app.py29
-rw-r--r--email_client.py64
2 files changed, 59 insertions, 34 deletions
diff --git a/app.py b/app.py
index f8422a9..6b49760 100644
--- a/app.py
+++ b/app.py
@@ -6,6 +6,10 @@ import os
import sys
import traceback
import uuid
+import threading
+import queue
+import time
+from typing import Callable
from user_agents import parse as parse_user_agent
from dataclasses import dataclass
from flask import Flask, render_template, redirect, request, make_response
@@ -30,6 +34,9 @@ AUTHOR_NAME_MAX_LENGTH = 100
AUTHOR_EMAIL_MAX_LENGTH = 100
VOTER_NAME_MAX_LENGTH = 100
+Task = Callable[[], None]
+background_tasks_queue: queue.Queue[Task] = queue.Queue()
+
def voter_selection_on_choice(voter_name: str, choice: db.Choice) -> int | None:
for vote in choice.votes:
if vote.voter_name == voter_name:
@@ -95,7 +102,11 @@ def create():
choices,
)
- email_client.send_poll_created_email_if_enabled(poll_id=poll.id)
+ if email_client.email_enabled:
+ def task():
+ email_client.send_poll_created_email(poll_id=poll.id)
+
+ background_tasks_queue.put(task)
resp = make_response(
redirect(f"/manage/{poll.manage_code}")
@@ -190,7 +201,10 @@ def vote_poll(id):
manage_code = db.vote_poll(id, voter_name, selections)
- email_client.send_participation_email_if_enabled(poll_id=id, voter_name=voter_name)
+ if email_client.email_enabled:
+ def task():
+ email_client.send_participation_email(poll_id=id, voter_name=voter_name)
+ background_tasks_queue.put(task)
response = make_response(
redirect(f"/poll/{id}")
@@ -346,3 +360,14 @@ def toggle_display_mode():
resp.set_cookie("diddle_display_mode", display_mode,
samesite="Lax", secure=False)
return resp
+
+### Background thread
+
+def background_thread():
+ print("Background thread started")
+ while True:
+ task = background_tasks_queue.get()
+ task()
+ time.sleep(0.1)
+
+threading.Thread(target=background_thread, daemon=True).start()
diff --git a/email_client.py b/email_client.py
index acb6c9c..e06c901 100644
--- a/email_client.py
+++ b/email_client.py
@@ -67,38 +67,38 @@ if email_enabled:
else:
print("SMTP email client not enabled")
-def send_participation_email_if_enabled(poll_id: str, voter_name: str):
- if email_enabled:
- poll = db.get_poll(poll_id)
- if not poll or poll.author_email is None:
- return
+def send_participation_email(poll_id: str, voter_name: str):
+ poll = db.get_poll(poll_id)
+ if not poll or poll.author_email is None:
+ return
- try:
- send_email(
- subject=f"{voter_name} participated in your poll \"{poll.title}\"",
- body=f"{voter_name} participated in your diddle \"{poll.title}\".\n\n"
- f"View the results at {BASE_URL}/poll/{poll.id}\n"
- f"Manage your diddle at {BASE_URL}/manage/{poll.manage_code}\n"
- "You will be notified by email when someone participates.",
- recipient=poll.author_email,
- )
- except Exception as e:
- traceback.print_exc(file=sys.stderr)
- print(f"Failed to send participation email to {poll.author_email}", file=sys.stderr)
+ print(f"Sending participation email to {poll.author_email}")
+ try:
+ send_email(
+ subject=f"{voter_name} participated in your poll \"{poll.title}\"",
+ body=f"{voter_name} participated in your diddle \"{poll.title}\".\n\n"
+ f"View the results at {BASE_URL}/poll/{poll.id}\n"
+ f"Manage your diddle at {BASE_URL}/manage/{poll.manage_code}\n"
+ "You will be notified by email when someone participates.",
+ recipient=poll.author_email,
+ )
+ except Exception as e:
+ traceback.print_exc(file=sys.stderr)
+ print(f"Failed to send participation email to {poll.author_email}", file=sys.stderr)
-def send_poll_created_email_if_enabled(poll_id: str):
- if email_enabled:
- poll = db.get_poll(poll_id)
- if not poll or poll.author_email is None:
- return
+def send_poll_created_email(poll_id: str):
+ poll = db.get_poll(poll_id)
+ if not poll or poll.author_email is None:
+ return
- try:
- send_email(
- subject=f"You created a new diddle \"{poll.title}\"",
- body=f"Manage your diddle at {BASE_URL}/manage/{poll.manage_code}\n"
- "You will be notified by email when someone participates.",
- recipient=poll.author_email,
- )
- except Exception as e:
- traceback.print_exc(file=sys.stderr)
- print(f"Failed to send poll created email to {poll.author_email}", file=sys.stderr) \ No newline at end of file
+ print(f"Sending poll created email to {poll.author_email}")
+ try:
+ send_email(
+ subject=f"You created a new diddle \"{poll.title}\"",
+ body=f"Manage your diddle at {BASE_URL}/manage/{poll.manage_code}\n"
+ "You will be notified by email when someone participates.",
+ recipient=poll.author_email,
+ )
+ except Exception as e:
+ traceback.print_exc(file=sys.stderr)
+ print(f"Failed to send poll created email to {poll.author_email}", file=sys.stderr)