11"""API routes related to application tasks (V2 / tenant friendly)."""
22
33from celery .result import AsyncResult
4- from fastapi import APIRouter , Depends , HTTPException
4+ from fastapi import APIRouter , Depends , HTTPException , Request
55
66from summary .core .celery_worker import (
7+ celery ,
78 process_audio_transcribe_v2_task ,
89 summarize_v2_task ,
910)
1011from summary .core .config import AuthorizedTenant
1112from summary .core .models import SummarizeTaskV2Request , TranscribeTaskV2Request
1213from summary .core .security import verify_tenant_api_key_v2
14+ from summary .core .shared_models import (
15+ SummarizeWebhookFailurePayload ,
16+ SummarizeWebhookPendingPayload ,
17+ SummarizeWebhookSuccessPayload ,
18+ TranscribeWebhookFailurePayload ,
19+ TranscribeWebhookPendingPayload ,
20+ TranscribeWebhookSuccessPayload ,
21+ )
1322
1423router_tasks_v2 = APIRouter ()
1524
@@ -24,7 +33,7 @@ async def create_transcribe_task_v2(
2433 args = [{** request .model_dump (), "tenant_id" : request_tenant .id }]
2534 )
2635
27- return { " job_id" : task .id , "message" : "Transcribe job created" }
36+ return TranscribeWebhookPendingPayload ( job_id = task .id ). model_dump ()
2837
2938
3039@router_tasks_v2 .post ("/async-jobs/summarize" )
@@ -37,25 +46,64 @@ async def create_summarize_task_v2(
3746 args = [{** request .model_dump (), "tenant_id" : request_tenant .id }]
3847 )
3948
40- return { " job_id" : task .id , "message" : "Summarize job created" }
49+ return SummarizeWebhookPendingPayload ( job_id = task .id ). model_dump ()
4150
4251
4352@router_tasks_v2 .get ("/async-jobs/transcribe/{job_id}" )
53+ async def get_transcribe_job_status (
54+ job_id : str ,
55+ request : Request ,
56+ request_tenant : AuthorizedTenant = Depends (verify_tenant_api_key_v2 ),
57+ ):
58+ """Check transcription task status by ID."""
59+ # We have to look directly in Redis to check if the task exists
60+ redis_client = celery .backend .client
61+ key = f"celery-task-meta-{ job_id } "
62+ if not redis_client .exists (key ):
63+ raise HTTPException (status_code = 404 , detail = "Not found" )
64+
65+ task = AsyncResult (job_id , app = celery )
66+ task_tenant_id = task .args [0 ]["tenant_id" ]
67+ if task_tenant_id != request_tenant .id :
68+ raise HTTPException (status_code = 403 , detail = "Forbidden" )
69+
70+ if task .status == "SUCCESS" :
71+ result = task .result
72+ return TranscribeWebhookSuccessPayload .model_validate (result ).model_dump ()
73+
74+ if task .status == "FAILURE" :
75+ return TranscribeWebhookFailurePayload (
76+ job_id = job_id , error_code = "unknown_error"
77+ ).model_dump ()
78+
79+ return TranscribeWebhookPendingPayload (job_id = job_id ).model_dump ()
80+
81+
4482@router_tasks_v2 .get ("/async-jobs/summarize/{job_id}" )
45- async def get_task_status (
83+ async def get_summarize_job_status (
4684 job_id : str ,
85+ request : Request ,
4786 request_tenant : AuthorizedTenant = Depends (verify_tenant_api_key_v2 ),
4887):
49- """Check task status by ID."""
50- task = AsyncResult (job_id )
51- try :
52- if (
53- isinstance (task .args , (list , tuple ))
54- and len (task .args ) > 0
55- and isinstance (task .args [0 ], dict )
56- and task .args [0 ].get ("tenant_id" ) == request_tenant .id
57- ):
58- return {"job_id" : job_id , "status" : task .status }
59- except (TypeError , KeyError ):
60- pass
61- raise HTTPException (status_code = 404 , detail = "Not found" )
88+ """Check summarize task status by ID."""
89+ # We have to look directly in Redis to check if the task exists
90+ redis_client = celery .backend .client
91+ key = f"celery-task-meta-{ job_id } "
92+ if not redis_client .exists (key ):
93+ raise HTTPException (status_code = 404 , detail = "Not found" )
94+
95+ task = AsyncResult (job_id , app = celery )
96+ task_tenant_id = task .args [0 ]["tenant_id" ]
97+ if task_tenant_id != request_tenant .id :
98+ raise HTTPException (status_code = 403 , detail = "Forbidden" )
99+
100+ if task .status == "SUCCESS" :
101+ result = task .result
102+ return SummarizeWebhookSuccessPayload .model_validate (result ).model_dump ()
103+
104+ if task .status == "FAILURE" :
105+ return SummarizeWebhookFailurePayload (
106+ job_id = job_id , error_code = "unknown_error"
107+ ).model_dump ()
108+
109+ return SummarizeWebhookPendingPayload (job_id = job_id ).model_dump ()
0 commit comments