Search in sources :

Example 1 with DisableConcurrentBuildsJobProperty

use of org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty in project workflow-job-plugin by jenkinsci.

the class WorkflowJob method setConcurrentBuild.

public void setConcurrentBuild(boolean b) throws IOException {
    concurrentBuild = null;
    boolean propertyExists = getProperty(DisableConcurrentBuildsJobProperty.class) != null;
    // does not exist, we need to add the property. Yay for flipping boolean values around!
    if (propertyExists == b) {
        BulkChange bc = new BulkChange(this);
        try {
            removeProperty(DisableConcurrentBuildsJobProperty.class);
            if (!b) {
                addProperty(new DisableConcurrentBuildsJobProperty());
            }
            bc.commit();
        } finally {
            bc.abort();
        }
    }
}
Also used : DisableConcurrentBuildsJobProperty(org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty) BulkChange(hudson.BulkChange)

Example 2 with DisableConcurrentBuildsJobProperty

use of org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty in project workflow-job-plugin by jenkinsci.

the class WorkflowRun method run.

/**
 * Actually executes the workflow.
 */
@Override
public void run() {
    if (!firstTime) {
        throw sleep();
    }
    try {
        onStartBuilding();
        // cannot override getCharset, and various Run methods do not call it anyway
        charset = "UTF-8";
        BuildListener myListener = getListener();
        myListener.started(getCauses());
        Authentication auth = Jenkins.getAuthentication();
        if (!auth.equals(ACL.SYSTEM)) {
            String name = auth.getName();
            if (!auth.equals(Jenkins.ANONYMOUS)) {
                User user = User.getById(name, false);
                if (user != null) {
                    name = ModelHyperlinkNote.encodeTo(user);
                }
            }
            myListener.getLogger().println(/* hudson.model.Messages.Run_running_as_(name) */
            "Running as " + name);
        }
        RunListener.fireStarted(this, myListener);
        updateSymlinks(myListener);
        FlowDefinition definition = getParent().getDefinition();
        if (definition == null) {
            throw new AbortException("No flow definition, cannot run");
        }
        Owner owner = new Owner(this);
        FlowExecution newExecution = definition.create(owner, myListener, getAllActions());
        if (newExecution instanceof BlockableResume) {
            boolean blockResume = getParent().isResumeBlocked();
            ((BlockableResume) newExecution).setResumeBlocked(blockResume);
            if (blockResume) {
                myListener.getLogger().println("Resume disabled by user, switching to high-performance, low-durability mode.");
            }
        }
        LOGGER.fine(() -> "Running in Durability level: " + DurabilityHintProvider.suggestedFor(this.project));
        // Save before we add to the FlowExecutionList, to ensure we never have a run with a null build.
        save();
        synchronized (getMetadataGuard()) {
            // Technically safe but it makes FindBugs happy
            FlowExecutionList.get().register(owner);
            newExecution.addListener(new GraphL());
            newExecution.addListener(new NodePrintListener());
            completed = Boolean.FALSE;
            executionLoaded = true;
            execution = newExecution;
        }
        SettableFuture<FlowExecution> exec = getSettableExecutionPromise();
        if (!exec.isDone()) {
            exec.set(newExecution);
        }
        FlowExecutionListener.fireCreated(newExecution);
        // We should probably have the promise set before beginning, no?
        newExecution.start();
        FlowExecutionListener.fireRunning(newExecution);
        DisableConcurrentBuildsJobProperty dcb = getParent().getProperty(DisableConcurrentBuildsJobProperty.class);
        if (dcb != null && dcb.isAbortPrevious()) {
            WorkflowRun prev = getPreviousBuild();
            if (prev != null && prev.isBuilding()) {
                Executor e = prev.getExecutor();
                if (e != null) {
                    e.interrupt(Result.NOT_BUILT, new DisableConcurrentBuildsJobProperty.CancelledCause(this));
                }
            }
        // Not bothering to look for other older builds in progress, since once we turn this on, going forward there should be at most one.
        }
    } catch (Throwable x) {
        // ensures isInProgress returns false
        execution = null;
        executionLoaded = true;
        Executor executor = Executor.currentExecutor();
        if (Thread.interrupted() && executor != null) {
            if (LOGGER.isLoggable(Level.FINE)) {
                // In general, the exception thrown by whatever code noticed that the thread was interrupted is not useful.
                LOGGER.log(Level.FINE, this + " was interrupted during startup", x);
            }
            Result result = executor.abortResult();
            Collection<CauseOfInterruption> causes = executor.getCausesOfInterruption();
            finish(result, new FlowInterruptedException(result, causes.toArray(new CauseOfInterruption[0])));
        } else {
            finish(Result.FAILURE, x);
        }
        try {
            SettableFuture<FlowExecution> exec = getSettableExecutionPromise();
            if (!exec.isDone()) {
                exec.setException(x);
            }
        } catch (Error e) {
            if (e != x) {
                // cf. CpsThread.runNextChunk
                throw e;
            }
        }
        return;
    }
    throw sleep();
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) DisableConcurrentBuildsJobProperty(org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty) FlowExecutionOwner(org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner) User(hudson.model.User) CauseOfInterruption(jenkins.model.CauseOfInterruption) FlowInterruptedException(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) Result(hudson.model.Result) BuildListener(hudson.model.BuildListener) StreamBuildListener(hudson.model.StreamBuildListener) Executor(hudson.model.Executor) Authentication(org.acegisecurity.Authentication) FlowExecution(org.jenkinsci.plugins.workflow.flow.FlowExecution) Collection(java.util.Collection) FlowDefinition(org.jenkinsci.plugins.workflow.flow.FlowDefinition) BlockableResume(org.jenkinsci.plugins.workflow.flow.BlockableResume) AbortException(hudson.AbortException)

Aggregations

DisableConcurrentBuildsJobProperty (org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty)2 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 AbortException (hudson.AbortException)1 BulkChange (hudson.BulkChange)1 BuildListener (hudson.model.BuildListener)1 Executor (hudson.model.Executor)1 Result (hudson.model.Result)1 StreamBuildListener (hudson.model.StreamBuildListener)1 User (hudson.model.User)1 Collection (java.util.Collection)1 CauseOfInterruption (jenkins.model.CauseOfInterruption)1 Authentication (org.acegisecurity.Authentication)1 BlockableResume (org.jenkinsci.plugins.workflow.flow.BlockableResume)1 FlowDefinition (org.jenkinsci.plugins.workflow.flow.FlowDefinition)1 FlowExecution (org.jenkinsci.plugins.workflow.flow.FlowExecution)1 FlowExecutionOwner (org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner)1 FlowInterruptedException (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException)1