如何在Jenkins插件中将数据从FileCallable发送回记录器?

时间:2013-11-14 15:20:22

标签: logging jenkins hudson jenkins-plugins hudson-api

我正在使用here中的以下示例:

 void someMethod(FilePath file) {
     // make 'file' a fresh empty directory.
     file.act(new Freshen());
 }
 // if 'file' is on a different node, this FileCallable will
 // be transferred to that node and executed there.
 private static final class Freshen implements FileCallable<Void> {
     private static final long serialVersionUID = 1;
     @Override public Void invoke(File f, VirtualChannel channel) {
         // f and file represent the same thing
         f.deleteContents();
         f.mkdirs();
         return null;
     }
 }

Freshen类将被序列化并发送给从站执行。如何从我的Freshen类中获取访问权限并将进度记录到主服务器上的记录器?

2 个答案:

答案 0 :(得分:1)

  

Freshen类......并发送给奴隶执行。

不,执行将在slave上执行, FilePath表示特定从站或主站上的文件路径。see documentation

尝试使用此代码将记录器传递给Freshen(未经测试):

void someMethod(FilePath file, PrintStream logger) {
    // make 'file' a fresh empty directory.
    file.act(new Freshen(logger));
}
// if 'file' is on a different node, this FileCallable will
// be transferred to that node and executed there.
private static final class Freshen implements FileCallable<Void> {
    private static final long serialVersionUID = 1;

    private final PrintStream logger;

    public Freshen(PrintStream logger) {
        this.logger = logger;
    }

    @Override public Void invoke(File f, VirtualChannel channel) {
        // f and file represent the same thing
        logger.println("test");
        f.deleteContents();
        f.mkdirs();
        return null;
    }
}

答案 1 :(得分:1)

我参加聚会有点晚了,但我偶然发现了同样的问题并将TaskListener传递给FileCallable来解决问题:

private static final class Freshen implements FileCallable<Void> {
    private static final long serialVersionUID = 1;

    private final TaskListener listener;

    public Freshen(TaskListener listener) {
        this.listener = listener;
    }

    @Override public Void invoke(File f, VirtualChannel channel) {
        RemoteOutputStream ros = new RemoteOutputStream(listener.getLogger());
        ros.write("hello there".getBytes(StandardCharsets.UTF_8));

        return null;
    }
}