-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPMSServer.py
214 lines (182 loc) · 6.35 KB
/
PMSServer.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
import asyncio
import json
import sys
import websockets
from PyMS.PMSExceptions import *
class PMSTask:
def __init__(self, name, token):
self.name = name
self.token = token
class PMSServer:
def __init__(self, address, autoconnect=True):
self.websocket = None
self.address = address
self.uri = f"ws://{address}/server"
self.loop = asyncio.get_event_loop()
self.verbose = False
if autoconnect:
self.Connect()
pass
def Connect(self):
self.loop.run_until_complete(self.__async__connect())
pass
def CloseConnection(self):
self.loop.run_until_complete(self.__async__close_connection())
pass
## function to query job summary.
## Parameters:
## (string) user
def Summary(self, user):
resp = self.loop.run_until_complete(self.send_to_orchestrator({
"command": "summary",
"user": user
}))
return json.loads(resp)
## function to query job info.
## Parameters:
## (name=string) query parameters
## (filter="string1,string2,...,stringN") selected fields
def QueryJobs(self, command, **kwargs):
request = {"command": command, "match": {}, "filter": {}}
for name,value in kwargs.items():
if name == 'filter':
fields = value.split(',')
for field in fields:
request['filter'][field] = 1
else:
request['match'][name] = value
print(request)
sys.stdout.flush()
resp = self.loop.run_until_complete(self.send_to_orchestrator(request))
if command == "findJobs":
return json.loads(resp)
else:
return resp
## function to query job info.
## Parameters:
## (name=string) query parameters
## (filter="string1,string2,...,stringN") selected fields
def QueryPilots(self, command, **kwargs):
request = {"command": command, "match": {}, "filter": {}}
for name,value in kwargs.items():
if name == 'filter':
fields = value.split(',')
for field in fields:
request['filter'][field] = 1
else:
request['match'][name] = value
print(request)
sys.stdout.flush()
resp = self.loop.run_until_complete(self.send_to_orchestrator(request))
if command == "findPilots":
return json.loads(resp)
else:
return resp
## function to create a new task.
## Parameters:
## (string) taskname
def CreateTask(self, taskname):
resp = self.loop.run_until_complete( self.send_to_orchestrator({
"command": "createTask",
"task": taskname
}) )
if resp.startswith("Task"):
return PMSTask(taskname, resp.split(" ")[-1])
else:
raise TaskOperationFailed(resp)
## function to delete an existing task.
## Parameters:
## (PMSTask) task
def ClearTask(self, task):
resp = self.loop.run_until_complete( self.send_to_orchestrator({
"command": "clearTask",
"task": task.name,
"token": task.token
}) )
if resp.startswith("Task"):
return resp
else:
raise TaskOperationFailed(resp)
## function to reset failed jobs in an existing task.
## Parameters:
## (PMSTask) task
def ResetFailedJobs(self, task):
resp = self.loop.run_until_complete( self.send_to_orchestrator({
"command": "resetFailedJobs",
"task": task.name,
"token": task.token
}) )
if not "failed" in resp:
return resp
else:
raise TaskOperationFailed(resp)
## function to remove all jobs from an existing task.
## Parameters:
## (PMSTask) task
def CleanTask(self, task):
resp = self.loop.run_until_complete( self.send_to_orchestrator({
"command": "cleanTask",
"task": task.name,
"token": task.token
}) )
if resp.startswith("Task"):
return resp
else:
raise TaskOperationFailed(resp)
## function to declare a dependency between tasks
## Parameters:
## (PMSTask) task
## (string) dependsOn
def DeclareTaskDependency(self, task, dependsOn):
resp = self.loop.run_until_complete( self.send_to_orchestrator({
"command": "declareTaskDependency",
"task": task.name,
"token": task.token,
"dependsOn": dependsOn
}) )
if resp.startswith("Task"):
return resp
else:
raise TaskOperationFailed(resp)
## function to check validity of a task/token pair
## Parameters:
## (PMSTask) task
def ValidateTaskToken(self, task):
resp = self.loop.run_until_complete( self.send_to_orchestrator({
"command": "validateTaskToken",
"task": task.name,
"token": task.token,
}) )
if resp.startswith("Task/token"):
return True
elif resp.startswith("Invalid"):
return False
else:
raise TaskOperationFailed(resp)
## function to submit a new job in an existing task.
## Parameters:
## (PMSJob) job
## (PMSTask) task
## Returns:
## (string) job hash
def SubmitJob(self, job, task):
resp = self.loop.run_until_complete( self.send_to_orchestrator({
"command": "submitJob",
"job": job.job,
"task": task.name,
"token": task.token
}) )
if resp.startswith("Job received"):
return resp.split(" ")[-1]
else:
raise JobOperationFailed(resp)
async def __async__connect(self):
self.websocket = await websockets.connect(self.uri, ping_timeout = None, max_size = None)
async def __async__close_connection(self):
await self.websocket.close()
async def send_to_orchestrator(self, msg):
await self.websocket.send(json.dumps(msg))
response = await self.websocket.recv()
if self.verbose:
print(f"PMS Server replied: {response}")
return response