1313import  aiodocker 
1414import  tenacity 
1515from  aiohttp  import  ClientConnectionError , ClientSession , web 
16- 
17- from  servicelib .async_utils  import  (
16+ from  servicelib .async_utils  import  (  # pylint: disable=no-name-in-module 
1817    run_sequentially_in_context ,
19- )   # pylint: disable=no-name-in-module 
20- from  servicelib .monitor_services  import  (
18+ )
19+ from  servicelib .monitor_services  import  (   # pylint: disable=no-name-in-module 
2120    service_started ,
2221    service_stopped ,
23- )   # pylint: disable=no-name-in-module 
22+ )
2423
2524from  . import  config , docker_utils , exceptions , registry_proxy 
2625from  .config  import  (
3029)
3130from  .services_common  import  ServicesCommonSettings 
3231from  .system_utils  import  get_system_extra_hosts_raw 
32+ from  .utils  import  parse_as_datetime 
3333
3434log  =  logging .getLogger (__name__ )
3535
@@ -503,9 +503,6 @@ async def _remove_overlay_network_of_swarm(
503503        ) from  err 
504504
505505
506- DATETIME_FORMAT  =  "%Y-%m-%dT%H:%M:%S.%fZ" 
507- 
508- 
509506async  def  _get_service_state (
510507    client : aiodocker .docker .Docker , service : Dict 
511508) ->  Tuple [ServiceState , str ]:
@@ -532,18 +529,7 @@ async def _wait_for_tasks(tasks):
532529    last_task  =  sorted (tasks , key = lambda  task : task ["UpdatedAt" ])[- 1 ]
533530    task_state  =  last_task ["Status" ]["State" ]
534531
535-     def  _to_datetime (datetime_str : str ) ->  datetime :
536-         # datetime_str is typically '2020-10-09T12:28:14.771034099Z' 
537-         #  - The T separates the date portion from the time-of-day portion 
538-         #  - The Z on the end means UTC, that is, an offset-from-UTC 
539-         # The 099 before the Z is not clear, therefore we will truncate the last part 
540-         N  =  len ("2020-10-09T12:28:14.7710" )
541-         if  len (datetime_str ) >  N :
542-             datetime_str  =  datetime_str [:N ]
543-         return  datetime .strptime (datetime_str , "%Y-%m-%dT%H:%M:%S.%f" )
544- 
545-     task_state_update_time  =  _to_datetime (last_task ["Status" ]["Timestamp" ])
546-     log .debug ("%s %s: time %s" , service ["ID" ], task_state , task_state_update_time )
532+     log .debug ("%s %s" , service ["ID" ], task_state )
547533
548534    last_task_state  =  ServiceState .STARTING   # default 
549535    last_task_error_msg  =  (
@@ -573,14 +559,20 @@ def _to_datetime(datetime_str: str) -> datetime:
573559        last_task_state  =  ServiceState .STARTING 
574560    elif  task_state  in  ("running" ):
575561        now  =  datetime .utcnow ()
562+         # NOTE: task_state_update_time is only used to discrimitate between 'starting' and 'running' 
563+         task_state_update_time  =  parse_as_datetime (
564+             last_task ["Status" ]["Timestamp" ], default = now 
565+         )
576566        time_since_running  =  now  -  task_state_update_time 
567+ 
577568        log .debug ("Now is %s, time since running mode is %s" , now , time_since_running )
578569        if  time_since_running  >  timedelta (
579570            seconds = config .DIRECTOR_SERVICES_STATE_MONITOR_S 
580571        ):
581572            last_task_state  =  ServiceState .RUNNING 
582573        else :
583574            last_task_state  =  ServiceState .STARTING 
575+ 
584576    elif  task_state  in  ("complete" , "shutdown" ):
585577        last_task_state  =  ServiceState .COMPLETE 
586578    log .debug ("service running state is %s" , last_task_state )
0 commit comments