Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #39 from oleg-nenashev/JENKINS-44014
[JENKINS-44014] - Fix the reported runtime exceptions (Pipeline integration)
  • Loading branch information
oleg-nenashev committed Jun 18, 2017
2 parents ca2ba81 + ed9db55 commit d39a17b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
11 changes: 6 additions & 5 deletions src/main/java/jenkins/advancedqueue/ItemTransitionLogger.java
Expand Up @@ -25,6 +25,7 @@

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;

import jenkins.advancedqueue.sorter.ItemInfo;
import jenkins.model.Jenkins;
Expand All @@ -37,27 +38,27 @@ public class ItemTransitionLogger {

private final static Logger LOGGER = Logger.getLogger("PrioritySorter.Queue.Items");

static public void logNewItem(ItemInfo info) {
static public void logNewItem(@Nonnull ItemInfo info) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer("New Item: " + info.toString() + "\n" + info.getDescisionLog());
} else {
LOGGER.fine("New Item: " + info.toString());
}
}

static public void logBlockedItem(ItemInfo info) {
static public void logBlockedItem(@Nonnull ItemInfo info) {
LOGGER.fine("Blocking: " + info.toString());
}

static public void logBuilableItem(ItemInfo info) {
static public void logBuilableItem(@Nonnull ItemInfo info) {
LOGGER.fine("Buildable: " + info.toString());
}

static public void logStartedItem(ItemInfo info) {
static public void logStartedItem(@Nonnull ItemInfo info) {
LOGGER.fine("Starting: " + info.toString());
}

static public void logCanceledItem(ItemInfo info) {
static public void logCanceledItem(@Nonnull ItemInfo info) {
LOGGER.fine("Canceling: " + info.toString());
}

Expand Down
Expand Up @@ -3,32 +3,43 @@
import hudson.Plugin;
import hudson.model.Job;
import hudson.model.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import jenkins.advancedqueue.sorter.ItemInfo;
import jenkins.advancedqueue.sorter.QueueItemCache;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution;

class PriorityConfigurationPlaceholderTaskHelper {

private static final Logger LOGGER = Logger.getLogger(PriorityConfigurationPlaceholderTaskHelper.class.getName());

boolean isPlaceholderTask(Queue.Task task) {
return isPlaceholderTaskUsed() && task instanceof ExecutorStepExecution.PlaceholderTask;
}

PriorityConfigurationCallback getPriority(ExecutorStepExecution.PlaceholderTask task,
PriorityConfigurationCallback priorityCallback) {
Job<?, ?> job = (Job<?, ?>) task.getOwnerTask();
ItemInfo itemInfo = QueueItemCache.get().getItem(job.getName());

// Can be null if job didn't run yet

if (itemInfo != null) {
priorityCallback.setPrioritySelection(itemInfo.getPriority());
} else {
priorityCallback.setPrioritySelection(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority());
}

return priorityCallback;
}
@Nonnull
PriorityConfigurationCallback getPriority(@Nonnull ExecutorStepExecution.PlaceholderTask task, @Nonnull PriorityConfigurationCallback priorityCallback) {
Queue.Task ownerTask = task.getOwnerTask();
if (ownerTask instanceof Job<?, ?>) {
Job<?, ?> job = (Job<?, ?>) ownerTask;
ItemInfo itemInfo = QueueItemCache.get().getItem(job.getName());
if (itemInfo != null) {
priorityCallback.setPrioritySelection(itemInfo.getPriority());
} else {
priorityCallback.setPrioritySelection(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority());
}
} else {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Cannot determine priority of the Pipeline Placeholder Task {0}. Its owner task {1} is not a Job (type is {2}). " +
"Custom priority will not be set",
new Object[] {task, ownerTask, ownerTask != null ? ownerTask.getClass() : "null"});
}
priorityCallback.setPrioritySelection(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority());
}
return priorityCallback;
}

static boolean isPlaceholderTaskUsed() {
Plugin plugin = Jenkins.getInstance().getPlugin("workflow-durable-task-step");
Expand Down
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;

import jenkins.advancedqueue.PriorityConfiguration;
import jenkins.advancedqueue.PrioritySorterConfiguration;
Expand Down Expand Up @@ -130,7 +131,7 @@ private float getCalculatedWeight(BuildableItem item) {
}
}

public void onNewItem(Item item) {
public void onNewItem(@Nonnull Item item) {
final SorterStrategy prioritySorterStrategy = PrioritySorterConfiguration.get().getStrategy();
ItemInfo itemInfo = new ItemInfo(item);
PriorityConfiguration.get().getPriority(item, itemInfo);
Expand All @@ -139,14 +140,20 @@ public void onNewItem(Item item) {
logNewItem(itemInfo);
}

public void onLeft(LeftItem li) {
final SorterStrategy prioritySorterStrategy = PrioritySorterConfiguration.get().getStrategy();
public void onLeft(@Nonnull LeftItem li) {
ItemInfo itemInfo = QueueItemCache.get().removeItem(li.id);
Float weight = itemInfo.getWeight();
if (itemInfo == null) {
LOGGER.log(Level.WARNING, "Received the onLeft() notification for the item from outside the QueueItemCache: {0}. " +
"Cannot process this item, Priority Sorter Strategy will not be invoked", li);
return;
}

final SorterStrategy prioritySorterStrategy = PrioritySorterConfiguration.get().getStrategy();
if (li.isCancelled()) {
prioritySorterStrategy.onCanceledItem(li);
logCanceledItem(itemInfo);
} else {
Float weight = itemInfo.getWeight();
StartedJobItemCache.get().addItem(itemInfo, li.outcome.getPrimaryWorkUnit());
prioritySorterStrategy.onStartedItem(li, weight);
logStartedItem(itemInfo);
Expand Down
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;

/**
* Keeps track of the Queue.Items seen by the Sorter. Uses a WeakHash to store the entries that have
Expand Down Expand Up @@ -78,8 +79,10 @@ synchronized public ItemInfo getItem(int itemId) {
* Get the ItemInfo for the last knows start of this Job Name
*
* @param jobName a name of a Job
* @return the {@link ItemInfo} for the last know start of the Job
* @return the {@link ItemInfo} for the last know start of the Job.
* Can be {@code null} if job didn't run yet
*/
@CheckForNull
synchronized public ItemInfo getItem(String jobName) {
return jobName2info.get(jobName);
}
Expand All @@ -91,6 +94,7 @@ synchronized public ItemInfo addItem(ItemInfo itemInfo) {
return itemInfo;
}

@CheckForNull
synchronized public ItemInfo removeItem(int itemId) {
return item2info.remove(itemId);
}
Expand Down
Expand Up @@ -30,6 +30,7 @@

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;

import jenkins.model.Jenkins;

Expand All @@ -54,21 +55,21 @@ public SorterStrategyDescriptor getDescriptor() {
* the weight before returning
* @return the {@link SorterStrategyCallback} provided to the call must be returned
*/
public abstract SorterStrategyCallback onNewItem(Queue.Item item, SorterStrategyCallback weightCallback);
public abstract SorterStrategyCallback onNewItem(@Nonnull Queue.Item item, SorterStrategyCallback weightCallback);

/**
* Called when a {@link hudson.model.Item} leaves the queue and it is started.
*
* @param item the {@link hudson.model.LeftItem}
* @param weight the weight assigned when the item entered the queue
*/
public void onStartedItem(LeftItem item, float weight) {
public void onStartedItem(@Nonnull LeftItem item, float weight) {
}

/**
* Called when a {@link hudson.model.Item} leaves the queue and it is canceled.
*/
public void onCanceledItem(LeftItem item) {
public void onCanceledItem(@Nonnull LeftItem item) {
};

/**
Expand Down

0 comments on commit d39a17b

Please sign in to comment.