Skip to content

Commit 727b0e8

Browse files
committed
Adding in the docker compose configs and the kubernetes exmples
1 parent 21fcc17 commit 727b0e8

File tree

3 files changed

+62
-53
lines changed

3 files changed

+62
-53
lines changed

backend/success/management/commands/send_daily_calendar_email.py

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
"""
22
Django management command to send daily calendar email summaries.
33
4-
This command should be run via cron job daily at the user's preferred time.
4+
This command checks if it's the user's preferred time (within 1 minute window)
5+
before sending emails. It respects the email_time and timezone settings from
6+
NotificationSettings.
57
6-
Example cron job (runs at 7:00 AM daily):
7-
0 7 * * * cd /path/to/project && python manage.py send_daily_calendar_email
8+
This should be run via cron job every minute, and it will only send emails
9+
at the user's configured preferred time:
10+
* * * * * cd /path/to/project && python manage.py send_daily_calendar_email
811
912
"""
1013
from django.core.management.base import BaseCommand
1114
from django.utils import timezone
1215
from success.calendar_service import CalendarService
1316
from success.models import NotificationSettings
17+
import datetime
18+
import pytz
1419

1520

1621
class Command(BaseCommand):
1722
help = 'Send daily calendar email summary if enabled'
1823

19-
def add_arguments(self, parser):
20-
parser.add_argument(
21-
'--force',
22-
action='store_true',
23-
help='Force send email even if already sent today',
24-
)
25-
parser.add_argument(
26-
'--test',
27-
action='store_true',
28-
help='Send test email for today (ignores normal scheduling)',
29-
)
3024

3125
def handle(self, *args, **options):
3226
self.stdout.write(
@@ -51,47 +45,26 @@ def handle(self, *args, **options):
5145
)
5246
return
5347

54-
# Initialize calendar service
55-
calendar_service = CalendarService()
56-
57-
if options['test']:
58-
# Send test email for today's events
59-
self.stdout.write('Sending test email...')
60-
events, date = calendar_service.get_tomorrow_events()
61-
62-
# For test, use today's events instead
63-
import datetime
64-
import pytz
65-
local_timezone = pytz.timezone(notification_settings.timezone)
66-
today = datetime.datetime.now(local_timezone).date()
67-
68-
# Override the date for test
69-
events, _ = calendar_service.get_tomorrow_events()
70-
text_summary = calendar_service.create_text_summary(events, today)
71-
self.stdout.write(f'Found {len(events)} events for {today}')
72-
73-
if calendar_service.send_daily_email():
74-
self.stdout.write(
75-
self.style.SUCCESS('Test email sent successfully!')
76-
)
77-
else:
78-
self.stdout.write(
79-
self.style.ERROR('Failed to send test email')
48+
# Check if it's the preferred time to send email
49+
local_timezone = pytz.timezone(notification_settings.timezone)
50+
current_time = datetime.datetime.now(local_timezone).time()
51+
preferred_time = notification_settings.email_time
52+
53+
# Allow a 1-minute window around the preferred time
54+
preferred_datetime = datetime.datetime.combine(datetime.date.today(), preferred_time)
55+
current_datetime = datetime.datetime.combine(datetime.date.today(), current_time)
56+
time_diff = abs((current_datetime - preferred_datetime).total_seconds())
57+
58+
if time_diff > 60: # More than 1 minute difference
59+
self.stdout.write(
60+
self.style.WARNING(
61+
f'Current time ({current_time.strftime("%H:%M")}) is not within 1 minute of preferred time ({preferred_time.strftime("%H:%M")}). Skipping email send.'
8062
)
63+
)
8164
return
8265

83-
# Normal daily email flow
84-
if options['force']:
85-
self.stdout.write('Forcing email send (ignoring previous sends)...')
86-
# Temporarily remove existing log to force send
87-
events, tomorrow_date = calendar_service.get_tomorrow_events()
88-
from success.models import CalendarEmailLog
89-
CalendarEmailLog.objects.filter(
90-
email_date=tomorrow_date,
91-
email_address=notification_settings.email_address
92-
).delete()
93-
94-
# Send daily email
66+
# Initialize calendar service and send daily email
67+
calendar_service = CalendarService()
9568
success = calendar_service.send_daily_email()
9669

9770
if success:

examples/docker-compose.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ services:
5959
- POSTGRES_DB=postgres
6060
- POSTGRES_USER=postgres
6161
- POSTGRES_PASSWORD=postgres
62+
- OPENAI_API_KEY=${OPENAI_API_KEY}
63+
- CALENDAR_ENCRYPTION_KEY=${CALENDAR_ENCRYPTION_KEY}
64+
- EMAIL_HOST_USER=${EMAIL_HOST_USER}
65+
- EMAIL_HOST_PASSWORD=${EMAIL_HOST_PASSWORD}
66+
- DEFAULT_FROM_EMAIL=${DEFAULT_FROM_EMAIL}
6267
healthcheck:
6368
test: "pg_isready -U postgres"
6469
start_period: 20s

examples/kubernetes.yaml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ stringData:
77
open-ai-api-key: {{open-ai-api-key}}
88
postgres-password: {{password}}
99
secret-key: {{secret-key}}
10+
calendar-encryption-key: {{calendar-encryption-key}}
11+
email-host-password: {{email-host-password}}
12+
1013

1114
---
1215
apiVersion: apps/v1
@@ -79,6 +82,20 @@ spec:
7982
secretKeyRef:
8083
name: success-secrets
8184
key: open-ai-api-key
85+
- name: CALENDAR_ENCRYPTION_KEY
86+
valueFrom:
87+
secretKeyRef:
88+
name: success-secrets
89+
key: calendar-encryption-key
90+
- name: EMAIL_HOST_USER
91+
value: "{{email-host-user}}"
92+
- name: EMAIL_HOST_PASSWORD
93+
valueFrom:
94+
secretKeyRef:
95+
name: success-secrets
96+
key: email-host-password
97+
- name: DEFAULT_FROM_EMAIL
98+
value: "{{default-from-email}}"
8299
---
83100
kind: Service
84101
apiVersion: v1
@@ -165,7 +182,7 @@ metadata:
165182
labels:
166183
app: success-daily-email
167184
spec:
168-
schedule: "0 7 * * *" # Run daily at 7:00 AM UTC
185+
schedule: "* * * * *"
169186
jobTemplate:
170187
spec:
171188
template:
@@ -200,6 +217,20 @@ spec:
200217
secretKeyRef:
201218
name: success-secrets
202219
key: open-ai-api-key
220+
- name: CALENDAR_ENCRYPTION_KEY
221+
valueFrom:
222+
secretKeyRef:
223+
name: success-secrets
224+
key: calendar-encryption-key
225+
- name: EMAIL_HOST_USER
226+
value: "{{email-host-user}}"
227+
- name: EMAIL_HOST_PASSWORD
228+
valueFrom:
229+
secretKeyRef:
230+
name: success-secrets
231+
key: email-host-password
232+
- name: DEFAULT_FROM_EMAIL
233+
value: "{{default-from-email}}"
203234
restartPolicy: OnFailure
204235
successfulJobsHistoryLimit: 3
205236
failedJobsHistoryLimit: 1

0 commit comments

Comments
 (0)