Search in sources :

Example 1 with FileWatchEvent

use of net.rubygrapefruit.platform.file.FileWatchEvent in project gradle by gradle.

the class AbstractFileWatcherRegistryFactory method createFileWatcherRegistry.

@Override
public FileWatcherRegistry createFileWatcherRegistry(FileWatcherRegistry.ChangeHandler handler) {
    BlockingQueue<FileWatchEvent> fileEvents = new ArrayBlockingQueue<>(FILE_EVENT_QUEUE_SIZE);
    try {
        // TODO How can we avoid hard-coding ".gradle" here?
        FileWatcherProbeRegistry probeRegistry = new DefaultFileWatcherProbeRegistry(buildDir -> new File(new File(buildDir, ".gradle"), "file-system.probe"));
        W watcher = createFileWatcher(fileEvents);
        WatchableHierarchies watchableHierarchies = new WatchableHierarchies(probeRegistry, watchFilter);
        FileWatcherUpdater fileWatcherUpdater = createFileWatcherUpdater(watcher, probeRegistry, watchableHierarchies);
        return new DefaultFileWatcherRegistry(fileEventFunctions, watcher, handler, fileWatcherUpdater, fileEvents);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    }
}
Also used : FileWatcherUpdater(org.gradle.internal.watch.registry.FileWatcherUpdater) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) FileWatchEvent(net.rubygrapefruit.platform.file.FileWatchEvent) FileWatcherProbeRegistry(org.gradle.internal.watch.registry.FileWatcherProbeRegistry) File(java.io.File)

Example 2 with FileWatchEvent

use of net.rubygrapefruit.platform.file.FileWatchEvent in project gradle by gradle.

the class DefaultFileWatcherRegistry method createAndStartEventConsumerThread.

private Thread createAndStartEventConsumerThread(ChangeHandler handler) {
    Thread thread = new Thread(() -> {
        LOGGER.debug("Started listening to file system change events");
        try {
            while (consumeEvents) {
                FileWatchEvent nextEvent = fileEvents.take();
                if (!stopping) {
                    nextEvent.handleEvent(new FileWatchEvent.Handler() {

                        @Override
                        public void handleChangeEvent(FileWatchEvent.ChangeType type, String absolutePath) {
                            fileWatchingStatistics.eventReceived();
                            fileWatcherUpdater.triggerWatchProbe(absolutePath);
                            handler.handleChange(convertType(type), Paths.get(absolutePath));
                        }

                        @Override
                        public void handleUnknownEvent(String absolutePath) {
                            LOGGER.error("Received unknown event for {}", absolutePath);
                            fileWatchingStatistics.unknownEventEncountered();
                            handler.stopWatchingAfterError();
                        }

                        @Override
                        public void handleOverflow(FileWatchEvent.OverflowType type, @Nullable String absolutePath) {
                            if (absolutePath == null) {
                                LOGGER.info("Overflow detected (type: {}), invalidating all watched files", type);
                                fileWatcherUpdater.getWatchedFiles().visitRoots(watchedRoot -> handler.handleChange(OVERFLOW, Paths.get(watchedRoot)));
                            } else {
                                LOGGER.info("Overflow detected (type: {}) for watched path '{}', invalidating", type, absolutePath);
                                handler.handleChange(OVERFLOW, Paths.get(absolutePath));
                            }
                        }

                        @Override
                        public void handleFailure(Throwable failure) {
                            LOGGER.error("Error while receiving file changes", failure);
                            fileWatchingStatistics.errorWhileReceivingFileChanges(failure);
                            handler.stopWatchingAfterError();
                        }

                        @Override
                        public void handleTerminated() {
                            consumeEvents = false;
                        }
                    });
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        // stop thread
        }
        LOGGER.debug("Finished listening to file system change events");
    });
    thread.setDaemon(true);
    thread.setName("File watcher consumer");
    thread.setUncaughtExceptionHandler((failedThread, exception) -> {
        LOGGER.error("File system event consumer thread stopped due to exception", exception);
        handler.stopWatchingAfterError();
    });
    thread.start();
    return thread;
}
Also used : FileWatcherUpdater(org.gradle.internal.watch.registry.FileWatcherUpdater) FileWatcherRegistry(org.gradle.internal.watch.registry.FileWatcherRegistry) INVALIDATED(org.gradle.internal.watch.registry.FileWatcherRegistry.Type.INVALIDATED) LoggerFactory(org.slf4j.LoggerFactory) FileWatcher(net.rubygrapefruit.platform.file.FileWatcher) InterruptedIOException(java.io.InterruptedIOException) Level(java.util.logging.Level) CREATED(org.gradle.internal.watch.registry.FileWatcherRegistry.Type.CREATED) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractFileEventFunctions(net.rubygrapefruit.platform.internal.jni.AbstractFileEventFunctions) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) WatchMode(org.gradle.internal.watch.registry.WatchMode) FileSystemLocationSnapshot(org.gradle.internal.snapshot.FileSystemLocationSnapshot) Collection(java.util.Collection) REMOVED(org.gradle.internal.watch.registry.FileWatcherRegistry.Type.REMOVED) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) SnapshotHierarchy(org.gradle.internal.snapshot.SnapshotHierarchy) NativeLogger(net.rubygrapefruit.platform.internal.jni.NativeLogger) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) MODIFIED(org.gradle.internal.watch.registry.FileWatcherRegistry.Type.MODIFIED) Paths(java.nio.file.Paths) FileWatchEvent(net.rubygrapefruit.platform.file.FileWatchEvent) Optional(java.util.Optional) OVERFLOW(org.gradle.internal.watch.registry.FileWatcherRegistry.Type.OVERFLOW) FileWatchEvent(net.rubygrapefruit.platform.file.FileWatchEvent)

Aggregations

File (java.io.File)2 FileWatchEvent (net.rubygrapefruit.platform.file.FileWatchEvent)2 FileWatcherUpdater (org.gradle.internal.watch.registry.FileWatcherUpdater)2 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 Paths (java.nio.file.Paths)1 Collection (java.util.Collection)1 List (java.util.List)1 Optional (java.util.Optional)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Level (java.util.logging.Level)1 Nullable (javax.annotation.Nullable)1 FileWatcher (net.rubygrapefruit.platform.file.FileWatcher)1 AbstractFileEventFunctions (net.rubygrapefruit.platform.internal.jni.AbstractFileEventFunctions)1 NativeLogger (net.rubygrapefruit.platform.internal.jni.NativeLogger)1 FileSystemLocationSnapshot (org.gradle.internal.snapshot.FileSystemLocationSnapshot)1 SnapshotHierarchy (org.gradle.internal.snapshot.SnapshotHierarchy)1