Skip to content

Commit ae8b920

Browse files
committed
signals & simple background tasks
1 parent f06592a commit ae8b920

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

.env.example

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ TELEGRAM_PYTHON_LOG_LEVEL=ERROR
44
TELEGRAM_TOKEN=123456789:ABCDEFG_HIJKLMN_OPQ
55
TELEGRAM_CHAT_ID=-87654321
66

7+
SITE_ADMIN_EMAIL=me@example.com
8+
9+
SMTP_MAILHOST_HOST=email.example.com
10+
SMTP_MAILHOST_PORT=25
11+
SMTP_FROMADDR=alerts@example.com
12+
SMTP_TOADDRS=alerts@example.com
13+
SMTP_SUBJECT="Log alert"
14+
SMTP_CREDENTIALS_EMAIL=alerts@example.com
15+
SMTP_CREDENTIALS_PASSWORD=changeme
16+
SMTP_SECURE=()
17+
SMTP_TIMEOUT=3
18+
19+
720
GITHUB_OAUTH_CLIENT_ID=changeme-get-from-github
821
GITHUB_OAUTH_CLIENT_SECRET=changeme-get-from-github
922
GITHUB_OAUTH_REDIRECT_URI="https://example.com/githubcallback"

app.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import secrets
2424
from dotenv import load_dotenv
2525
import subprocess
26+
from signals import signal_new_repo
2627

2728
log = logger
2829

@@ -175,6 +176,9 @@ async def githubcallback(request):
175176
req = requests.get("https://api.github.com/user/emails", headers=headers)
176177
email = req.json()[0].get("email", None)
177178

179+
# Signal that a new repo is created
180+
signal_new_repo.send()
181+
178182
# Create a repo for organisation user has access to
179183
# req = requests.post("https://api.github.com/orgs/karmacomputing/repos", headers=headers, data=json.dumps(data))
180184
# Create repo for authenticated user
@@ -535,10 +539,14 @@ async def blog(request):
535539

536540
async def health(request):
537541
log.debug(request)
538-
log.error("Testing error logging is working")
539542
return PlainTextResponse("OK")
540543

541544

545+
async def notify(request):
546+
log.info(request)
547+
return PlainTextResponse("Notification sent")
548+
549+
542550
async def not_found(request: Request, exc: HTTPException):
543551
return HTMLResponse(content="404", status_code=exc.status_code)
544552

@@ -563,6 +571,7 @@ async def robots(request: Request):
563571
Route("/health", health, methods=["GET"]),
564572
Route("/githubcallback", githubcallback, methods=["GET"]),
565573
Route("/heroku-alternatives", blog, methods=["GET"]),
574+
Route("/notify", notify, methods=["GET"]),
566575
Route("/{path:path}", catch_all),
567576
]
568577

email_tools.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import os
2+
from dotenv import load_dotenv
3+
import background
4+
5+
load_dotenv()
6+
7+
SMTP_MAILHOST_HOST = os.getenv("SMTP_MAILHOST_HOST")
8+
SMTP_MAILHOST_PORT = int(os.getenv("SMTP_MAILHOST_PORT"))
9+
10+
SMTP_MAILHOST = (
11+
SMTP_MAILHOST_HOST,
12+
SMTP_MAILHOST_PORT,
13+
) # noqa: E501
14+
SMTP_FROMADDR = os.getenv("SMTP_FROMADDR")
15+
SMTP_TOADDRS = os.getenv("SMTP_TOADDRS")
16+
SMTP_SUBJECT = os.getenv("SMTP_SUBJECT")
17+
SMTP_CREDENTIALS_EMAIL = os.getenv("SMTP_CREDENTIALS_EMAIL")
18+
SMTP_CREDENTIALS_PASSWORD = os.getenv("SMTP_CREDENTIALS_PASSWORD") # noqa: E501
19+
SMTP_CREDENTIALS = (
20+
SMTP_CREDENTIALS_EMAIL,
21+
SMTP_CREDENTIALS_PASSWORD,
22+
)
23+
SMTP_SECURE = os.getenv("SMTP_SECURE")
24+
if SMTP_SECURE == "()":
25+
SMTP_SECURE = ()
26+
else:
27+
SMTP_SECURE = None
28+
29+
SMTP_TIMEOUT = int(os.getenv("SMTP_TIMEOUT"))
30+
31+
32+
@background.task
33+
def send_email(subject, from_email, to_email, body):
34+
try:
35+
# Import smtplib for the actual sending function
36+
import smtplib
37+
38+
# Import the email modules we'll need
39+
from email.message import EmailMessage
40+
41+
# Create a text/plain message with body
42+
msg = EmailMessage()
43+
msg.set_content(body)
44+
msg["Subject"] = subject
45+
msg["From"] = from_email
46+
msg["To"] = to_email
47+
48+
# Send the message via our own SMTP server.
49+
s = smtplib.SMTP(host=SMTP_MAILHOST_HOST, port=SMTP_MAILHOST_PORT)
50+
s.connect(SMTP_MAILHOST_HOST, SMTP_MAILHOST_PORT)
51+
s.starttls()
52+
s.login(
53+
user=SMTP_CREDENTIALS_EMAIL, password=SMTP_CREDENTIALS_PASSWORD
54+
) # noqa: E501
55+
s.send_message(msg)
56+
s.quit()
57+
except Exception as e:
58+
print(f"Could not send email: {e}")

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ cryptography
88
PyNaCl
99
GitPython
1010
coloredlogs
11+
blinker
12+
background

signals.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from blinker import signal
2+
from logger import logger
3+
from email_tools import send_email
4+
import os
5+
6+
log = logger
7+
8+
SITE_ADMIN_EMAIL = os.getenv("SITE_ADMIN_EMAIL")
9+
10+
# Signals
11+
signal_new_repo = signal("signal_new_repo")
12+
13+
# Signal subscribers:
14+
15+
16+
def signal_subscriber_new_repo(sender):
17+
log.info(f"signal_subscriber_new_repo signal caught by sender {sender}")
18+
send_email(
19+
"New repo created", SITE_ADMIN_EMAIL, SITE_ADMIN_EMAIL, "New repo created"
20+
)
21+
22+
23+
# Bind Signals to subscribers
24+
signal_new_repo.connect(signal_subscriber_new_repo)

0 commit comments

Comments
 (0)