@@ -123,30 +123,16 @@ async def add_file(
123123 vectordb = Depends (get_vectordb ),
124124 user = Depends (require_partition_editor ),
125125):
126- log = logger .bind (
127- file_id = file_id ,
128- partition = partition ,
129- filename = file .filename ,
130- user = user .get ("display_name" ),
131- )
132-
133126 if await vectordb .file_exists .remote (file_id , partition ):
134127 raise HTTPException (
135128 status_code = status .HTTP_409_CONFLICT ,
136129 detail = f"File '{ file_id } ' already exists in partition { partition } " ,
137130 )
138131
139132 save_dir = Path (DATA_DIR )
140- try :
141- original_filename = file .filename
142- file .filename = sanitize_filename (file .filename )
143- file_path = await save_file_to_disk (file , save_dir , with_random_prefix = True )
144- except Exception as e :
145- log .exception ("Failed to save file to disk." , error = str (e ))
146- raise HTTPException (
147- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
148- detail = str (e ),
149- )
133+ original_filename = file .filename
134+ file .filename = sanitize_filename (file .filename )
135+ file_path = await save_file_to_disk (file , save_dir , with_random_prefix = True )
150136
151137 metadata .update (
152138 {
@@ -240,8 +226,6 @@ async def put_file(
240226 vectordb = Depends (get_vectordb ),
241227 user = Depends (require_partition_editor ),
242228):
243- log = logger .bind (file_id = file_id , partition = partition , filename = file .filename )
244-
245229 if not await vectordb .file_exists .remote (file_id , partition ):
246230 raise HTTPException (
247231 status_code = status .HTTP_404_NOT_FOUND ,
@@ -252,16 +236,9 @@ async def put_file(
252236 await indexer .delete_file .remote (file_id , partition )
253237
254238 save_dir = Path (DATA_DIR )
255- try :
256- original_filename = file .filename
257- file .filename = sanitize_filename (file .filename )
258- file_path = await save_file_to_disk (file , save_dir , with_random_prefix = True )
259- except Exception :
260- log .exception ("Failed to save file to disk." )
261- raise HTTPException (
262- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
263- detail = "Failed to save uploaded file." ,
264- )
239+ original_filename = file .filename
240+ file .filename = sanitize_filename (file .filename )
241+ file_path = await save_file_to_disk (file , save_dir , with_random_prefix = True )
265242
266243 metadata .update (
267244 {
@@ -441,21 +418,13 @@ async def get_task_error(
441418 task_state_manager = Depends (get_task_state_manager ),
442419 task_details = Depends (require_task_owner ),
443420):
444- try :
445- error = await task_state_manager .get_error .remote (task_id )
446- if error is None :
447- raise HTTPException (
448- status_code = status .HTTP_404_NOT_FOUND ,
449- detail = f"No error found for task '{ task_id } '." ,
450- )
451- return {"task_id" : task_id , "traceback" : error .splitlines ()}
452- except HTTPException :
453- raise
454- except Exception :
421+ error = await task_state_manager .get_error .remote (task_id )
422+ if error is None :
455423 raise HTTPException (
456- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
457- detail = "Failed to retrieve task error ." ,
424+ status_code = status .HTTP_404_NOT_FOUND ,
425+ detail = f"No error found for task ' { task_id } ' ." ,
458426 )
427+ return {"task_id" : task_id , "traceback" : error .splitlines ()}
459428
460429
461430@router .get (
@@ -475,32 +444,27 @@ async def get_task_error(
475444""" ,
476445)
477446async def get_task_logs (task_id : str , max_lines : int = 100 , task_details = Depends (require_task_owner )):
478- try :
479- if not LOG_FILE .exists ():
480- raise HTTPException (status_code = 500 , detail = "Log file not found." )
481-
482- logs = []
483- with open (LOG_FILE , errors = "replace" ) as f :
484- for line in reversed (list (f )):
485- try :
486- record = json .loads (line ).get ("record" , {})
487- if record .get ("extra" , {}).get ("task_id" ) == task_id :
488- logs .append (
489- f"{ record ['time' ]['repr' ]} - { record ['level' ]['name' ]} - { record ['message' ]} - { (record ['extra' ])} "
490- )
491- if len (logs ) >= max_lines :
492- break
493- except json .JSONDecodeError :
494- continue
495-
496- if not logs :
497- raise HTTPException (status_code = 404 , detail = f"No logs found for task '{ task_id } '" )
498-
499- return JSONResponse (content = {"task_id" : task_id , "logs" : logs [::- 1 ]}) # restore order
500- except HTTPException :
501- raise
502- except Exception as e :
503- raise HTTPException (status_code = 500 , detail = f"Failed to fetch logs: { e !s} " )
447+ if not LOG_FILE .exists ():
448+ raise HTTPException (status_code = 500 , detail = "Log file not found." )
449+
450+ logs = []
451+ with open (LOG_FILE , errors = "replace" ) as f :
452+ for line in reversed (list (f )):
453+ try :
454+ record = json .loads (line ).get ("record" , {})
455+ if record .get ("extra" , {}).get ("task_id" ) == task_id :
456+ logs .append (
457+ f"{ record ['time' ]['repr' ]} - { record ['level' ]['name' ]} - { record ['message' ]} - { (record ['extra' ])} "
458+ )
459+ if len (logs ) >= max_lines :
460+ break
461+ except json .JSONDecodeError :
462+ continue
463+
464+ if not logs :
465+ raise HTTPException (status_code = 404 , detail = f"No logs found for task '{ task_id } '" )
466+
467+ return JSONResponse (content = {"task_id" : task_id , "logs" : logs [::- 1 ]}) # restore order
504468
505469
506470@router .delete (
@@ -525,13 +489,9 @@ async def cancel_task(
525489 task_state_manager = Depends (get_task_state_manager ),
526490 task_details = Depends (require_task_owner ),
527491):
528- try :
529- obj_ref = await task_state_manager .get_object_ref .remote (task_id )
530- if obj_ref is None :
531- raise HTTPException (404 , f"No ObjectRef stored for task { task_id } " )
532-
533- ray .cancel (obj_ref ["ref" ], recursive = True )
534- return {"message" : f"Cancellation signal sent for task { task_id } " }
535- except Exception as e :
536- logger .exception ("Failed to cancel task." )
537- raise HTTPException (status_code = 500 , detail = str (e ))
492+ obj_ref = await task_state_manager .get_object_ref .remote (task_id )
493+ if obj_ref is None :
494+ raise HTTPException (404 , f"No ObjectRef stored for task { task_id } " )
495+
496+ ray .cancel (obj_ref ["ref" ], recursive = True )
497+ return {"message" : f"Cancellation signal sent for task { task_id } " }
0 commit comments