commit 621fbbfcce67e63994fe67014a56147f2b5b2299 Author: Karsten Loesing karsten.loesing@gmx.net Date: Wed Nov 16 12:09:05 2016 +0100
Stop using a lock file.
Chances of two concurrent runs are much lower since we switched to an internal scheduler. Let's remove some complexity here.
Implements #20681. --- .../java/org/torproject/onionoo/cron/Main.java | 27 --------- .../java/org/torproject/onionoo/util/LockFile.java | 67 ---------------------- 2 files changed, 94 deletions(-)
diff --git a/src/main/java/org/torproject/onionoo/cron/Main.java b/src/main/java/org/torproject/onionoo/cron/Main.java index 04b6955..5b80934 100644 --- a/src/main/java/org/torproject/onionoo/cron/Main.java +++ b/src/main/java/org/torproject/onionoo/cron/Main.java @@ -8,7 +8,6 @@ import org.torproject.onionoo.docs.DocumentStoreFactory; import org.torproject.onionoo.updater.DescriptorSource; import org.torproject.onionoo.updater.DescriptorSourceFactory; import org.torproject.onionoo.updater.StatusUpdateRunner; -import org.torproject.onionoo.util.LockFile; import org.torproject.onionoo.writer.DocumentWriterRunner;
import org.slf4j.Logger; @@ -125,7 +124,6 @@ public class Main implements Runnable {
@Override public void run() { - this.acquireLockOrExit(); this.initialize(); this.downloadDescriptors(); this.updateStatuses(); @@ -133,21 +131,6 @@ public class Main implements Runnable { this.shutDown(); this.gatherStatistics(); this.cleanUp(); - this.releaseLock(); - } - - private LockFile lf; - - private void acquireLockOrExit() { - this.log.info("Initializing."); - this.lf = new LockFile(); - if (this.lf.acquireLock()) { - this.log.info("Acquired lock"); - } else { - this.log.error("Could not acquire lock. Is Onionoo already " - + "running? Terminating"); - System.exit(1); - } }
private DescriptorSource dso; @@ -246,15 +229,5 @@ public class Main implements Runnable { DescriptorSourceFactory.setDescriptorSource(null); this.log.info("Done."); } - - private void releaseLock() { - this.log.info("Releasing lock."); - if (this.lf.releaseLock()) { - this.log.info("Released lock"); - } else { - this.log.error("Could not release lock. The next execution may " - + "not start as expected"); - } - } }
diff --git a/src/main/java/org/torproject/onionoo/util/LockFile.java b/src/main/java/org/torproject/onionoo/util/LockFile.java deleted file mode 100644 index 521b867..0000000 --- a/src/main/java/org/torproject/onionoo/util/LockFile.java +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright 2013--2016 The Tor Project - * See LICENSE for licensing information */ - -package org.torproject.onionoo.util; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; - -public class LockFile { - - private static final Logger log = LoggerFactory.getLogger( - LockFile.class); - - private final File lockFile = new File("lock"); - - /** - * Acquire the lock by writing a lock file with the current time in - * milliseconds and return whether this operation was successful. - * - * @return <code>true</code> if the lock file did not exist and writing - * that file now succeeded, <code>false</code> otherwise. - */ - public boolean acquireLock() { - Time time = TimeFactory.getTime(); - try { - if (this.lockFile.exists()) { - return false; - } - if (this.lockFile.getParentFile() != null) { - this.lockFile.getParentFile().mkdirs(); - } - } catch (SecurityException e) { - log.error("Unable to access lock file location", e); - return false; - } - - try (BufferedWriter bw = new BufferedWriter(new FileWriter( - this.lockFile))) { - bw.append(String.valueOf(time.currentTimeMillis())); - bw.newLine(); - return true; - } catch (IOException e) { - log.error("Caught exception while trying to acquire lock!", e); - return false; - } - } - - /** - * Release the lock by deleting the lock file if it exists and return - * whether the file was successfully deleted. - * - * @return <code>true</code> if the lock file does not exist anymore - * when returning, <code>false</code> otherwise. - */ - public boolean releaseLock() { - if (this.lockFile.exists()) { - this.lockFile.delete(); - } - return !this.lockFile.exists(); - } -} -