[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [onionoo/master] Use a lock file to prevent concurrent executions.
commit 92f50e1eae8765d54d4163be0a61f40b15837689
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Wed Sep 11 16:47:15 2013 +0200
Use a lock file to prevent concurrent executions.
---
src/org/torproject/onionoo/LockFile.java | 46 ++++++++++++++++++++++++++++++
src/org/torproject/onionoo/Main.java | 18 ++++++++++++
2 files changed, 64 insertions(+)
diff --git a/src/org/torproject/onionoo/LockFile.java b/src/org/torproject/onionoo/LockFile.java
new file mode 100644
index 0000000..aad7076
--- /dev/null
+++ b/src/org/torproject/onionoo/LockFile.java
@@ -0,0 +1,46 @@
+/* Copyright 2013 The Tor Project
+ * See LICENSE for licensing information */
+package org.torproject.onionoo;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+public class LockFile {
+
+ private File lockFile;
+
+ public LockFile(File lockFile) {
+ this.lockFile = lockFile;
+ }
+
+ public boolean acquireLock() {
+ try {
+ if (this.lockFile.exists()) {
+ return false;
+ }
+ if (this.lockFile.getParentFile() != null) {
+ this.lockFile.getParentFile().mkdirs();
+ }
+ BufferedWriter bw = new BufferedWriter(new FileWriter(
+ this.lockFile));
+ bw.append("" + System.currentTimeMillis() + "\n");
+ bw.close();
+ return true;
+ } catch (IOException e) {
+ System.err.println("Caught exception while trying to acquire "
+ + "lock!");
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ public boolean releaseLock() {
+ if (this.lockFile.exists()) {
+ this.lockFile.delete();
+ }
+ return !this.lockFile.exists();
+ }
+}
+
diff --git a/src/org/torproject/onionoo/Main.java b/src/org/torproject/onionoo/Main.java
index 0d29ab5..9d9852b 100644
--- a/src/org/torproject/onionoo/Main.java
+++ b/src/org/torproject/onionoo/Main.java
@@ -11,6 +11,16 @@ public class Main {
public static void main(String[] args) {
printStatus("Initializing.");
+
+ LockFile lf = new LockFile(new File("lock"));
+ if (lf.acquireLock()) {
+ printStatusTime("Acquired lock");
+ } else {
+ printStatusTime("Could not acquire lock. Is Onionoo already "
+ + "running? Terminating");
+ return;
+ }
+
DescriptorSource dso = new DescriptorSource(new File("in"),
new File("status"));
printStatusTime("Initialized descriptor source");
@@ -122,6 +132,14 @@ public class Main {
printStatistics("Reverse domain name resolver",
rdnr.getStatsString());
+ printStatus("Releasing lock.");
+ if (lf.releaseLock()) {
+ printStatusTime("Released lock");
+ } else {
+ printStatusTime("Could not release lock. The next execution "
+ + "may not start as expected");
+ }
+
printStatus("Terminating.");
}
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits