PageRenderTime 12ms CodeModel.GetById 7ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/hudson-core/src/main/java/hudson/model/queue/WorkUnit.java

http://github.com/hudson/hudson
Java | 95 lines | 34 code | 11 blank | 50 comment | 0 complexity | c03febb4ffa4a0ea4620f30da036e727 MD5 | raw file
 1/*
 2 * The MIT License
 3 *
 4 * Copyright (c) 2010, InfraDNA, Inc.
 5 *
 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
 7 * of this software and associated documentation files (the "Software"), to deal
 8 * in the Software without restriction, including without limitation the rights
 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24package hudson.model.queue;
25
26import hudson.model.Executor;
27import hudson.model.Queue;
28import hudson.model.Queue.Executable;
29import hudson.model.Queue.Task;
30import org.kohsuke.stapler.export.ExportedBean;
31
32/**
33 * Represents a unit of hand-over to {@link Executor} from {@link Queue}.
34 *
35 * @author Kohsuke Kawaguchi
36 * @since 1.377
37 */
38@ExportedBean
39public final class WorkUnit {
40    /**
41     * Task to be executed.
42     */
43    //TODO: review and check whether we can do it private
44    public final SubTask work;
45
46    /**
47     * Shared context among {@link WorkUnit}s.
48     */
49    //TODO: review and check whether we can do it private
50    public final WorkUnitContext context;
51
52    private volatile Executor executor;
53
54    WorkUnit(WorkUnitContext context, SubTask work) {
55        this.context = context;
56        this.work = work;
57    }
58
59    public SubTask getWork() {
60        return work;
61    }
62
63    public WorkUnitContext getContext() {
64        return context;
65    }
66
67    /**
68     * {@link Executor} running this work unit.
69     * <p>
70     * {@link Executor#getCurrentWorkUnit()} and {@link WorkUnit#getExecutor()}
71     * form a bi-directional reachability between them.
72     */
73    public Executor getExecutor() {
74        return executor;
75    }
76
77    public void setExecutor(Executor e) {
78        executor = e;
79    }
80
81    /**
82     * If the execution has already started, return the current executable.
83     */
84    public Executable getExecutable() {
85        return executor!=null ? executor.getCurrentExecutable() : null;
86    }
87
88    /**
89     * Is this work unit the "main work", which is the primary {@link SubTask}
90     * represented by {@link Task} itself.
91     */
92    public boolean isMainWork() {
93        return context.task==work;
94    }
95}