Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Fixing reloading of watchOperations in daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
pimotte committed Aug 3, 2014
1 parent 5d405d4 commit 1c9ba58
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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());
}
Expand All @@ -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());
Expand Down
Expand Up @@ -73,7 +73,9 @@ public void reload(DaemonConfigTO daemonConfigTO) {
try {
Map<File, FolderTO> 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) {
Expand Down Expand Up @@ -122,16 +124,27 @@ private void startWatchOperations(Map<File, FolderTO> newWatchedFolderTOs) throw
}
}

private void stopWatchOperations(Set<File> 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<File, WatchRunner> watchOperationsCopy = new TreeMap<File, WatchRunner>(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);
}
}

Expand All @@ -147,34 +160,6 @@ private Map<File, FolderTO> getFolderMap(List<FolderTO> watchedFolders) {
return watchedFolderTOs;
}

private Map<File, FolderTO> determineNewWatchedFolderTOs(Map<File, FolderTO> watchedFolders) {
Map<File, FolderTO> newWatchedFolderTOs = new TreeMap<File, FolderTO>();

for (Map.Entry<File, FolderTO> folderEntry : watchedFolders.entrySet()) {
File localDir = folderEntry.getKey();
boolean isManaged = watchOperations.containsKey(localDir);

if (!isManaged) {
newWatchedFolderTOs.put(folderEntry.getKey(), folderEntry.getValue());
}
}

return newWatchedFolderTOs;
}

private List<File> determineRemovedWatchedFolderIds(Map<File, FolderTO> watchedFolders) {
List<File> removedWatchedFolderIds = new ArrayList<File>();

for (File localDir : watchOperations.keySet()) {
boolean isInConfig = watchedFolders.containsKey(localDir);

if (!isInConfig) {
removedWatchedFolderIds.add(localDir);
}
}

return removedWatchedFolderIds;
}

@Subscribe
public void onRequestReceived(Request request) {
Expand Down

0 comments on commit 1c9ba58

Please sign in to comment.