Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ class TraceFileObserver implements TraceObserverV2 {
Files.createDirectories(parent)

// create a new trace file
traceFile = new PrintWriter(TraceHelper.newFileWriter(tracePath,overwrite, 'Trace'))
try {
traceFile = new PrintWriter(TraceHelper.newFileWriter(tracePath, overwrite, 'Trace'))
}
catch (Exception e) {
log.warn "Failed to create trace file: ${tracePath.toUriString()} -- ${e.message}"
return
}

// launch the agent
writer = new Agent<PrintWriter>(traceFile)
Expand All @@ -186,12 +192,14 @@ class TraceFileObserver implements TraceObserverV2 {
log.debug "Workflow completed -- saving trace file"

// wait for termination and flush the agent content
writer.await()
writer?.await()

// write the remaining records
current.values().each { record -> traceFile.println(render(record)) }
traceFile.flush()
traceFile.close()
if( traceFile ) {
current.values().each { record -> traceFile.println(render(record)) }
traceFile.flush()
traceFile.close()
}
}

@Override
Expand All @@ -216,7 +224,7 @@ class TraceFileObserver implements TraceObserverV2 {
current.remove(taskId)

// save to the file
writer.send { PrintWriter it ->
writer?.send { PrintWriter it ->
it.println(render(event.trace))
it.flush()
}
Expand All @@ -230,7 +238,7 @@ class TraceFileObserver implements TraceObserverV2 {
}

// save to the file
writer.send { PrintWriter it ->
writer?.send { PrintWriter it ->
it.println(render(event.trace))
it.flush()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,49 @@ class TraceFileObserverTest extends Specification {

}

def 'should degrade gracefully when trace file already exists'() {

given:
def testFolder = Files.createTempDirectory('trace-dir')
def file = testFolder.resolve('trace')
file.text = 'existing content' // pre-create the file so newFileWriter fails

// the handler
def task = new TaskRun(id:TaskId.of(1), name:'test_task', hash: CacheHelper.hasher(1).hash(), config: new TaskConfig())
task.processor = Mock(TaskProcessor)
task.processor.getSession() >> new Session()
task.processor.getName() >> 'x'
task.processor.getExecutor() >> Mock(Executor)
task.processor.getProcessEnvironment() >> [:]

def handler = new NopeTaskHandler(task)
handler.status = TaskStatus.COMPLETED

// observer with overwrite=false (the default)
def config = new TraceConfig(file: file.toString())
def observer = new TraceFileObserver(config)

when: 'onFlowCreate fails to create the file'
observer.onFlowCreate(null)
then: 'writer and traceFile remain null'
observer.@writer == null
observer.@traceFile == null

when: 'task events and flow completion should not throw NPE'
observer.onTaskSubmit( new TaskEvent(handler, handler.getTraceRecord()) )
observer.onTaskComplete( new TaskEvent(handler, handler.getTraceRecord()) )
observer.onTaskCached( new TaskEvent(handler, handler.getTraceRecord()) )
observer.onFlowComplete()
then:
noExceptionThrown()

and: 'the original file content is unchanged'
file.text == 'existing content'

cleanup:
testFolder.deleteDir()
}

def 'should create a record in the trace file'() {

given:
Expand Down
Loading