Search in sources :

Example 1 with PollingResult

use of hudson.scm.PollingResult in project workflow-job-plugin by jenkinsci.

the class WorkflowJob method poll.

@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", justification = "TODO 1.653+ switch to Jenkins.getInstanceOrNull")
@Override
public PollingResult poll(TaskListener listener) {
    if (!isBuildable()) {
        listener.getLogger().println("Build disabled");
        return PollingResult.NO_CHANGES;
    }
    // TODO 2.11+ call SCMDecisionHandler
    // TODO call SCMPollListener
    WorkflowRun lastBuild = getLastBuild();
    if (lastBuild == null) {
        listener.getLogger().println("no previous build to compare to");
        // Note that we have no equivalent of AbstractProject.NoSCM because without an initial build we do not know if this project has any SCM at all.
        return Queue.getInstance().contains(this) ? PollingResult.NO_CHANGES : PollingResult.BUILD_NOW;
    }
    WorkflowRun perhapsCompleteBuild = getLastSuccessfulBuild();
    if (perhapsCompleteBuild == null) {
        perhapsCompleteBuild = lastBuild;
    }
    if (pollingBaselines == null) {
        pollingBaselines = new ConcurrentHashMap<>();
    }
    PollingResult result = PollingResult.NO_CHANGES;
    for (WorkflowRun.SCMCheckout co : perhapsCompleteBuild.checkouts(listener)) {
        if (!co.scm.supportsPolling()) {
            listener.getLogger().println("polling not supported from " + co.workspace + " on " + co.node);
            continue;
        }
        String key = co.scm.getKey();
        SCMRevisionState pollingBaseline = pollingBaselines.get(key);
        if (pollingBaseline == null) {
            // after a restart, transient cache will be empty
            pollingBaseline = co.pollingBaseline;
        }
        if (pollingBaseline == null) {
            listener.getLogger().println("no polling baseline in " + co.workspace + " on " + co.node);
            continue;
        }
        try {
            FilePath workspace;
            Launcher launcher;
            WorkspaceList.Lease lease;
            if (co.scm.requiresWorkspaceForPolling()) {
                Jenkins j = Jenkins.getInstance();
                if (j == null) {
                    listener.error("Jenkins is shutting down");
                    continue;
                }
                Computer c = j.getComputer(co.node);
                if (c == null) {
                    listener.error("no such computer " + co.node);
                    continue;
                }
                workspace = new FilePath(c.getChannel(), co.workspace);
                launcher = workspace.createLauncher(listener).decorateByEnv(getEnvironment(c.getNode(), listener));
                lease = c.getWorkspaceList().acquire(workspace, !isConcurrentBuild());
            } else {
                workspace = null;
                launcher = null;
                lease = null;
            }
            PollingResult r;
            try {
                r = co.scm.compareRemoteRevisionWith(this, launcher, workspace, listener, pollingBaseline);
                if (r.remote != null) {
                    pollingBaselines.put(key, r.remote);
                }
            } finally {
                if (lease != null) {
                    lease.release();
                }
            }
            if (r.change.compareTo(result.change) > 0) {
                // note that if we are using >1 checkout, we can clobber baseline/remote here; anyway SCMTrigger only calls hasChanges()
                result = r;
            }
        } catch (AbortException x) {
            listener.error("polling failed in " + co.workspace + " on " + co.node + ": " + x.getMessage());
        } catch (Exception x) {
            // TODO 2.43+ use Functions.printStackTrace
            listener.error("polling failed in " + co.workspace + " on " + co.node).println(Functions.printThrowable(x).trim());
        }
    }
    return result;
}
Also used : FilePath(hudson.FilePath) WorkspaceList(hudson.slaves.WorkspaceList) ServletException(javax.servlet.ServletException) AbortException(hudson.AbortException) IOException(java.io.IOException) Jenkins(jenkins.model.Jenkins) SCMRevisionState(hudson.scm.SCMRevisionState) PollingResult(hudson.scm.PollingResult) Computer(hudson.model.Computer) Launcher(hudson.Launcher) AbortException(hudson.AbortException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with PollingResult

use of hudson.scm.PollingResult in project hudson-2.x by hudson.

the class AbstractProject method poll.

/**
 * Checks if there's any update in SCM, and returns true if any is found.
 *
 * <p>
 * The implementation is responsible for ensuring mutual exclusion between polling and builds
 * if necessary.
 *
 * @since 1.345
 */
public PollingResult poll(TaskListener listener) {
    SCM scm = getScm();
    if (scm == null) {
        listener.getLogger().println(Messages.AbstractProject_NoSCM());
        return NO_CHANGES;
    }
    if (isDisabled()) {
        listener.getLogger().println(Messages.AbstractProject_Disabled());
        return NO_CHANGES;
    }
    R lb = getLastBuild();
    if (lb == null) {
        listener.getLogger().println(Messages.AbstractProject_NoBuilds());
        return isInQueue() ? NO_CHANGES : BUILD_NOW;
    }
    if (pollingBaseline == null) {
        // if we have a persisted baseline, we'll find it by this
        R success = getLastSuccessfulBuild();
        for (R r = lb; r != null; r = r.getPreviousBuild()) {
            SCMRevisionState s = r.getAction(SCMRevisionState.class);
            if (s != null) {
                pollingBaseline = s;
                break;
            }
            // searched far enough
            if (r == success)
                break;
        }
    // NOTE-NO-BASELINE:
    // if we don't have baseline yet, it means the data is built by old Hudson that doesn't set the baseline
    // as action, so we need to compute it. This happens later.
    }
    try {
        if (scm.requiresWorkspaceForPolling()) {
            // lock the workspace of the last build
            FilePath ws = lb.getWorkspace();
            if (ws == null || !ws.exists()) {
                // workspace offline. build now, or nothing will ever be built
                Label label = getAssignedLabel();
                if (label != null && label.isSelfLabel()) {
                    // if the build is fixed on a node, then attempting a build will do us
                    // no good. We should just wait for the slave to come back.
                    listener.getLogger().println(Messages.AbstractProject_NoWorkspace());
                    return NO_CHANGES;
                }
                listener.getLogger().println(ws == null ? Messages.AbstractProject_WorkspaceOffline() : Messages.AbstractProject_NoWorkspace());
                if (isInQueue()) {
                    listener.getLogger().println(Messages.AbstractProject_AwaitingBuildForWorkspace());
                    return NO_CHANGES;
                } else {
                    listener.getLogger().println(Messages.AbstractProject_NewBuildForWorkspace());
                    return BUILD_NOW;
                }
            } else {
                Node node = lb.getBuiltOn();
                if (node == null || node.toComputer() == null) {
                    LOGGER.log(Level.FINE, "Node on which this job previously was built is not available now, build is started on an available node");
                    return isInQueue() ? NO_CHANGES : BUILD_NOW;
                }
                WorkspaceList l = node.toComputer().getWorkspaceList();
                // if doing non-concurrent build, acquire a workspace in a way that causes builds to block for this workspace.
                // this prevents multiple workspaces of the same job --- the behavior of Hudson < 1.319.
                // 
                // OTOH, if a concurrent build is chosen, the user is willing to create a multiple workspace,
                // so better throughput is achieved over time (modulo the initial cost of creating that many workspaces)
                // by having multiple workspaces
                WorkspaceList.Lease lease = l.acquire(ws, !concurrentBuild);
                Launcher launcher = ws.createLauncher(listener);
                try {
                    LOGGER.fine("Polling SCM changes of " + getName());
                    if (// see NOTE-NO-BASELINE above
                    pollingBaseline == null)
                        calcPollingBaseline(lb, launcher, listener);
                    PollingResult r = scm.poll(this, launcher, ws, listener, pollingBaseline);
                    pollingBaseline = r.remote;
                    return r;
                } finally {
                    lease.release();
                }
            }
        } else {
            // polling without workspace
            LOGGER.fine("Polling SCM changes of " + getName());
            if (// see NOTE-NO-BASELINE above
            pollingBaseline == null)
                calcPollingBaseline(lb, null, listener);
            PollingResult r = scm.poll(this, null, null, listener, pollingBaseline);
            pollingBaseline = r.remote;
            return r;
        }
    } catch (AbortException e) {
        listener.getLogger().println(e.getMessage());
        listener.fatalError(Messages.AbstractProject_Aborted());
        LOGGER.log(Level.FINE, "Polling " + this + " aborted", e);
        return NO_CHANGES;
    } catch (IOException e) {
        e.printStackTrace(listener.fatalError(e.getMessage()));
        return NO_CHANGES;
    } catch (InterruptedException e) {
        e.printStackTrace(listener.fatalError(Messages.AbstractProject_PollingABorted()));
        return NO_CHANGES;
    }
}
Also used : FilePath(hudson.FilePath) WorkspaceList(hudson.slaves.WorkspaceList) IOException(java.io.IOException) SC_INTERNAL_SERVER_ERROR(javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR) SCMRevisionState(hudson.scm.SCMRevisionState) PollingResult(hudson.scm.PollingResult) Launcher(hudson.Launcher) SCM(hudson.scm.SCM) NullSCM(hudson.scm.NullSCM) AbortException(hudson.AbortException)

Aggregations

AbortException (hudson.AbortException)2 FilePath (hudson.FilePath)2 Launcher (hudson.Launcher)2 PollingResult (hudson.scm.PollingResult)2 SCMRevisionState (hudson.scm.SCMRevisionState)2 WorkspaceList (hudson.slaves.WorkspaceList)2 IOException (java.io.IOException)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Computer (hudson.model.Computer)1 NullSCM (hudson.scm.NullSCM)1 SCM (hudson.scm.SCM)1 ServletException (javax.servlet.ServletException)1 SC_INTERNAL_SERVER_ERROR (javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR)1 Jenkins (jenkins.model.Jenkins)1