Skip to content

Instantly share code, notes, and snippets.

@hameed0z
Created August 23, 2021 23:18
Show Gist options
  • Select an option

  • Save hameed0z/a2b80f91ff909d6818938e201be42155 to your computer and use it in GitHub Desktop.

Select an option

Save hameed0z/a2b80f91ff909d6818938e201be42155 to your computer and use it in GitHub Desktop.
from fastapi import BackgroundTasks, Header
from app import crud
from typing import Any, Optional
from app.api import deps
from sqlalchemy.orm import Session
@router.post("/publish")
async def publish_order(
background_tasks: BackgroundTasks,
db: Session = Depends(deps.get_db),
accept_language: Optional[str] = Header(default="ar", regex="(ar|en)$"),
) -> Any:
# list of orders could be 1000 or even milion and it might timeout
orders = crud.order.get_scheduled(db)
for order in orders:
# send emails and sms - background task because it takes lot of time
# depend on the network latency and the recipient number
def send_order_notifications(db, order, accept_language):
order.notify_recipients(db, accept_language)
background_tasks.add_task(
send_order_notifications, db, order, accept_language
)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment