@@ -5,6 +5,7 @@ use crate::console::Console;
55use bollard:: models:: ContainerStateStatusEnum ;
66use chrono:: { DateTime , Utc } ;
77use log:: debug;
8+ use shared:: models:: heartbeat:: TaskDetails ;
89use shared:: models:: node:: GpuSpecs ;
910use shared:: models:: task:: Task ;
1011use shared:: models:: task:: TaskState ;
@@ -347,6 +348,65 @@ impl DockerService {
347348 None => Ok ( ( ) ) ,
348349 }
349350 }
351+
352+ pub async fn get_task_details ( & self , task : & Task ) -> Option < TaskDetails > {
353+ let config_hash = task. generate_config_hash ( ) ;
354+ let container_name = format ! ( "{}-{}-{:x}" , TASK_PREFIX , task. id, config_hash) ;
355+
356+ match self . docker_manager . list_containers ( true ) . await {
357+ Ok ( containers) => {
358+ let container = containers
359+ . iter ( )
360+ . find ( |c| c. names . contains ( & format ! ( "/{}" , container_name) ) ) ;
361+
362+ if let Some ( container) = container {
363+ match self
364+ . docker_manager
365+ . get_container_details ( & container. id )
366+ . await
367+ {
368+ Ok ( details) => {
369+ let docker_image_id = if let Ok ( inspect_result) =
370+ self . docker_manager . inspect_container ( & container. id ) . await
371+ {
372+ inspect_result. image
373+ } else {
374+ Some ( container. image . clone ( ) )
375+ } ;
376+
377+ Some ( TaskDetails {
378+ docker_image_id,
379+ container_id : Some ( container. id . clone ( ) ) ,
380+ container_status : details. status . map ( |s| format ! ( "{:?}" , s) ) ,
381+ container_created_at : Some ( container. created ) ,
382+ container_exit_code : details. status_code ,
383+ } )
384+ }
385+ Err ( e) => {
386+ debug ! ( "Failed to get container details: {}" , e) ;
387+ Some ( TaskDetails {
388+ docker_image_id : Some ( container. image . clone ( ) ) ,
389+ container_id : Some ( container. id . clone ( ) ) ,
390+ container_status : None ,
391+ container_created_at : Some ( container. created ) ,
392+ container_exit_code : None ,
393+ } )
394+ }
395+ }
396+ } else {
397+ debug ! (
398+ "Container {} not found for task {}" ,
399+ container_name, task. id
400+ ) ;
401+ None
402+ }
403+ }
404+ Err ( e) => {
405+ debug ! ( "Failed to list containers: {}" , e) ;
406+ None
407+ }
408+ }
409+ }
350410}
351411
352412#[ cfg( test) ]
0 commit comments