Search in sources :

Example 1 with InterruptionFailedException

use of com.facebook.buck.util.InterruptionFailedException in project buck by facebook.

the class AdbHelper method adbCall.

/**
   * Execute an {@link AdbCallable} for all matching devices. This functions performs device
   * filtering based on three possible arguments:
   *
   *  -e (emulator-only) - only emulators are passing the filter
   *  -d (device-only) - only real devices are passing the filter
   *  -s (serial) - only device/emulator with specific serial number are passing the filter
   *
   *  If more than one device matches the filter this function will fail unless multi-install
   *  mode is enabled (-x). This flag is used as a marker that user understands that multiple
   *  devices will be used to install the apk if needed.
   */
@SuppressWarnings("PMD.EmptyCatchBlock")
@SuppressForbidden
public boolean adbCall(AdbCallable adbCallable, boolean quiet) throws InterruptedException {
    List<IDevice> devices;
    try (SimplePerfEvent.Scope ignored = SimplePerfEvent.scope(buckEventBus, "set_up_adb_call")) {
        devices = getDevices(quiet);
        if (devices.size() == 0) {
            return false;
        }
    }
    int adbThreadCount = options.getAdbThreadCount();
    if (adbThreadCount <= 0) {
        adbThreadCount = devices.size();
    }
    // Start executions on all matching devices.
    List<ListenableFuture<Boolean>> futures = Lists.newArrayList();
    ListeningExecutorService executorService = listeningDecorator(newMultiThreadExecutor(new CommandThreadFactory(getClass().getSimpleName()), adbThreadCount));
    for (final IDevice device : devices) {
        futures.add(executorService.submit(adbCallable.forDevice(device)));
    }
    // Wait for all executions to complete or fail.
    List<Boolean> results = null;
    try {
        results = Futures.allAsList(futures).get();
    } catch (ExecutionException ex) {
        console.printBuildFailure("Failed: " + adbCallable);
        ex.printStackTrace(console.getStdErr());
        return false;
    } catch (InterruptedException e) {
        try {
            Futures.allAsList(futures).cancel(true);
        } catch (CancellationException ignored) {
        // Rethrow original InterruptedException instead.
        }
        Thread.currentThread().interrupt();
        throw e;
    } finally {
        MostExecutors.shutdownOrThrow(executorService, 10, TimeUnit.MINUTES, new InterruptionFailedException("Failed to shutdown ExecutorService."));
    }
    int successCount = 0;
    for (Boolean result : results) {
        if (result) {
            successCount++;
        }
    }
    int failureCount = results.size() - successCount;
    // Report results.
    if (successCount > 0 && !quiet) {
        console.printSuccess(String.format("Successfully ran %s on %d device(s)", adbCallable, successCount));
    }
    if (failureCount > 0) {
        console.printBuildFailure(String.format("Failed to %s on %d device(s).", adbCallable, failureCount));
    }
    return failureCount == 0;
}
Also used : CommandThreadFactory(com.facebook.buck.log.CommandThreadFactory) IDevice(com.android.ddmlib.IDevice) InterruptionFailedException(com.facebook.buck.util.InterruptionFailedException) CancellationException(java.util.concurrent.CancellationException) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) ExecutionException(java.util.concurrent.ExecutionException) SuppressForbidden(com.facebook.buck.annotations.SuppressForbidden)

Example 2 with InterruptionFailedException

use of com.facebook.buck.util.InterruptionFailedException in project buck by facebook.

the class Main method runMainThenExit.

/* Define all error handling surrounding main command */
private void runMainThenExit(String[] args, Optional<NGContext> context, final long initTimestamp) {
    installUncaughtExceptionHandler(context);
    Path projectRoot = Paths.get(".");
    int exitCode = FAIL_EXIT_CODE;
    BuildId buildId = getBuildId(context);
    // Only post an overflow event if Watchman indicates a fresh instance event
    // after our initial query.
    WatchmanWatcher.FreshInstanceAction watchmanFreshInstanceAction = daemon == null ? WatchmanWatcher.FreshInstanceAction.NONE : WatchmanWatcher.FreshInstanceAction.POST_OVERFLOW_EVENT;
    // Get the client environment, either from this process or from the Nailgun context.
    ImmutableMap<String, String> clientEnvironment = getClientEnvironment(context);
    try {
        CommandMode commandMode = CommandMode.RELEASE;
        exitCode = runMainWithExitCode(buildId, projectRoot, context, clientEnvironment, commandMode, watchmanFreshInstanceAction, initTimestamp, args);
    } catch (IOException e) {
        if (e.getMessage().startsWith("No space left on device")) {
            (new Console(Verbosity.STANDARD_INFORMATION, stdOut, stdErr, new Ansi(AnsiEnvironmentChecking.environmentSupportsAnsiEscapes(platform, clientEnvironment)))).printBuildFailure(e.getMessage());
        } else {
            LOG.error(e);
        }
    } catch (HumanReadableException e) {
        Console console = new Console(Verbosity.STANDARD_INFORMATION, stdOut, stdErr, new Ansi(AnsiEnvironmentChecking.environmentSupportsAnsiEscapes(platform, clientEnvironment)));
        console.printBuildFailure(e.getHumanReadableErrorMessage());
    } catch (InterruptionFailedException e) {
        // Command could not be interrupted.
        if (context.isPresent()) {
            // Exit process to halt command execution.
            context.get().getNGServer().shutdown(true);
        }
    } catch (BuckIsDyingException e) {
        LOG.warn(e, "Fallout because buck was already dying");
    } catch (Throwable t) {
        LOG.error(t, "Uncaught exception at top level");
    } finally {
        LOG.debug("Done.");
        LogConfig.flushLogs();
        // Exit explicitly so that non-daemon threads (of which we use many) don't
        // keep the VM alive.
        System.exit(exitCode);
    }
}
Also used : ClassPath(com.google.common.reflect.ClassPath) Path(java.nio.file.Path) CommandMode(com.facebook.buck.util.environment.CommandMode) IOException(java.io.IOException) WatchmanWatcher(com.facebook.buck.util.WatchmanWatcher) InterruptionFailedException(com.facebook.buck.util.InterruptionFailedException) BuildId(com.facebook.buck.model.BuildId) BuckIsDyingException(com.facebook.buck.util.BuckIsDyingException) HumanReadableException(com.facebook.buck.util.HumanReadableException) Console(com.facebook.buck.util.Console) Ansi(com.facebook.buck.util.Ansi)

Aggregations

InterruptionFailedException (com.facebook.buck.util.InterruptionFailedException)2 IDevice (com.android.ddmlib.IDevice)1 SuppressForbidden (com.facebook.buck.annotations.SuppressForbidden)1 SimplePerfEvent (com.facebook.buck.event.SimplePerfEvent)1 CommandThreadFactory (com.facebook.buck.log.CommandThreadFactory)1 BuildId (com.facebook.buck.model.BuildId)1 Ansi (com.facebook.buck.util.Ansi)1 BuckIsDyingException (com.facebook.buck.util.BuckIsDyingException)1 Console (com.facebook.buck.util.Console)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 WatchmanWatcher (com.facebook.buck.util.WatchmanWatcher)1 CommandMode (com.facebook.buck.util.environment.CommandMode)1 ClassPath (com.google.common.reflect.ClassPath)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1