Search in sources :

Example 1 with WatchmanDiagnosticEvent

use of com.facebook.buck.io.WatchmanDiagnosticEvent in project buck by facebook.

the class ProjectBuildFileParser method handleWatchmanDiagnostic.

private static void handleWatchmanDiagnostic(Path buildFile, String level, String message, BuckEventBus buckEventBus) throws IOException {
    WatchmanDiagnostic.Level watchmanDiagnosticLevel;
    switch(level) {
        // then return.
        case "debug":
            LOG.debug("%s (watchman): %s", buildFile, message);
            return;
        case "info":
            LOG.info("%s (watchman): %s", buildFile, message);
            return;
        case "warning":
            watchmanDiagnosticLevel = WatchmanDiagnostic.Level.WARNING;
            break;
        case "error":
            watchmanDiagnosticLevel = WatchmanDiagnostic.Level.ERROR;
            break;
        case "fatal":
            throw new IOException(String.format("%s: %s", buildFile, message));
        default:
            throw new RuntimeException(String.format("Unrecognized watchman diagnostic level: %s (message=%s)", level, message));
    }
    WatchmanDiagnostic watchmanDiagnostic = WatchmanDiagnostic.of(watchmanDiagnosticLevel, message);
    buckEventBus.post(new WatchmanDiagnosticEvent(watchmanDiagnostic));
}
Also used : WatchmanDiagnosticEvent(com.facebook.buck.io.WatchmanDiagnosticEvent) WatchmanDiagnostic(com.facebook.buck.io.WatchmanDiagnostic) IOException(java.io.IOException)

Example 2 with WatchmanDiagnosticEvent

use of com.facebook.buck.io.WatchmanDiagnosticEvent in project buck by facebook.

the class WatchmanWatcher method postEvents.

