-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindLanguage.py
186 lines (162 loc) · 5.73 KB
/
findLanguage.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
# -*- coding: utf-8 -*-
'''
@author Julia
Script to trigger the flask server.
When the ECS can push new solutions from the student, then this script will not be needed anymore -> just trigger the /newcontainer endpoint of the backend
'''
import threading
import time
import json
import requests
import sys
import docker
running_container = {}
client = docker.from_env()
url = 'https://nfldevvipecs.rus.uni-stuttgart.de'
exercisesQueue = '/numlab/solutions/fifo'
def findLanguage(data):
'''
finds the language the exercise has
Params:
- data (dict): '{"Exercise": exercise, "Solution": solution}'
Return:
- language the exercise has
'''
lang = list(data["Exercise"]["config"].keys())[0]
if lang in ["C", "C++"]:
pass
elif lang == "Matlab":
pass
elif lang == "Octave":
pass
elif lang == "Java":
pass
elif lang == "DuMuX":
pass
elif lang == "Python":
pass
else:
print("No supported lang detected")
return lang
def createNewContainer(data):
'''
sending a POST request to the flask backend, and triggers the /newcontainer endpoint
Params:
- data (dict): '{"data": data, "receiver": receiver, "debug": debug, "language": lang}'
'''
global running_container
try:
request = requests.post('http://localhost:5001/newcontainer', data=json.dumps(data), headers = {'Content-type': 'application/json'})
running_container.update(request.json())
return request.status_code
except:
return 404
def returnExitedContainer():
'''
checks if a container exited with status code 1 -> failed, looks up the student and POST a message to the ECS that something failed
'''
global running_container
failedContainer=(client.containers.list(all=True,filters={"exited":1}))
#print(running_container)
for all in failedContainer:
#print(all.id)
if all.id in running_container:
print(all.id)
receiver = running_container[all.id]
del running_container[all.id]
#post zum receiver, dass irgendetwas fehlgeschlagen ist
def getExerciseFromExerciseUrl(exercise_url):
'''
gets the Exercise from the exercise url
Params:
- exercise_url: from a solution
Return:
- exercise
'''
try:
# TODO: security issue! exercise_url can be anything and anywhere!
request = requests.get(exercise_url, auth=(conf.username, conf.passwd), headers = {'Accept': 'application/json', 'Content-Type': 'application/json'})
return request.json()
except:
return 404
def getSolutionsFromQueue():
'''
checks if a new solution is in the solutions/fifo. -> Production System: change GET-Method to POST-Method
Return:
- None, None, None: if content-length = 0, means: "no new solution is in the solutions/fifo"
- solution, x_ecsSender, [getExerciseFromExerciseUrl(exercise_url) -> exercise from exercise url]
'''
r = requests.post(url + exercisesQueue, auth=(conf.username, conf.passwd))
if r.headers.get("Content-Length") == "0":
print("no new Solution available")
return None, None, None
else:
x_ecsSender = r.headers.get("X-EcsSender")
#try:
exercise_url = r.json().get("Solution").get("exercise")
return r.json(), x_ecsSender, getExerciseFromExerciseUrl(exercise_url)
#except:
# return None, None, None
def do_something(solution, receiver, exercise, arg):
'''
starts the needed Workflow
Params:
- solution (dict)
- receiver
- exercise
- arg (bool): debug param -> True, if container will be removed after compiling, False, if container will not be removed -> if an argument was passed while starting the script -> Debug=True, else Debug=False
'''
data = {"Exercise" : exercise.get("Exercise"), "Solution" : solution.get("Solution")}
lang = findLanguage(data)
if len(arg)== 1:
debug = True
else:
debug = False
whole_data = {"data": data, "receiver" : receiver, "debug": debug, "language":lang}
statuscode = createNewContainer(whole_data)
if statuscode != 201:
createNewContainer(whole_data)
returnExitedContainer()
print(statuscode)
if __name__ == "__main__":
'''
main method: starts for each solution from the solution queue a new thread, else if no new solution is in solutions/fifo, it waits 1 sec. and starts a new try
'''
starttime = time.time()
arg = sys.argv #from starting this script
#while(True):
d=False
i = 1
while(d==False):
# for _ in range(1):
print(i)
i = i + 1
solution, receiver, exercise = getSolutionsFromQueue()
if solution != None:
t = threading.Thread(target=do_something, args=[solution, receiver, exercise, arg])
t.start()
#do_something(solution, receiver, exercise, arg)
#t.join()
#print(time.time()-starttime)
else:
d=True
time.sleep(1.0)
# t.join()
print(time.time()-starttime)
#t.join()
print(time.time()-starttime)
starttime=time.time()
i = 1
while True:
solution, receiver, exercise = getSolutionsFromQueue()
print(receiver)
print(solution)
print(json.dumps(solution, indent=4, sort_keys=True))
print(exercise)
if solution != None:
data = {"Exercise" : exercise.get("Exercise"), "Solution" : solution.get("Solution"), "Receiver" : receiver}
do_something(solution, receiver, exercise, arg)
print(data)
print("tick" + str(i))
i += 1
time.sleep(1.0 - ((time.time() - starttime) % 1.0))