blob: cabeafd2727645b061b9c4a3f009985ae106c3ac (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.process;
import com.yahoo.vespa.hosted.node.admin.component.TaskContext;
import java.nio.file.Path;
import java.util.Optional;
import java.util.logging.Logger;
/**
* Represents a forked child process that still exists or has terminated.
*
* @author hakonhall
*/
public class ChildProcessImpl implements ChildProcess {
private final TaskContext taskContext;
private final ProcessApi process;
private final String commandLine;
private Optional<String> utf8OutputCache = Optional.empty();
ChildProcessImpl(TaskContext taskContext,
Process process,
Path processOutputPath,
String commandLine) {
this(taskContext, new ProcessApiImpl(process, processOutputPath), commandLine);
}
ChildProcessImpl(TaskContext taskContext,
ProcessApi process,
String commandLine) {
this.taskContext = taskContext;
this.process = process;
this.commandLine = commandLine;
}
@Override
public String commandLine() {
return commandLine;
}
public String getUtf8Output() {
if (!utf8OutputCache.isPresent()) {
waitForTermination();
utf8OutputCache = Optional.of(process.getUtf8Output());
}
return utf8OutputCache.get();
}
public ChildProcessImpl waitForTermination() {
process.waitForTermination();
return this;
}
public int exitValue() {
waitForTermination();
return process.exitCode();
}
public ChildProcess throwIfFailed() {
waitForTermination();
int exitCode = process.exitCode();
if (exitCode != 0) {
String message = ErrorMessageFormatter.createSnippetForTerminatedProcess(
"terminated with non-zero exit code " + exitCode,
this);
throw new CommandException(message);
}
return this;
}
@Override
public void logAsModifyingSystemAfterAll(Logger logger) {
taskContext.recordSystemModification(logger, "Executed command: " + commandLine);
}
@Override
public void close() {
process.close();
}
@Override
public Path getProcessOutputPath() {
return process.getProcessOutputPath();
}
}
|