feat: schedule S3 upload for variant cards after Playwright render
Adds BackgroundTasks to the card render endpoint. After rendering a variant card (variant > 0) where image_url is None, schedules backfill_variant_image_url to upload the PNG to S3 and populate image_url on the card row. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
534d50f1a8
commit
4ccc0841a8
@ -2,7 +2,15 @@ import datetime
|
||||
import os.path
|
||||
|
||||
import pandas as pd
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, Response, Query
|
||||
from fastapi import (
|
||||
APIRouter,
|
||||
BackgroundTasks,
|
||||
Depends,
|
||||
HTTPException,
|
||||
Request,
|
||||
Response,
|
||||
Query,
|
||||
)
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from typing import Optional, List, Literal
|
||||
@ -732,6 +740,7 @@ async def get_one_player(player_id: int, csv: Optional[bool] = False):
|
||||
@router.get("/{player_id}/{card_type}card/{d}/{variant}")
|
||||
async def get_batter_card(
|
||||
request: Request,
|
||||
background_tasks: BackgroundTasks,
|
||||
player_id: int,
|
||||
card_type: Literal["batting", "pitching"],
|
||||
variant: int = 0,
|
||||
@ -906,6 +915,27 @@ async def get_batter_card(
|
||||
# save_as=f'{player_id}-{d}-v{variant}.png'
|
||||
# )
|
||||
|
||||
# Schedule S3 upload for variant cards that don't have an image_url yet
|
||||
if variant > 0 and tier is None:
|
||||
CardModel = BattingCard if card_type == "batting" else PitchingCard
|
||||
try:
|
||||
card_row = CardModel.get(
|
||||
(CardModel.player_id == player_id) & (CardModel.variant == variant)
|
||||
)
|
||||
if card_row.image_url is None:
|
||||
from app.services.card_storage import backfill_variant_image_url
|
||||
|
||||
background_tasks.add_task(
|
||||
backfill_variant_image_url,
|
||||
player_id=player_id,
|
||||
variant=variant,
|
||||
card_type=card_type,
|
||||
cardset_id=this_player.cardset.id,
|
||||
png_path=file_path,
|
||||
)
|
||||
except CardModel.DoesNotExist:
|
||||
pass
|
||||
|
||||
return FileResponse(path=file_path, media_type="image/png", headers=headers)
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user