diff options
author | Simon Rettberg | 2015-09-11 13:42:11 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-09-11 13:42:11 +0200 |
commit | 0a59efeb3fa92156323adcd7c35ea206e88932bc (patch) | |
tree | 9550d3b94435c60380988631662c5680dcaa0ffe /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/StorageUseCheck.java | |
parent | Merge branch 'v1.1' of git.openslx.org:openslx-ng/tutor-module into v1.1 (diff) | |
download | tutor-module-0a59efeb3fa92156323adcd7c35ea206e88932bc.tar.gz tutor-module-0a59efeb3fa92156323adcd7c35ea206e88932bc.tar.xz tutor-module-0a59efeb3fa92156323adcd7c35ea206e88932bc.zip |
[server] Handle deletion/undeletion flags
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/StorageUseCheck.java')
-rw-r--r-- | dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/StorageUseCheck.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/StorageUseCheck.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/StorageUseCheck.java new file mode 100644 index 00000000..a91e6535 --- /dev/null +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/StorageUseCheck.java @@ -0,0 +1,73 @@ +package org.openslx.bwlp.sat; + +import java.io.File; +import java.io.IOException; +import java.util.UUID; + +import org.apache.commons.io.FileUtils; +import org.apache.log4j.Logger; +import org.openslx.bwlp.sat.util.Configuration; +import org.openslx.bwlp.sat.util.FileSystem; +import org.openslx.util.QuickTimer.Task; + +/** + * As it might have catastrophic results if two instances of this server operate + * on the same file storage, we write a randomly generated id to a file on the + * storage, and check it periodically. If it changed, we bail out in complete + * panic. + */ +public class StorageUseCheck extends Task { + + private static final Logger LOGGER = Logger.getLogger(StorageUseCheck.class); + + private final String uuid = UUID.randomUUID().toString(); + + private final File canary = new File(Configuration.getVmStoreProdPath(), "dozmod.lock"); + + private boolean created = false; + + public StorageUseCheck() { + if (FileSystem.waitForStorage()) { + createCanary(); + } + } + + private void createCanary() { + if (!FileSystem.isStorageMounted()) { + LOGGER.warn("Cannot check storage lock, storage not mounted"); + return; + } + if (!created || !canary.exists()) { + try { + FileUtils.write(canary, uuid); + } catch (IOException e) { + LOGGER.fatal("Cannot write lock file to VMStore", e); + System.exit(1); + } + created = true; + } else { + String canaryContents; + try { + canaryContents = FileUtils.readFileToString(canary); + } catch (IOException e) { + LOGGER.warn("Lock file cannot be accessed. Cannot ensure exclusive use of VMStore", e); + return; + } + if (!canaryContents.equals(uuid)) { + LOGGER.fatal("Lock file content changed. Another server instance is using the VMStore." + + " Will exit immediately to prevent any damages."); + try { + FileUtils.write(canary, uuid); + } catch (IOException e) { + } + System.exit(1); + } + } + } + + @Override + public void fire() { + createCanary(); + } + +} |