Skip to content
Snippets Groups Projects
Commit 621fbbfc authored by Karsten Loesing's avatar Karsten Loesing
Browse files

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.
parent 9b02db9e
Branches
Tags
No related merge requests found
......@@ -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");
}
}
}
/* 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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment