我正在开发一个Jenkins构建自定义插件,其中的部分代码应在从属服务器上运行。我可以从该代码登录到Jenkins主服务器,但是只有在从服务器中运行的代码完成后,所有要记录的数据才有效地记录在一个块中。实际上,我希望在每个日志例程之后立即记录数据。
这是我的代码,基于Jenkins VirtualChannel / Callable机制(简化)在从属服务器下运行事物:
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {
ServerNode serverNode = new ServerNode(workspace,listener)
Result result = Result.SUCCESS,
VirtualChannel channel = launcher.getChannel();
try {
result = channel.call(serverNode);
}catch(Exception e)
{
}
run.setResult(result);
}
public class ServerNode implements Callable<Result,Exception>{
private final FilePath workspace;
private final TaskListener taskListener;
public ServerNode(FilePath workspace, TaskListener taskListener) throws IOException, InterruptedException {
this.workspace = workspace;
this.taskListener = taskListener;
}
@Override
public Result call() throws Exception
{
//Code executed in the slave
taskListener.getLogger().print("run some code on slave!");
taskListener.getLogger().print("run some more code on slave!");
// Do a lot more, time-consuming stuff....
//We are finally done
return Result.SUCCESS;
}
}
因此在该示例中,主服务器将记录:
run some code on slave!
run some more code on slave!
问题是一旦完成call()
,它只会在一个块中完成。