mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-16 05:22:22 +00:00
improving cross-linking between WorkUnit and Executor
git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@34614 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
0cee4ef199
commit
40d4c2d76a
5 files changed
+50
-9
No files matched your search
@@ -1010,6 +1010,7 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A
|
||||
|
||||
public List<SubTask> getSubTasks() {
|
||||
List<SubTask> r = new ArrayList<SubTask>();
|
||||
r.add(this);
|
||||
for (SubTaskContributor euc : SubTaskContributor.all())
|
||||
r.addAll(euc.forProject(this));
|
||||
for (JobProperty<? super P> p : properties)
|
||||
|
||||
@@ -108,6 +108,7 @@ public class Executor extends Thread implements ModelObject {
|
||||
try {
|
||||
synchronized (queue) {// perform this state change as an atomic operation wrt other queue operations
|
||||
workUnit = grabJob();
|
||||
workUnit.setExecutor(this);
|
||||
task = workUnit.work;
|
||||
startTime = System.currentTimeMillis();
|
||||
executable = task.createExecutable();
|
||||
|
||||
@@ -44,11 +44,27 @@ public final class WorkUnit {
|
||||
*/
|
||||
public final WorkUnitContext context;
|
||||
|
||||
private Executor executor;
|
||||
|
||||
WorkUnit(WorkUnitContext context, SubTask work) {
|
||||
this.context = context;
|
||||
this.work = work;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Executor} running this work unit.
|
||||
* <p>
|
||||
* {@link Executor#getCurrentWorkUnit()} and {@link WorkUnit#getExecutor()}
|
||||
* form a bi-directional reachability between them.
|
||||
*/
|
||||
public Executor getExecutor() {
|
||||
return executor;
|
||||
}
|
||||
|
||||
public void setExecutor(Executor e) {
|
||||
executor = e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is this work unit the "main work", which is the primary {@link SubTask}
|
||||
* represented by {@link Task} itself.
|
||||
|
||||
@@ -29,6 +29,8 @@ import hudson.model.Queue;
|
||||
import hudson.model.Queue.BuildableItem;
|
||||
import hudson.model.Queue.Task;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -37,7 +39,6 @@ import java.util.List;
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public final class WorkUnitContext {
|
||||
private final int workUnitSize;
|
||||
|
||||
public final BuildableItem item;
|
||||
|
||||
@@ -55,6 +56,8 @@ public final class WorkUnitContext {
|
||||
|
||||
private final Latch startLatch, endLatch;
|
||||
|
||||
private List<WorkUnit> workUnits = new ArrayList<WorkUnit>();
|
||||
|
||||
public WorkUnitContext(BuildableItem item) {
|
||||
this.item = item;
|
||||
this.task = item.task;
|
||||
@@ -62,7 +65,7 @@ public final class WorkUnitContext {
|
||||
this.actions = item.getActions();
|
||||
|
||||
// +1 for the main task
|
||||
workUnitSize = task.getSubTasks().size();
|
||||
int workUnitSize = task.getSubTasks().size();
|
||||
startLatch = new Latch(workUnitSize) {
|
||||
@Override
|
||||
protected void onCriteriaMet() {
|
||||
@@ -84,7 +87,17 @@ public final class WorkUnitContext {
|
||||
*/
|
||||
public WorkUnit createWorkUnit(SubTask execUnit) {
|
||||
future.addExecutor(Executor.currentExecutor());
|
||||
return new WorkUnit(this,execUnit);
|
||||
WorkUnit wu = new WorkUnit(this, execUnit);
|
||||
workUnits.add(wu);
|
||||
return wu;
|
||||
}
|
||||
|
||||
public List<WorkUnit> getWorkUnits() {
|
||||
return Collections.unmodifiableList(workUnits);
|
||||
}
|
||||
|
||||
public WorkUnit getPrimaryWorkUnit() {
|
||||
return workUnits.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,10 +23,13 @@
|
||||
*/
|
||||
package hudson.model.queue;
|
||||
|
||||
import hudson.model.AbstractBuild;
|
||||
import hudson.model.AbstractProject;
|
||||
import hudson.model.Executor;
|
||||
import hudson.model.FreeStyleBuild;
|
||||
import hudson.model.FreeStyleProject;
|
||||
import hudson.model.Queue.Executable;
|
||||
import hudson.model.Queue.Task;
|
||||
import org.jvnet.hudson.test.HudsonTestCase;
|
||||
import org.jvnet.hudson.test.TestExtension;
|
||||
|
||||
@@ -38,10 +41,8 @@ import java.util.Collections;
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class WideExecutionTest extends HudsonTestCase {
|
||||
boolean run = false;
|
||||
|
||||
@TestExtension
|
||||
public class Contributer extends SubTaskContributor {
|
||||
public static class Contributer extends SubTaskContributor {
|
||||
public Collection<? extends SubTask> forProject(final AbstractProject<?, ?> p) {
|
||||
return Collections.singleton(new AbstractSubTask() {
|
||||
private final AbstractSubTask outer = this;
|
||||
@@ -52,12 +53,21 @@ public class WideExecutionTest extends HudsonTestCase {
|
||||
}
|
||||
|
||||
public void run() {
|
||||
run = true;
|
||||
// no-op
|
||||
WorkUnitContext wuc = Executor.currentExecutor().getCurrentWorkUnit().context;
|
||||
AbstractBuild b = (AbstractBuild)wuc.getPrimaryWorkUnit().getExecutor().getCurrentExecutable();
|
||||
try {
|
||||
b.setDescription("I was here");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Task getOwnerTask() {
|
||||
return p;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return "Company of "+p.getDisplayName();
|
||||
}
|
||||
@@ -68,6 +78,6 @@ public class WideExecutionTest extends HudsonTestCase {
|
||||
public void testRun() throws Exception {
|
||||
FreeStyleProject p = createFreeStyleProject();
|
||||
FreeStyleBuild b = assertBuildStatusSuccess(p.scheduleBuild2(0));
|
||||
assertTrue(run);
|
||||
assertTrue(b.getDescription().equals("I was here"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user