summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/StorageUseCheck.java
blob: 2a5f1b1a8cb56b03945a2c6a971367f2f4dbb07b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package org.openslx.bwlp.sat;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
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, StandardCharsets.UTF_8);
			} 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, StandardCharsets.UTF_8);
			} 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, StandardCharsets.UTF_8);
				} catch (IOException e) {
				}
				System.exit(1);
			}
		}
	}

	@Override
	public void fire() {
		createCanary();
	}

}