@SuppressWarnings("unchecked")
private void postEvents(BuckEventBus buckEventBus, FreshInstanceAction freshInstanceAction, WatchmanQuery query, WatchmanCursor cursor, AtomicBoolean filesHaveChanged) throws IOException, InterruptedException {
    try {
        Optional<? extends Map<String, ? extends Object>> queryResponse = watchmanClient.queryWithTimeout(TimeUnit.MILLISECONDS.toNanos(timeoutMillis), query.toList(cursor.get()).toArray());
        if (!queryResponse.isPresent()) {
            LOG.warn("Could not get response from Watchman for query %s within %d ms", query, timeoutMillis);
            postWatchEvent(createOverflowEvent("Query to Watchman timed out after " + timeoutMillis + "ms"));
            filesHaveChanged.set(true);
            return;
        }
        Map<String, ? extends Object> response = queryResponse.get();
        String error = (String) response.get("error");
        if (error != null) {
            // This message is not de-duplicated via WatchmanDiagnostic.
            WatchmanWatcherException e = new WatchmanWatcherException(error);
            LOG.error(e, "Error in Watchman output. Posting an overflow event to flush the caches");
            postWatchEvent(createOverflowEvent("Watchman Error occurred - " + e.getMessage()));
            throw e;
        }
        if (cursor.get().startsWith("c:")) {
            // Update the clockId
            String newCursor = Optional.ofNullable((String) response.get("clock")).orElse(Watchman.NULL_CLOCK);
            LOG.debug("Updating Watchman Cursor from %s to %s", cursor.get(), newCursor);
            cursor.set(newCursor);
        }
        String warning = (String) response.get("warning");
        if (warning != null) {
            buckEventBus.post(new WatchmanDiagnosticEvent(WatchmanDiagnostic.of(WatchmanDiagnostic.Level.WARNING, warning)));
        }
        Boolean isFreshInstance = (Boolean) response.get("is_fresh_instance");
        if (isFreshInstance != null && isFreshInstance) {
            LOG.debug("Watchman indicated a fresh instance (fresh instance action %s)", freshInstanceAction);
            switch(freshInstanceAction) {
                case NONE:
                    break;
                case POST_OVERFLOW_EVENT:
                    postWatchEvent(createOverflowEvent("New Buck instance"));
                    break;
            }
            filesHaveChanged.set(true);
            return;
        }
        List<Map<String, Object>> files = (List<Map<String, Object>>) response.get("files");
        if (files != null) {
            for (Map<String, Object> file : files) {
                String fileName = (String) file.get("name");
                if (fileName == null) {
                    LOG.warn("Filename missing from Watchman file response %s", file);
                    postWatchEvent(createOverflowEvent("Filename missing from Watchman response"));
                    filesHaveChanged.set(true);
                    return;
                }
                PathEventBuilder builder = new PathEventBuilder();
                builder.setPath(Paths.get(fileName));
                Boolean fileNew = (Boolean) file.get("new");
                if (fileNew != null && fileNew) {
                    builder.setCreationEvent();
                }
                Boolean fileExists = (Boolean) file.get("exists");
                if (fileExists != null && !fileExists) {
                    builder.setDeletionEvent();
                }
                postWatchEvent(builder.build());
            }
            if (!files.isEmpty() || freshInstanceAction == FreshInstanceAction.NONE) {
                filesHaveChanged.set(true);
            }
            LOG.debug("Posted %d Watchman events.", files.size());
        } else {
            if (freshInstanceAction == FreshInstanceAction.NONE) {
                filesHaveChanged.set(true);
            }
        }
    } catch (InterruptedException e) {
        String message = "Watchman communication interrupted";
        LOG.warn(e, message);
        // Events may have been lost, signal overflow.
        postWatchEvent(createOverflowEvent(message));
        Thread.currentThread().interrupt();
        throw e;
    } catch (IOException e) {
        String message = "I/O error talking to Watchman";
        LOG.error(e, message);
        // Events may have been lost, signal overflow.
        postWatchEvent(createOverflowEvent(message + " - " + e.getMessage()));
        throw e;
    }
}
Also used : WatchmanDiagnosticEvent(com.facebook.buck.io.WatchmanDiagnosticEvent) IOException(java.io.IOException) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with WatchmanDiagnosticEvent

use of com.facebook.buck.io.WatchmanDiagnosticEvent in project buck by facebook.

the class ProjectBuildFileParserTest method whenSubprocessReturnsWarningThenConsoleEventPublished.

@Test
public void whenSubprocessReturnsWarningThenConsoleEventPublished() throws IOException, BuildFileParseException, InterruptedException {
    // This test depends on unix utilities that don't exist on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    TestProjectBuildFileParserFactory buildFileParserFactory = new TestProjectBuildFileParserFactory(cell.getRoot(), cell.getKnownBuildRuleTypes());
    BuckEventBus buckEventBus = BuckEventBusFactory.newInstance(new FakeClock(0));
    final List<ConsoleEvent> consoleEvents = new ArrayList<>();
    final List<WatchmanDiagnosticEvent> watchmanDiagnosticEvents = new ArrayList<>();
    class EventListener {

        @Subscribe
        public void on(ConsoleEvent consoleEvent) {
            consoleEvents.add(consoleEvent);
        }

        @Subscribe
        public void on(WatchmanDiagnosticEvent event) {
            watchmanDiagnosticEvents.add(event);
        }
    }
    EventListener eventListener = new EventListener();
    buckEventBus.register(eventListener);
    try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsSuccessWithWarning(buckEventBus, "This is a warning", "parser")) {
        buildFileParser.initIfNeeded();
        buildFileParser.getAllRulesAndMetaRules(Paths.get("foo"));
    }
    assertThat(consoleEvents, Matchers.contains(Matchers.hasToString("Warning raised by BUCK file parser: This is a warning")));
    assertThat("Should not receive any watchman diagnostic events", watchmanDiagnosticEvents, Matchers.empty());
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeClock(com.facebook.buck.timing.FakeClock) WatchmanDiagnosticEvent(com.facebook.buck.io.WatchmanDiagnosticEvent) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with WatchmanDiagnosticEvent

