Search in sources :

Example 11 with AbortException

use of hudson.AbortException in project hudson-2.x by hudson.

the class Run method run.

protected final void run(Runner job) {
    if (result != null)
        // already built.
        return;
    StreamBuildListener listener = null;
    runner = job;
    onStartBuilding();
    try {
        // to set the state to COMPLETE in the end, even if the thread dies abnormally.
        // otherwise the queue state becomes inconsistent
        long start = System.currentTimeMillis();
        try {
            try {
                Charset charset = Computer.currentComputer().getDefaultCharset();
                this.charset = charset.name();
                // don't do buffering so that what's written to the listener
                // gets reflected to the file immediately, which can then be
                // served to the browser immediately
                OutputStream logger = new FileOutputStream(getLogFile());
                RunT build = job.getBuild();
                // Global log filters
                for (ConsoleLogFilter filter : ConsoleLogFilter.all()) {
                    logger = filter.decorateLogger((AbstractBuild) build, logger);
                }
                // Project specific log filterss
                if (project instanceof BuildableItemWithBuildWrappers && build instanceof AbstractBuild) {
                    BuildableItemWithBuildWrappers biwbw = (BuildableItemWithBuildWrappers) project;
                    for (BuildWrapper bw : biwbw.getBuildWrappersList()) {
                        logger = bw.decorateLogger((AbstractBuild) build, logger);
                    }
                }
                listener = new StreamBuildListener(logger, charset);
                listener.started(getCauses());
                RunListener.fireStarted(this, listener);
                // create a symlink from build number to ID.
                Util.createSymlink(getParent().getBuildDir(), getId(), String.valueOf(getNumber()), listener);
                setResult(job.run(listener));
                LOGGER.info(toString() + " main build action completed: " + result);
                CheckPoint.MAIN_COMPLETED.report();
            } catch (ThreadDeath t) {
                throw t;
            } catch (AbortException e) {
                // orderly abortion.
                result = Result.FAILURE;
                listener.error(e.getMessage());
                LOGGER.log(FINE, "Build " + this + " aborted", e);
            } catch (RunnerAbortedException e) {
                // orderly abortion.
                result = Result.FAILURE;
                LOGGER.log(FINE, "Build " + this + " aborted", e);
            } catch (InterruptedException e) {
                // aborted
                result = Result.ABORTED;
                listener.getLogger().println(Messages.Run_BuildAborted());
                LOGGER.log(Level.INFO, toString() + " aborted", e);
            } catch (Throwable e) {
                handleFatalBuildProblem(listener, e);
                result = Result.FAILURE;
            }
            // even if the main build fails fatally, try to run post build processing
            job.post(listener);
        } catch (ThreadDeath t) {
            throw t;
        } catch (Throwable e) {
            handleFatalBuildProblem(listener, e);
            result = Result.FAILURE;
        } finally {
            long end = System.currentTimeMillis();
            // @see HUDSON-5844
            duration = Math.max(end - start, 0);
            // advance the state.
            // the significance of doing this is that Hudson
            // will now see this build as completed.
            // things like triggering other builds requires this as pre-condition.
            // see issue #980.
            state = State.POST_PRODUCTION;
            try {
                job.cleanUp(listener);
            } catch (Exception e) {
                handleFatalBuildProblem(listener, e);
            // too late to update the result now
            }
            RunListener.fireCompleted(this, listener);
            if (listener != null)
                listener.finished(result);
            if (listener != null)
                listener.closeQuietly();
            try {
                save();
            } catch (IOException e) {
                LOGGER.log(Level.SEVERE, "Failed to save build record", e);
            }
        }
        try {
            getParent().logRotate();
        } catch (IOException e) {
            LOGGER.log(Level.SEVERE, "Failed to rotate log", e);
        } catch (InterruptedException e) {
            LOGGER.log(Level.SEVERE, "Failed to rotate log", e);
        }
    } finally {
        onEndBuilding();
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FlushProofOutputStream(hudson.util.FlushProofOutputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) ParseException(java.text.ParseException) FormException(hudson.model.Descriptor.FormException) AbortException(hudson.AbortException) IOException(java.io.IOException) ConsoleLogFilter(hudson.console.ConsoleLogFilter) FileOutputStream(java.io.FileOutputStream) BuildWrapper(hudson.tasks.BuildWrapper) AbortException(hudson.AbortException)

Example 12 with AbortException

use of hudson.AbortException in project hudson-2.x by hudson.

the class Queue method pop.

/**
 * Called by the executor to fetch something to build next.
 * <p>
 * This method blocks until a next project becomes buildable.
 */
public synchronized WorkUnit pop() throws InterruptedException {
    final Executor exec = Executor.currentExecutor();
    if (exec instanceof OneOffExecutor) {
        OneOffExecutor ooe = (OneOffExecutor) exec;
        final WorkUnit wu = ooe.getWorkUnit();
        pendings.remove(wu.context.item);
        return wu;
    }
    try {
        while (true) {
            final JobOffer offer = new JobOffer(exec);
            long sleep = -1;
            // consider myself parked
            assert !parked.containsKey(exec);
            parked.put(exec, offer);
            // reuse executor thread to do a queue maintenance.
            // at the end of this we get all the buildable jobs
            // in the buildables field.
            maintain();
            // allocate buildable jobs to executors
            Iterator<BuildableItem> itr = buildables.iterator();
            while (itr.hasNext()) {
                BuildableItem p = itr.next();
                // one last check to make sure this build is not blocked.
                if (isBuildBlocked(p.task)) {
                    itr.remove();
                    blockedProjects.put(p.task, new BlockedItem(p));
                    continue;
                }
                List<JobOffer> candidates = new ArrayList<JobOffer>(parked.size());
                for (JobOffer j : parked.values()) if (j.canTake(p.task))
                    candidates.add(j);
                MappingWorksheet ws = new MappingWorksheet(p, candidates);
                Mapping m = loadBalancer.map(p.task, ws);
                if (m == null)
                    // check if we can execute other projects
                    continue;
                // found a matching executor. use it.
                WorkUnitContext wuc = new WorkUnitContext(p);
                m.execute(wuc);
                itr.remove();
                if (!wuc.getWorkUnits().isEmpty())
                    pendings.add(p);
            }
            if (!waitingList.isEmpty()) {
                // wait until the first item in the queue is due
                sleep = peek().timestamp.getTimeInMillis() - new GregorianCalendar().getTimeInMillis();
                // avoid wait(0)
                if (sleep < 100)
                    sleep = 100;
            }
            if (sleep == -1)
                offer.event.block();
            else
                offer.event.block(sleep);
            // retract the offer object
            assert parked.get(exec) == offer;
            parked.remove(exec);
            // am I woken up because I have a project to build?
            if (offer.workUnit != null) {
                // if so, just build it
                LOGGER.fine("Pop returning " + offer.workUnit + " for " + exec.getName());
                // TODO: I think this has to be done by the last executor that leaves the pop(), not by main executor
                if (offer.workUnit.isMainWork())
                    pendings.remove(offer.workUnit.context.item);
                return offer.workUnit;
            }
        // otherwise run a queue maintenance
        }
    } finally {
        // remove myself from the parked list
        JobOffer offer = parked.remove(exec);
        if (offer != null && offer.workUnit != null) {
            // we are already assigned a project, but now we can't handle it.
            offer.workUnit.context.abort(new AbortException());
        }
        // since this executor might have been chosen for
        // maintenance, schedule another one. Worst case
        // we'll just run a pointless maintenance, and that's
        // fine.
        scheduleMaintenance();
    }
}
Also used : WorkUnitContext(hudson.model.queue.WorkUnitContext) ArrayList(java.util.ArrayList) GregorianCalendar(java.util.GregorianCalendar) Mapping(hudson.model.queue.MappingWorksheet.Mapping) MappingWorksheet(hudson.model.queue.MappingWorksheet) WorkUnit(hudson.model.queue.WorkUnit) AbortException(hudson.AbortException)

Example 13 with AbortException

use of hudson.AbortException in project hudson-2.x by hudson.

the class BuildCommand method run.

protected int run() throws Exception {
    job.checkPermission(Item.BUILD);
    ParametersAction a = null;
    if (!parameters.isEmpty()) {
        ParametersDefinitionProperty pdp = job.getProperty(ParametersDefinitionProperty.class);
        if (pdp == null)
            throw new AbortException(job.getFullDisplayName() + " is not parameterized but the -p option was specified");
        List<ParameterValue> values = new ArrayList<ParameterValue>();
        for (Entry<String, String> e : parameters.entrySet()) {
            String name = e.getKey();
            ParameterDefinition pd = pdp.getParameterDefinition(name);
            if (pd == null)
                throw new AbortException(String.format("\'%s\' is not a valid parameter. Did you mean %s?", name, EditDistance.findNearest(name, pdp.getParameterDefinitionNames())));
            values.add(pd.createValue(this, e.getValue()));
        }
        for (ParameterDefinition pd : pdp.getParameterDefinitions()) {
            if (parameters.get(pd.getName()) == null) {
                values.add(pd.getDefaultParameterValue());
            }
        }
        a = new ParametersAction(values);
    }
    Future<? extends AbstractBuild> f = job.scheduleBuild2(0, new CLICause(), a);
    if (!sync)
        return 0;
    // wait for the completion
    AbstractBuild b = f.get();
    stdout.println("Completed " + b.getFullDisplayName() + " : " + b.getResult());
    return b.getResult().ordinal;
}
Also used : ParametersDefinitionProperty(hudson.model.ParametersDefinitionProperty) ParameterValue(hudson.model.ParameterValue) AbstractBuild(hudson.model.AbstractBuild) ArrayList(java.util.ArrayList) ParametersAction(hudson.model.ParametersAction) AbortException(hudson.AbortException) ParameterDefinition(hudson.model.ParameterDefinition)

Example 14 with AbortException

use of hudson.AbortException in project hudson-2.x by hudson.

the class Latch method abort.

public synchronized void abort(Throwable cause) {
    interrupted = new AbortException();
    if (cause != null)
        interrupted.initCause(cause);
    notifyAll();
}
Also used : AbortException(hudson.AbortException)

Example 15 with AbortException

use of hudson.AbortException in project hudson-2.x by hudson.

the class JUnitResultArchiver method perform.

@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
    listener.getLogger().println(Messages.JUnitResultArchiver_Recording());
    TestResultAction action;
    final String testResults = build.getEnvironment(listener).expand(this.testResults);
    try {
        TestResult result = parse(testResults, build, launcher, listener);
        try {
            action = new TestResultAction(build, result, listener);
        } catch (NullPointerException npe) {
            throw new AbortException(Messages.JUnitResultArchiver_BadXML(testResults));
        }
        result.freeze(action);
        if (result.getPassCount() == 0 && result.getFailCount() == 0)
            throw new AbortException(Messages.JUnitResultArchiver_ResultIsEmpty());
        // TODO: Move into JUnitParser [BUG 3123310]
        List<Data> data = new ArrayList<Data>();
        if (testDataPublishers != null) {
            for (TestDataPublisher tdp : testDataPublishers) {
                Data d = tdp.getTestData(build, launcher, listener, result);
                if (d != null) {
                    data.add(d);
                }
            }
        }
        action.setData(data);
        CHECKPOINT.block();
    } catch (AbortException e) {
        if (build.getResult() == Result.FAILURE)
            // don't report confusing error message.
            return true;
        listener.getLogger().println(e.getMessage());
        build.setResult(Result.FAILURE);
        return true;
    } catch (IOException e) {
        e.printStackTrace(listener.error("Failed to archive test reports"));
        build.setResult(Result.FAILURE);
        return true;
    }
    build.getActions().add(action);
    CHECKPOINT.report();
    if (action.getResult().getFailCount() > 0)
        build.setResult(Result.UNSTABLE);
    return true;
}
Also used : ArrayList(java.util.ArrayList) Data(hudson.tasks.junit.TestResultAction.Data) IOException(java.io.IOException) AbortException(hudson.AbortException)

Aggregations

AbortException (hudson.AbortException)37 IOException (java.io.IOException)13 FilePath (hudson.FilePath)11 EnvVars (hudson.EnvVars)5 ArrayList (java.util.ArrayList)5 Node (hudson.model.Node)4 ArgumentListBuilder (hudson.util.ArgumentListBuilder)4 Launcher (hudson.Launcher)3 Computer (hudson.model.Computer)3 Run (hudson.model.Run)3 WorkspaceList (hudson.slaves.WorkspaceList)3 ServletException (javax.servlet.ServletException)3 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)2 AbstractBuild (hudson.model.AbstractBuild)2 ParameterDefinition (hudson.model.ParameterDefinition)2 ParameterValue (hudson.model.ParameterValue)2 PollingResult (hudson.scm.PollingResult)2 SCMRevisionState (hudson.scm.SCMRevisionState)2 File (java.io.File)2 Authentication (org.acegisecurity.Authentication)2