WorkUnit.java 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 *
 * Copyright (c) 2010, InfraDNA, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25 26 27
package hudson.model.queue;

import hudson.model.Executor;
import hudson.model.Queue;
K
kohsuke 已提交
28
import hudson.model.Queue.Executable;
29
import hudson.model.Queue.Task;
30
import javax.annotation.CheckForNull;
31
import hudson.model.Run;
32 33
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
34
import org.kohsuke.stapler.export.ExportedBean;
35 36 37 38 39

/**
 * Represents a unit of hand-over to {@link Executor} from {@link Queue}.
 *
 * @author Kohsuke Kawaguchi
M
mindless 已提交
40
 * @since 1.377
41
 */
42
@ExportedBean
43 44 45 46
public final class WorkUnit {
    /**
     * Task to be executed.
     */
47
    public final SubTask work;
48 49

    /**
50
     * Shared context among {@link WorkUnit}s.
51 52 53
     */
    public final WorkUnitContext context;

K
kohsuke 已提交
54
    private volatile Executor executor;
55
    private Executable executable;
56

57
    WorkUnit(WorkUnitContext context, SubTask work) {
58
        this.context = context;
59 60 61
        this.work = work;
    }

62 63 64 65 66 67
    /**
     * {@link Executor} running this work unit.
     * <p>
     * {@link Executor#getCurrentWorkUnit()} and {@link WorkUnit#getExecutor()}
     * form a bi-directional reachability between them.
     */
68
    public @CheckForNull Executor getExecutor() {
69 70 71
        return executor;
    }

72
    public void setExecutor(@CheckForNull Executor e) {
73 74 75
        executor = e;
    }

K
kohsuke 已提交
76
    /**
77
     * If the execution has already started, return the executable that was created.
K
kohsuke 已提交
78 79
     */
    public Executable getExecutable() {
80 81 82
        return executable;
    }

83 84 85 86
    /**
     * This method is only meant to be called internally by {@link Executor}.
     */
    @Restricted(NoExternalUse.class)
87 88
    public void setExecutable(Executable executable) {
        this.executable = executable;
89 90 91
        if (executable instanceof Run) {
            ((Run) executable).setQueueId(context.item.getId());
        }
K
kohsuke 已提交
92 93
    }

94
    /**
95
     * Is this work unit the "main work", which is the primary {@link SubTask}
96 97 98
     * represented by {@link Task} itself.
     */
    public boolean isMainWork() {
99
        return context.task==work;
100
    }
K
Kohsuke Kawaguchi 已提交
101 102 103 104 105 106 107 108

    @Override
    public String toString() {
        if (work==context.task)
            return super.toString()+"[work="+context.task.getFullDisplayName()+"]";
        else
            return super.toString()+"[work="+work+",context.task="+context.task.getFullDisplayName()+"]";
    }
109
}