Search in sources :

Example 1 with InterruptSignalHandler

use of com.google.devtools.build.lib.server.signal.InterruptSignalHandler in project bazel by bazelbuild.

the class BlazeRuntime method serverMain.

/**
   * A main method that does not send email. The return value indicates the desired exit status of
   * the program.
   */
private static int serverMain(Iterable<BlazeModule> modules, OutErr outErr, String[] args) {
    InterruptSignalHandler sigintHandler = null;
    try {
        final RPCServer blazeServer = createBlazeRPCServer(modules, Arrays.asList(args));
        // Register the signal handler.
        sigintHandler = new InterruptSignalHandler() {

            @Override
            public void run() {
                LOG.severe("User interrupt");
                blazeServer.interrupt();
            }
        };
        blazeServer.serve();
        return ExitCode.SUCCESS.getNumericExitCode();
    } catch (OptionsParsingException e) {
        outErr.printErr(e.getMessage());
        return ExitCode.COMMAND_LINE_ERROR.getNumericExitCode();
    } catch (IOException e) {
        outErr.printErr("I/O Error: " + e.getMessage());
        return ExitCode.BUILD_FAILURE.getNumericExitCode();
    } catch (AbruptExitException e) {
        outErr.printErr(e.getMessage());
        return e.getExitCode().getNumericExitCode();
    } finally {
        if (sigintHandler != null) {
            sigintHandler.uninstall();
        }
    }
}
Also used : InterruptSignalHandler(com.google.devtools.build.lib.server.signal.InterruptSignalHandler) RPCServer(com.google.devtools.build.lib.server.RPCServer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) IOException(java.io.IOException) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException)

Example 2 with InterruptSignalHandler

use of com.google.devtools.build.lib.server.signal.InterruptSignalHandler in project bazel by bazelbuild.

the class BlazeRuntime method captureSigint.

private static void captureSigint() {
    final Thread mainThread = Thread.currentThread();
    final AtomicInteger numInterrupts = new AtomicInteger();
    final Runnable interruptWatcher = new Runnable() {

        @Override
        public void run() {
            int count = 0;
            // Not an actual infinite loop because it's run in a daemon thread.
            while (true) {
                count++;
                Uninterruptibles.sleepUninterruptibly(10, TimeUnit.SECONDS);
                LOG.warning("Slow interrupt number " + count + " in batch mode");
                ThreadUtils.warnAboutSlowInterrupt();
            }
        }
    };
    new InterruptSignalHandler() {

        @Override
        public void run() {
            LOG.info("User interrupt");
            OutErr.SYSTEM_OUT_ERR.printErrLn("Blaze received an interrupt");
            mainThread.interrupt();
            int curNumInterrupts = numInterrupts.incrementAndGet();
            if (curNumInterrupts == 1) {
                Thread interruptWatcherThread = new Thread(interruptWatcher, "interrupt-watcher");
                interruptWatcherThread.setDaemon(true);
                interruptWatcherThread.start();
            } else if (curNumInterrupts == 2) {
                LOG.warning("Second --batch interrupt: Reverting to JVM SIGINT handler");
                uninstall();
            }
        }
    };
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InterruptSignalHandler(com.google.devtools.build.lib.server.signal.InterruptSignalHandler)

Aggregations

InterruptSignalHandler (com.google.devtools.build.lib.server.signal.InterruptSignalHandler)2 RPCServer (com.google.devtools.build.lib.server.RPCServer)1 AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)1 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1