-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction_server.py
262 lines (211 loc) · 8.04 KB
/
action_server.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/python3 -u
import MDSplus
import redis
import time
import random
import threading
import traceback
from multiprocessing import Process, Pipe
import sys
import os
import time
import signal
def closeStdout(signum, frame):
sys.stdout.flush()
sys.exit()
def execute(treeName, shot, actionPath):
signal.signal(signal.SIGTERM, closeStdout)
outFd = open(str(os.getpid())+'Log.out', 'w')
os.dup2(outFd.fileno(), 1)
statusFile = open(str(os.getpid()) + 'Status.out', 'w')
try:
tree = MDSplus.Tree(treeName, shot)
node = tree.getNode(actionPath)
task = node.getData().getTask()
print('Doing '+ actionPath)
if isinstance(task, MDSplus.Program) or isinstance(task, MDSplus.Procedure or isinstance(task, MDSplus.Routine)):
tree.tcl('do '+ actionPath)
status = 'Success'
elif isinstance(task, MDSplus.Method):
status = task.getObject().doMethod(task.getMethod())
if status == None:
status = 'Success'
else:
status = int(task.data())
if status % 2 != 0:
status = 'Success'
else:
status = 'Failure'
except Exception as exc:
status = '0'
traceback.print_exc(exc)
print('Done '+ actionPath)
statusFile.write(status)
statusFile.flush()
statusFile.close()
outFd.flush()
outFd.close()
class WorkerAction(threading.Thread):
def __init__(self, treeName, shot, actionPath, timeout, updateMutex, processHash, ident, red):
super(WorkerAction, self).__init__()
self.treeName = treeName
self.shot = shot
self.actionPath = actionPath
self.timeout = timeout
self.updateMutex = updateMutex
self.red = red
self.processHash = processHash
self.serverClass = ident
def run(self):
p = Process(target=execute, args = (self.treeName, self.shot, self.actionPath, ))
self.updateMutex.acquire()
self.red.hset('ACTION_INFO:'+self.treeName+':'+str(self.shot), self.actionPath, 'DOING')
self.processHash[self.treeName+':' + str(self.shot) + self.actionPath] = p
self.updateMutex.release()
p.start()
pid = p.pid
if self.timeout == 0:
p.join()
else:
timeoutSecs = int(self.timeout)
for i in range(timeoutSecs):
p.join(1)
if p.exitcode != None:
break
if p.exitcode == None: #not yet terminated
p.terminate()
logFile = open(str(pid) + "Log.out", "r")
log = logFile.read()
logFile.close()
print(log)
try:
statusFile = open(str(pid) + "Status.out", "r")
status = statusFile.read()
statusFile.close()
except:
status = 'Aborted'
self.updateMutex.acquire()
self.processHash[self.treeName+':' + str(self.shot) + self.actionPath] = None
self.red.publish('ACTION_DISPATCHER_PUBSUB', self.treeName +'+'+str(self.shot)+'+'+self.serverClass + '+' + self.actionPath+'+'+status+'+'+log)
self.red.hset('ACTION_INFO:'+self.treeName+':'+str(self.shot), self.actionPath, 'DONE')
self.updateMutex.release()
class WorkerCommands(threading.Thread):
def __init__(self, actionServer):
super(WorkerCommands, self).__init__()
self.actionServer = actionServer
def run(self):
self.actionServer.handleCommands()
class ActionServer:
def __init__(self, ident, serverId, red):
self.ident = ident
self.serverId = serverId
self.updPubsub = red.pubsub()
self.updPubsub.subscribe('ACTION_SERVER_PUBSUB:'+ident)
self.cmdPubsub = red.pubsub()
self.cmdPubsub.subscribe('ACTION_SERVER_COMMANDS:'+ident)
self.pendingPids = []
self.updateMutex = threading.Lock()
self.updateEvent = threading.Event()
self.red = red
self.processHash = {}
self.stopped = False
red.set('ACTION_SERVER_HEARTBEAT:'+self.ident, 0)
def handleDo(self):
if self.stopped:
return
while True:
message = self.updPubsub.get_message(timeout=100)
if message == None:
continue
if not 'data' in message.keys() or not isinstance(message['data'], bytes):
continue
msg = message['data'].decode('utf8')
if msg.upper() != 'DO':
print('INTERNAL ERROR: Wrong Message '+msg)
return
while True:
toDoMsg = self.red.lpop('ACTION_SERVER_TODO:'+self.ident)
if toDoMsg == None:
break
items = toDoMsg.split('+')
if len(items) != 3 and len(items) != 4:
print('INTERNAL ERROR. Wrong server message: '+toDoMsg)
continue
if len(items) == 3:
timeout = None
else:
timeout = int(items[3])
worker = WorkerAction(items[0], int(items[1]), items[2], timeout, self.updateMutex, self.processHash, self.ident, self.red)
worker.start()
def handleAbort(self, treeName, shot, actionPath):
self.updateMutex.acquire()
try:
p = self.processHash[treeName+':' + str(shot) + actionPath]
if p != None:
p.terminate()
except:
pass
self.updateMutex.release()
def handleRestart(self):
self.updateMutex.acquire()
for k in self.processHash.keys():
p = self.processHash[k]
if p != None:
p.terminate()
self.updateMutex.release()
self.stopped = False
def handleStop(self):
self.stopped = True
def handleHeartBeat(self):
self.red.hincrby('ACTION_SERVER_HEARTBEAT:'+self.ident, self.serverId, 1)
def handleCommands(self):
while True:
message = self.cmdPubsub.get_message(timeout=100)
if message == None:
continue
if not 'data' in message.keys() or not isinstance(message['data'], bytes):
continue
msg = message['data'].decode('utf8')
print('RECEIVED COMMAND: ', msg)
if msg.upper()[:5] != 'ABORT':
items = msg.split('+')
if len(items) != 4:
print('Internal error. Wrong command message: '+msg)
continue
self.handleAbort(items[1], int(items[2]), items[3])
elif msg.upper()[:7] == 'RESTART':
items = msg.split('+')
if len(items) != 2:
print('Internal error. Wrong command message: '+msg)
continue
if items[2] == self.serverId:
self.handleRestart()
elif msg.upper()[:4] == 'STOP':
items = msg.split('+')
if len(items) != 2:
print('Internal error. Wrong command message: '+msg)
continue
if items[2] == self.serverId:
self.handleStop()
elif msg.upper()[:9] == 'HEARTBEAT':
items = msg.split('+')
if len(items) != 2:
print('Internal error. Wrong command message: '+msg)
continue
if items[2] == self.serverId:
self.handleHeartBeat()
else:
print('INVALID MESSAGE: '+msg)
def startHandleCommands(self):
worker = WorkerCommands(self)
worker.start()
if len(sys.argv) != 3 and len(sys.argv) != 4:
print('usage: python action_server.py <server class> <server id> [redis server]')
sys.exit(0)
if len(sys.argv) == 3:
red = redis.Redis(host='localhost')
else:
red = redis.Redis(host=sys.argv[3])
act = ActionServer(sys.argv[1], sys.argv[2], red)
act.startHandleCommands()
act.handleDo()