@@ -30,9 +30,7 @@ class KubernetesTaskRunner {
3030 ) ;
3131 const isRunning = await KubernetesPods . IsPodRunning ( podName , namespace , kubeClient ) ;
3232 let extraFlags = `` ;
33- extraFlags += isRunning
34- ? ` -f -c ${ containerName } -n ${ namespace } `
35- : ` --previous -n ${ namespace } ` ;
33+ extraFlags += isRunning ? ` -f -c ${ containerName } -n ${ namespace } ` : ` --previous -n ${ namespace } ` ;
3634
3735 const callback = ( outputChunk : string ) => {
3836 output += outputChunk ;
@@ -53,30 +51,61 @@ class KubernetesTaskRunner {
5351 await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ;
5452 const continueStreaming = await KubernetesPods . IsPodRunning ( podName , namespace , kubeClient ) ;
5553 CloudRunnerLogger . log ( `K8s logging error ${ error } ${ continueStreaming } ` ) ;
54+
5655 // If pod is not running and we tried --previous but it failed, try without --previous
5756 if ( ! isRunning && ! continueStreaming && error ?. message ?. includes ( 'previous terminated container' ) ) {
5857 CloudRunnerLogger . log ( `Previous container not found, trying current container logs...` ) ;
5958 try {
60- await CloudRunnerSystem . Run ( `kubectl logs ${ podName } -c ${ containerName } -n ${ namespace } ` , false , true , callback ) ;
59+ await CloudRunnerSystem . Run (
60+ `kubectl logs ${ podName } -c ${ containerName } -n ${ namespace } ` ,
61+ false ,
62+ true ,
63+ callback ,
64+ ) ;
65+ // If we successfully got logs, check for end of transmission
66+ if ( FollowLogStreamService . DidReceiveEndOfTransmission ) {
67+ CloudRunnerLogger . log ( 'end of log stream' ) ;
68+ break ;
69+ }
70+ // If we got logs but no end marker, continue trying (might be more logs)
71+ if ( retriesAfterFinish < KubernetesTaskRunner . maxRetry ) {
72+ retriesAfterFinish ++ ;
73+ continue ;
74+ }
75+ // If we've exhausted retries, break
76+ break ;
6177 } catch ( fallbackError : any ) {
6278 CloudRunnerLogger . log ( `Fallback log fetch also failed: ${ fallbackError } ` ) ;
63- // If both fail, continue - we'll get what we can from pod status
79+ // If both fail, continue retrying if we haven't exhausted retries
80+ if ( retriesAfterFinish < KubernetesTaskRunner . maxRetry ) {
81+ retriesAfterFinish ++ ;
82+ continue ;
83+ }
84+ // Only break if we've exhausted all retries
85+ CloudRunnerLogger . logWarning (
86+ `Could not fetch any container logs after ${ KubernetesTaskRunner . maxRetry } retries` ,
87+ ) ;
88+ break ;
6489 }
6590 }
91+
6692 if ( continueStreaming ) {
6793 continue ;
6894 }
6995 if ( retriesAfterFinish < KubernetesTaskRunner . maxRetry ) {
7096 retriesAfterFinish ++ ;
71-
7297 continue ;
7398 }
74- // Don't throw if we're just missing previous container logs - this is non-fatal
75- if ( error ?. message ?. includes ( 'previous terminated container' ) ) {
76- CloudRunnerLogger . logWarning ( `Could not fetch previous container logs, but continuing...` ) ;
77- break ;
99+
100+ // If we've exhausted retries and it's not a previous container issue, throw
101+ if ( ! error ?. message ?. includes ( ' previous terminated container' ) ) {
102+ throw error ;
78103 }
79- throw error ;
104+ // For previous container errors, we've already tried fallback, so just break
105+ CloudRunnerLogger . logWarning (
106+ `Could not fetch previous container logs after retries, but continuing with available logs` ,
107+ ) ;
108+ break ;
80109 }
81110 if ( FollowLogStreamService . DidReceiveEndOfTransmission ) {
82111 CloudRunnerLogger . log ( 'end of log stream' ) ;
0 commit comments