Skip to content

Commit

Permalink
optimized modal cron
Browse files Browse the repository at this point in the history
  • Loading branch information
Becca-Saka committed Aug 23, 2024
1 parent cd93a7f commit b4a0d98
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
5 changes: 2 additions & 3 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import json
import os

Expand Down Expand Up @@ -64,6 +63,6 @@ def api():
os.makedirs(path)


@modal_app.function(image=image,schedule=Cron('15 * * * *'))
@modal_app.function(image=image,schedule=Cron('* * * * *'))
async def start_job():
asyncio.run(start_cron_job())
await start_cron_job()
32 changes: 31 additions & 1 deletion backend/utils/notifications.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
from firebase_admin import messaging
import math

def send_notification(token: str, title: str, body: str, data: dict = None):
print('send_notification', token, title, body, data)
Expand All @@ -8,8 +10,36 @@ def send_notification(token: str, title: str, body: str, data: dict = None):
if data:
message.data = data

try:
try:
response = messaging.send(message)
print("Successfully sent message:", response)
except Exception as e:
print("Error sending message:", e)


async def send_bulk_notification(user_tokens: list, title: str, body: str):
try:
batch_size = 500
num_batches = math.ceil(len(user_tokens) / batch_size)

def send_batch(batch_users):
messages = [
messaging.Message(
notification=messaging.Notification(title=title, body=body),
token=token
) for token in batch_users
]
return messaging.send_all(messages)

tasks = []
for i in range(num_batches):
start = i * batch_size
end = start + batch_size
batch_users = user_tokens[start:end]
task = asyncio.to_thread(send_batch, batch_users)
tasks.append(task)

await asyncio.gather(*tasks)

except Exception as e:
print("Error sending message:", e)
24 changes: 10 additions & 14 deletions backend/utils/other/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytz

import database.notifications as notification_db
from utils.notifications import send_notification
from utils.notifications import send_bulk_notification


async def start_cron_job():
Expand All @@ -23,8 +23,14 @@ async def send_daily_notification():
morning_target_time = "08:00"
daily_summary_target_time = "22:00"

await _send_notification_for_time(morning_target_time, morning_alert_title, morning_alert_body)
await _send_notification_for_time(daily_summary_target_time, daily_summary_title, daily_summary_body)
morning_task = asyncio.create_task(
_send_notification_for_time(morning_target_time, morning_alert_title, morning_alert_body)
)
evening_task = asyncio.create_task(
_send_notification_for_time(daily_summary_target_time, daily_summary_title, daily_summary_body)
)

await asyncio.gather(morning_task, evening_task)

except Exception as e:
print(e)
Expand All @@ -34,7 +40,7 @@ async def send_daily_notification():

async def _send_notification_for_time(target_time: str, title: str, body: str):
user_in_time_zone = await _get_users_in_timezone(target_time)
await _send_bulk_notification(user_in_time_zone, title, body)
await send_bulk_notification(user_in_time_zone, title, body)
return user_in_time_zone


Expand All @@ -51,13 +57,3 @@ def _get_timezones_at_time(target_time):
if current_time == target_time:
target_timezones.append(tz_name)
return target_timezones


async def _send_bulk_notification(users: list, title: str, body: str):
loop = asyncio.get_running_loop()
with concurrent.futures.ThreadPoolExecutor() as pool:
tasks = [
loop.run_in_executor(pool, send_notification, token, title, body)
for token in users
]
await asyncio.gather(*tasks)

0 comments on commit b4a0d98

Please sign in to comment.