use of com.jezhumble.javasysmon.ProcessVisitor in project gocd by gocd.
the class SubprocessLogger method logSubprocess.
private void logSubprocess() {
final StringBuffer processDetails = new StringBuffer();
sysMon.visitProcessTree(sysMon.currentPid(), new ProcessVisitor() {
public boolean visit(OsProcess process, int level) {
if (level == 1) {
ProcessInfo processInfo = process.processInfo();
processDetails.append(String.format("\n\tPID: %s\tname: %s\towner: %s\tcommand: %s", processInfo.getPid(), processInfo.getName(), processInfo.getOwner(), processInfo.getCommand()));
}
return false;
}
});
if (!processDetails.toString().isEmpty()) {
LOGGER.warn(warnMessage + processDetails);
}
}
use of com.jezhumble.javasysmon.ProcessVisitor in project gocd by gocd.
the class BuildSessionCancelingTest method cancelShouldProcessOnCancelCommandOfCommandThatIsRunning.
@Test
public void cancelShouldProcessOnCancelCommandOfCommandThatIsRunning() throws InterruptedException {
final BuildSession buildSession = newBuildSession();
Thread buildingThread = new Thread(new Runnable() {
@Override
public void run() {
buildSession.build(compose(compose(execSleepScript(50).setOnCancel(echo("exec canceled")), echo("after sleep")).setOnCancel(echo("inner oncancel"))).setOnCancel(echo("outter oncancel")));
}
});
buildingThread.start();
waitUntilSubProcessExists(execSleepScriptProcessCommand(), true);
assertTrue(buildInfo(), buildSession.cancel(30, TimeUnit.SECONDS));
waitUntilSubProcessExists(execSleepScriptProcessCommand(), false);
JavaSysMon javaSysMon = new JavaSysMon();
final boolean[] exists = { false };
javaSysMon.visitProcessTree(javaSysMon.currentPid(), new ProcessVisitor() {
@Override
public boolean visit(OsProcess osProcess, int i) {
String command = osProcess.processInfo().getName();
if (execSleepScriptProcessCommand().equals(command)) {
exists[0] = true;
}
return false;
}
});
assertThat(exists[0], is(false));
assertThat(buildInfo(), getLast(statusReporter.results()), is(Cancelled));
assertThat(buildInfo(), console.output(), not(containsString("after sleep")));
assertThat(buildInfo(), console.output(), containsString("exec canceled"));
assertThat(buildInfo(), console.output(), containsString("inner oncancel"));
assertThat(buildInfo(), console.output(), containsString("outter oncancel"));
buildingThread.join();
}
use of com.jezhumble.javasysmon.ProcessVisitor in project gocd by gocd.
the class SubprocessLoggerTest method stubSysMon.
private JavaSysMon stubSysMon() {
final OsProcess process1 = mock(OsProcess.class);
when(process1.processInfo()).thenReturn(new ProcessInfo(101, 100, "command-1", "name-1", "owner-1", 100, 200, 400, 800));
final OsProcess process1a = mock(OsProcess.class);
when(process1a.processInfo()).thenReturn(new ProcessInfo(103, 100, "command-1a", "name-1a", "owner-1", 160, 260, 460, 860));
final OsProcess process2 = mock(OsProcess.class);
when(process2.processInfo()).thenReturn(new ProcessInfo(102, 101, "command-2", "name-2", "owner-1", 150, 250, 450, 850));
JavaSysMon sysMon = new JavaSysMon() {
@Override
public void visitProcessTree(int pid, ProcessVisitor processVisitor) {
processVisitor.visit(process2, 2);
processVisitor.visit(process1, 1);
processVisitor.visit(process1a, 1);
}
@Override
public int currentPid() {
return 100;
}
};
return sysMon;
}
use of com.jezhumble.javasysmon.ProcessVisitor in project gocd by gocd.
the class BuildSessionCancelingTest method subProcessNames.
private List<String> subProcessNames() {
JavaSysMon javaSysMon = new JavaSysMon();
final List<String> names = new ArrayList<>();
final int currentPid = javaSysMon.currentPid();
javaSysMon.visitProcessTree(currentPid, new ProcessVisitor() {
@Override
public boolean visit(OsProcess osProcess, int i) {
if (osProcess.processInfo().getPid() != currentPid) {
names.add(osProcess.processInfo().getName());
}
return false;
}
});
return names;
}
Aggregations