Skip to content

Commit 1fed07f

Browse files
committed
revert savePodLogOnError remove
Signed-off-by: jorgee <[email protected]>
1 parent 2765d4a commit 1fed07f

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

plugins/nf-k8s/src/main/nextflow/k8s/K8sTaskHandler.groovy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ class K8sTaskHandler extends TaskHandler implements FusionAwareTask {
441441
task.stderr = errorFile
442442
}
443443
status = TaskStatus.COMPLETED
444+
savePodLogOnError(task)
444445
deletePodIfSuccessful(task)
445446
updateTimestamps(state.terminated as Map)
446447
determineNode()
@@ -450,6 +451,31 @@ class K8sTaskHandler extends TaskHandler implements FusionAwareTask {
450451
return false
451452
}
452453

454+
protected void savePodLogOnError(TaskRun task) {
455+
if( task.isSuccess() )
456+
return
457+
458+
if( errorFile && !errorFile.empty() )
459+
return
460+
461+
final session = executor.getSession()
462+
if( session.isAborted() || session.isCancelled() || session.isTerminated() )
463+
return
464+
465+
try {
466+
final stream = useJobResource()
467+
? client.jobLog(podName)
468+
: client.podLog(podName)
469+
Files.copy(stream, task.workDir.resolve(TaskRun.CMD_LOG))
470+
}
471+
catch( FileAlreadyExistsException e ) {
472+
log.debug "Log file already exists for ${resourceType.lower()} $podName", e
473+
}
474+
catch( Exception e ) {
475+
log.warn "Failed to copy log for ${resourceType.lower()} $podName", e
476+
}
477+
}
478+
453479
protected int readExitFile() {
454480
try {
455481
exitFile.text as Integer

plugins/nf-k8s/src/test/nextflow/k8s/K8sTaskHandlerTest.groovy

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ class K8sTaskHandlerTest extends Specification {
498498
1 * handler.updateTimestamps(termState)
499499
1 * handler.readExitFile() >> EXIT_STATUS
500500
1 * handler.deletePodIfSuccessful(task) >> null
501+
1 * handler.savePodLogOnError(task) >> null
501502
handler.task.exitStatus == EXIT_STATUS
502503
handler.task.@stdout == OUT_FILE
503504
handler.task.@stderr == ERR_FILE
@@ -528,6 +529,7 @@ class K8sTaskHandlerTest extends Specification {
528529
1 * handler.updateTimestamps(termState)
529530
0 * handler.readExitFile()
530531
1 * handler.deletePodIfSuccessful(task) >> null
532+
1 * handler.savePodLogOnError(task) >> null
531533
handler.task.exitStatus == 137
532534
handler.status == TaskStatus.COMPLETED
533535
result == true
@@ -786,6 +788,43 @@ class K8sTaskHandlerTest extends Specification {
786788

787789
}
788790

791+
def 'should save pod log' () {
792+
793+
given:
794+
def folder = Files.createTempDirectory('test')
795+
def POD_NAME = 'the-pod-name'
796+
def POD_MESSAGE = 'Hello world!'
797+
def POD_LOG = new ByteArrayInputStream(new String(POD_MESSAGE).bytes)
798+
def session = Mock(Session)
799+
def task = Mock(TaskRun)
800+
def executor = Mock(K8sExecutor)
801+
def client = Mock(K8sClient)
802+
and:
803+
def handler = Spy(new K8sTaskHandler(executor: executor, client: client, podName: POD_NAME))
804+
805+
when:
806+
handler.savePodLogOnError(task)
807+
then:
808+
task.isSuccess() >> true
809+
0 * client.podLog(_)
810+
811+
when:
812+
handler.savePodLogOnError(task)
813+
then:
814+
task.isSuccess() >> false
815+
task.getWorkDir() >> folder
816+
executor.getSession() >> session
817+
session.isTerminated() >> false
818+
session.isCancelled() >> false
819+
session.isAborted() >> false
820+
1 * client.podLog(POD_NAME) >> POD_LOG
821+
822+
folder.resolve( TaskRun.CMD_LOG ).text == POD_MESSAGE
823+
cleanup:
824+
folder?.deleteDir()
825+
826+
}
827+
789828
def 'should merge pod options' () {
790829
given:
791830
PodOptions opts

0 commit comments

Comments
 (0)