-
Notifications
You must be signed in to change notification settings - Fork 2
/
general_keepalive.py
104 lines (87 loc) · 3.42 KB
/
general_keepalive.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#************************************************************************#
#* *#
#* P R O J E K T A V A L O N *#
#* *#
#* general_keepalive.py Watchdog for all the other processes *#
#* *#
#* Last Change July 31, 2009; Hendrik Erckens *#
#* *#
#************************************************************************#
import os
# import sys
import time
# import signal
# DDX Access
import ddxInterface
# Init DDX Interface
store = ddxInterface.ddxStore()
current_time = 0
class Variable:
#TODO probably 'self' has to be removed in the next line
def __init__(self,name,timeout,process):
self.name = name
self.timeout = timeout
self.process = process
self.var = store.variable(name)
def check(self):
# Check age of all the variables by comparing
# their timestamps to the current time. Current time is updated
# when a timestamp appears that is newer then the old current_time.
global current_time
self.var.read()
ts = self.var.getTimeStamp()
if (self.var.getTimeStamp() > current_time):
current_time = self.var.getTimeStamp()
age = current_time - self.var.getTimeStamp()
timeout = float(self.timeout)
# TODO check processes that don't write ddxVariables (such as store and
# catalog differently with:
# system_out = os.system("killall -0 " + self.process)
return age < timeout
def restart(self):
os.system("killall -9 " + self.process)
# print "bash -c '( " + self.process + " & )' &"
# os.system("bash -c '( " + self.process + " & )' &")
# start-stop-daemon -b --start --exec flag-checker --startas
# /home/he/dokumente/ethz/fokusprojekt/segelboot/regelung/ssa/flag-checker
print "start-stop-daemon -b --start --exec " + self.process + " --startas /home/he/dokumente/ethz/fokusprojekt/segelboot/regelung/ssa/" + self.process
os.system("start-stop-daemon -b --start --exec " + self.process + " --startas /home/he/dokumente/ethz/fokusprojekt/segelboot/regelung/ssa/" + self.process)
print self.process, "has just been restarted"
# TODO add logging capability
'''
pid = os.fork()
#execv("sh " + self.process + ".sh")
if pid:
# we are the parent
signal.SIGCLD
signal.SIG_IGN
else:
# we are the child
os.system("sh " + self.process + ".sh &")
sys.exit(0)
'''
# format of config-file:
#
# rudder_state 1 ./ruddermain
# navigator 10 ./navigator
# ...
config_file = open("general_keepalive_config.txt","r")
log_file = open("general_keepalive_log.txt","r+")
lines = config_file.readlines() #.split("\n")
variables = {}
# read in the config file
for line in lines:
# remove trailing newline ('\n') and split at spaces
line = line.rstrip('\n')
list = line.split(" ")
print list
# results in: ['flags', '5', './flag-checker.cpp']
variables[list[0]] = Variable(list[0],list[1],list[2])
# initialize current time
current_time = 0
# Start the loop...
while True:
for var in variables.values():
time.sleep(0.1)
if not var.check():
var.restart()