-
Notifications
You must be signed in to change notification settings - Fork 0
/
phoenix.py
54 lines (48 loc) · 1.63 KB
/
phoenix.py
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
import subprocess
import time
import os, sys
deadTreshold = 3 #seconds
delay = 1 #seconds
state = "slave"
counter = 0
scriptPath = os.path.abspath(os.path.dirname(__file__)) + "\phoenix.py"
print("New process started - state set to slave")
print("Script path: "+scriptPath)
def readCounterValueFromBackup():
f = open("backup.txt", 'r')
counter = int(f.readline().strip("\n").split(" ")[1])
f.close()
return counter
def fileWrittenToRecently():
f = open("backup.txt", 'r')
backupTimestamp = int(f.readline().strip("\n").split(" ")[0])
f.close()
if state is "slave":
print("[SLAVE] File last written to " + str(int(time.time()) - backupTimestamp) + " seconds ago")
else:
print("[MASTER] File last written to " + str(int(time.time()) - backupTimestamp) + " seconds ago")
# check timestamp in file against current time, return true or false
return (deadTreshold >= int(time.time()) - backupTimestamp)
def storeBackup(counter):
f = open("backup.txt", 'w')
f.seek(0)
f.truncate()
f.write(str(int(time.time())) + " " + str(counter))
f.close()
if not os.path.isfile("backup.txt"):
f = open("backup.txt", 'w')
storeBackup(counter)
f.close()
# check if file has been written to recently
while True:
if not fileWrittenToRecently() and state is "slave":
print("[SLAVE] Master timed out, assumed dead - state set to master")
subprocess.Popen("start cmd /C py \"" + scriptPath +"\"", shell=True)
print("[MASTER] Tried to spawn new backup process")
state = "master"
counter = readCounterValueFromBackup()
if state is "master":
print("[MASTER] Counter value: "+str(counter))
counter = counter + 1
storeBackup(counter)
time.sleep(1)