From 642e6e67b3c9eefd37c03807be87d14d8554621e Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 25 Jun 2019 21:43:07 +0100 Subject: [PATCH] wait for all files loaded before watching dirs --- core/src/main/groovy/com/muwire/core/Core.groovy | 3 ++- .../groovy/com/muwire/core/files/DirectoryWatcher.groovy | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 2a11b72b..c571a5b7 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -27,6 +27,7 @@ import com.muwire.core.files.FileSharedEvent import com.muwire.core.files.FileUnsharedEvent import com.muwire.core.files.HasherService import com.muwire.core.files.PersisterService +import com.muwire.core.files.AllFilesLoadedEvent import com.muwire.core.files.DirectoryWatcher import com.muwire.core.hostcache.CacheClient import com.muwire.core.hostcache.HostCache @@ -236,6 +237,7 @@ public class Core { log.info("initializing directory watcher") directoryWatcher = new DirectoryWatcher(eventBus, fileManager) eventBus.register(FileSharedEvent.class, directoryWatcher) + eventBus.register(AllFilesLoadedEvent.class, directoryWatcher) log.info("initializing hasher service") hasherService = new HasherService(new FileHasher(), eventBus, fileManager) @@ -244,7 +246,6 @@ public class Core { public void startServices() { hasherService.start() - directoryWatcher.start() trustService.start() trustService.waitForLoad() hostCache.start() diff --git a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy index 3c02d2e4..a49b4c0a 100644 --- a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy @@ -47,7 +47,7 @@ class DirectoryWatcher { publisherThread.setDaemon(true) } - void start() { + void onAllFilesLoadedEvent(AllFilesLoadedEvent e) { watchService = FileSystems.getDefault().newWatchService() watcherThread.start() publisherThread.start() @@ -55,9 +55,9 @@ class DirectoryWatcher { void stop() { shutdown = true - watcherThread.interrupt() - publisherThread.interrupt() - watchService.close() + watcherThread?.interrupt() + publisherThread?.interrupt() + watchService?.close() } void onFileSharedEvent(FileSharedEvent e) {