diff --git a/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchRunner.java b/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchRunner.java index 25f0bb5a1..2e1d38a28 100644 --- a/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchRunner.java +++ b/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchRunner.java @@ -67,6 +67,7 @@ import org.syncany.operations.watch.WatchOperation; import org.syncany.operations.watch.WatchOperationListener; import org.syncany.operations.watch.WatchOperationOptions; +import org.syncany.operations.watch.WatchOperationResult; import org.syncany.plugins.transfer.TransferManager; import org.syncany.util.StringUtil; @@ -86,6 +87,7 @@ public class WatchRunner implements WatchOperationListener { private File portFile; private Thread watchThread; private WatchOperation watchOperation; + private WatchOperationResult watchOperationResult; private LocalEventBus eventBus; private SqlDatabase localDatabase; @@ -107,11 +109,11 @@ public void start() { public void run() { try { logger.log(Level.INFO, "STARTING watch at" + config.getLocalDir()); - + watchOperationResult = null; portFile.createNewFile(); // TODO actually use this file properly, see #171 portFile.deleteOnExit(); - watchOperation.execute(); + watchOperationResult = watchOperation.execute(); logger.log(Level.INFO, "STOPPED watch at " + config.getLocalDir()); } @@ -131,6 +133,10 @@ public void stop() { watchThread = null; } + public boolean hasStopped() { + return (watchOperationResult != null); + } + @Subscribe public void onRequestReceived(WatchRequest watchRequest) { File requestRootFolder = new File(watchRequest.getRoot()); diff --git a/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchServer.java b/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchServer.java index b8fd30406..1cd47557c 100644 --- a/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchServer.java +++ b/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchServer.java @@ -73,7 +73,9 @@ public void reload(DaemonConfigTO daemonConfigTO) { try { Map watchedFolders = getFolderMap(daemonConfigTO.getFolders()); - stopWatchOperations(watchedFolders.keySet()); + // Stop all currently running operations + stopAllWatchOperations(); + // Start all operations that are present in the config. startWatchOperations(watchedFolders); } catch (Exception e) { @@ -122,16 +124,27 @@ private void startWatchOperations(Map newWatchedFolderTOs) throw } } - private void stopWatchOperations(Set removedWatchedFolderIds) { - for (File localDir : removedWatchedFolderIds) { + /** + * stopAllWatchOperations stops all watchOperations and verifies if they actually have stopped. + * + */ + private void stopAllWatchOperations() { + for (File localDir : watchOperations.keySet()) { WatchRunner watchOperationThread = watchOperations.get(localDir); - - if (watchOperationThread != null) { - logger.log(Level.INFO, "- Stopping watch operation at " + localDir + " ..."); - watchOperationThread.stop(); + + logger.log(Level.INFO, "- Stopping watch operation at " + localDir + " ..."); + watchOperationThread.stop(); + } + // Check if watch operations actually have stopped. + while (watchOperations.keySet().size() > 0) { + Map watchOperationsCopy = new TreeMap(watchOperations); + for (File localDir : watchOperationsCopy.keySet()) { + WatchRunner watchOperationThread = watchOperationsCopy.get(localDir); + if (watchOperationThread.hasStopped()) { + logger.log(Level.INFO, "- Watch operation at " + localDir + " has stopped"); + watchOperations.remove(localDir); + } } - - watchOperations.remove(localDir); } } @@ -147,34 +160,6 @@ private Map getFolderMap(List watchedFolders) { return watchedFolderTOs; } - private Map determineNewWatchedFolderTOs(Map watchedFolders) { - Map newWatchedFolderTOs = new TreeMap(); - - for (Map.Entry folderEntry : watchedFolders.entrySet()) { - File localDir = folderEntry.getKey(); - boolean isManaged = watchOperations.containsKey(localDir); - - if (!isManaged) { - newWatchedFolderTOs.put(folderEntry.getKey(), folderEntry.getValue()); - } - } - - return newWatchedFolderTOs; - } - - private List determineRemovedWatchedFolderIds(Map watchedFolders) { - List removedWatchedFolderIds = new ArrayList(); - - for (File localDir : watchOperations.keySet()) { - boolean isInConfig = watchedFolders.containsKey(localDir); - - if (!isInConfig) { - removedWatchedFolderIds.add(localDir); - } - } - - return removedWatchedFolderIds; - } @Subscribe public void onRequestReceived(Request request) {