use of com.facebook.buck.io.WatchmanDiagnosticEvent in project buck by facebook.

the class WatchmanWatcherTest method whenWatchmanProducesAWarningThenDiagnosticEventGenerated.

@Test
public void whenWatchmanProducesAWarningThenDiagnosticEventGenerated() throws IOException, InterruptedException {
    String message = "Find me!";
    ImmutableMap<String, Object> watchmanOutput = ImmutableMap.of("files", ImmutableList.of(), "warning", message);
    Capture<WatchmanDiagnosticEvent> eventCapture = newCapture();
    EventBus eventBus = new EventBus("watchman test");
    BuckEventBus buckEventBus = createStrictMock(BuckEventBus.class);
    buckEventBus.post(capture(eventCapture));
    replay(buckEventBus);
    WatchmanWatcher watcher = createWatcher(eventBus, watchmanOutput);
    watcher.postEvents(buckEventBus, WatchmanWatcher.FreshInstanceAction.NONE);
    verify(buckEventBus);
    assertThat(eventCapture.getValue().getDiagnostic().getMessage(), Matchers.containsString(message));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) WatchmanDiagnosticEvent(com.facebook.buck.io.WatchmanDiagnosticEvent) EasyMock.anyObject(org.easymock.EasyMock.anyObject) BuckEventBus(com.facebook.buck.event.BuckEventBus) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.Test)

Example 5 with WatchmanDiagnosticEvent

use of com.facebook.buck.io.WatchmanDiagnosticEvent in project buck by facebook.

the class ProjectBuildFileParserTest method whenSubprocessReturnsNewWatchmanWarningThenDiagnosticEventPublished.

@Test
public void whenSubprocessReturnsNewWatchmanWarningThenDiagnosticEventPublished() throws IOException, BuildFileParseException, InterruptedException {
    // This test depends on unix utilities that don't exist on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    TestProjectBuildFileParserFactory buildFileParserFactory = new TestProjectBuildFileParserFactory(cell.getRoot(), cell.getKnownBuildRuleTypes());
    BuckEventBus buckEventBus = BuckEventBusFactory.newInstance(new FakeClock(0));
    final List<WatchmanDiagnosticEvent> watchmanDiagnosticEvents = new ArrayList<>();
    class EventListener {

        @Subscribe
        public void on(WatchmanDiagnosticEvent consoleEvent) {
            watchmanDiagnosticEvents.add(consoleEvent);
        }
    }
    EventListener eventListener = new EventListener();
    buckEventBus.register(eventListener);
    try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsSuccessWithWarning(buckEventBus, "This is a watchman warning", "watchman")) {
        buildFileParser.initIfNeeded();
        buildFileParser.getAllRulesAndMetaRules(Paths.get("foo"));
    }
    assertThat(watchmanDiagnosticEvents, Matchers.contains(Matchers.hasToString(Matchers.containsString("This is a watchman warning"))));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeClock(com.facebook.buck.timing.FakeClock) WatchmanDiagnosticEvent(com.facebook.buck.io.WatchmanDiagnosticEvent) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

WatchmanDiagnosticEvent (com.facebook.buck.io.WatchmanDiagnosticEvent)5 BuckEventBus (com.facebook.buck.event.BuckEventBus)3 Test (org.junit.Test)3 ProjectBuildFileParser (com.facebook.buck.json.ProjectBuildFileParser)2 FakeClock (com.facebook.buck.timing.FakeClock)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ConsoleEvent (com.facebook.buck.event.ConsoleEvent)1 WatchmanDiagnostic (com.facebook.buck.io.WatchmanDiagnostic)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 EventBus (com.google.common.eventbus.EventBus)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 EasyMock.anyObject (org.easymock.EasyMock.anyObject)1