forked from ciren/pleiades
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.py
executable file
·193 lines (154 loc) · 5.65 KB
/
worker.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
#!/usr/bin/python2
from pysage import *
from settings import *
from messages import *
from database import *
from network import *
from subprocess import *
from traceback import *
from socket import *
import time
import uuid
import os, os.path
import sys
template = '''<?xml version='1.0'?>
<!DOCTYPE simulator [
<!ATTLIST algorithm id ID #IMPLIED>
<!ATTLIST problem id ID #IMPLIED>
<!ATTLIST measurements id ID #IMPLIED>
]>
<simulator>
<algorithms>%s</algorithms>
<problems>%s</problems>
%s
<simulations>%s</simulations>
</simulator>'''
class Worker(Actor):
subscriptions = ['JobMessage', 'NoJobMessage', 'AckResultMessage', 'KillMessage', 'RmJarMessage']
def __init__(self):
self.mgr = ActorManager.get_singleton()
self.mgr.register_actor(self)
self.mgr.connect(transport.SelectTCPTransport, host=SERVER_IP, port=SERVER_PORT)
self.mgr.broadcast_message(JobRequestMessage(msg={'allowed_users':ALLOWED_USERS}))
self.db, self.connection = mongo_connect(MONGO_RO_USER, MONGO_RO_PWD)
self.status = ''
self.running = True
def get_status_string(self):
return self.status
def handle_JobMessage(self, msg):
try:
self.status = 'job received'
sim = msg.get_property('msg')
user_id = sim['user_id']
sim_id = sim['sim_id']
job_id = sim['job_id']
current_sample = sim['sample']
print "Executing: ", user_id, job_id, sim_id, current_sample
self.status = 'getting xml'
xml_name = '%s_%i_%i_%i.xml' % (user_id, job_id, sim_id, current_sample)
meas_xml = self.db.xml.find_one({
'type': 'meas',
'idref': sim['meas'],
'job_id': job_id,
'user_id': user_id
})
prob_xml = self.db.xml.find_one({
'type': 'prob',
'idref': sim['prob'],
'job_id': job_id,
'user_id': user_id
})
algs_xml = list(self.db.xml.find({
'type':'alg',
'job_id':job_id,
'user_id':user_id
}))
sim_xml = self.db.xml.find_one({
'type':'sim',
'job_id':job_id,
'sim_id':sim_id,
'user_id':user_id
})
self.status = 'writing files'
output_file_name = str(uuid.uuid4())
with open(xml_name, 'w') as xml_file:
xml_string = template % (
'\n'.join([a['value'] for a in algs_xml]),
prob_xml['value'],
meas_xml['value'],
sim_xml['value'].replace('_output_', output_file_name)
)
xml_file.write(xml_string)
# Execute job
jar_name = '%s_%i_%i.run' % (user_id, job_id, self.gid[1])
if not os.path.exists(jar_name):
with open(jar_name, 'wb') as jar_file:
jar = get_file(job_id, user_id)
jar_file.write(jar)
self.status = 'executing'
process = Popen(['java', '-jar', jar_name, xml_name], stdout=PIPE)
while process.poll() == None:
self.status = process.stdout.read(256)
print self.status,
self.status = 'posting results'
print 'Setting up file transfer...'
sock = socket(AF_INET, SOCK_STREAM)
local_ip = get_local_ip()
sock.bind((local_ip, 0))
sock.listen(1)
# send back result
with open(output_file_name, 'r') as result_file:
result = result_file.read().encode('zlib').encode('base64')
self.mgr.broadcast_message(ResultMessage(msg={
'job_id': sim['job_id'],
'sim_id': sim['sim_id'],
'user_id': sim['user_id'],
'sample': sim['sample'],
'socket': sock.getsockname(),
'm_size': len(result),
}))
print 'Transferring result file on', sock.getsockname()
s,a = sock.accept()
sendall(s, result)
sock.close()
os.remove(xml_name)
os.remove(output_file_name)
print
except Exception, e:
print 'Worker error: ', e
print 'Stack trace:'
print_exc(file=sys.stdout)
print
#TODO: maybe send an error message to replenish the lost sample
self.mgr.broadcast_message(JobErrorMessage(msg={
'job_id': sim['job_id'],
'sim_id': sim['sim_id'],
'user_id':sim['user_id']
}))
return True
def handle_RmJarMessage(self, msg):
jar_file = msg.get_property('msg')['jar_file']
if os.path.exists(jar_file):
os.remove(jar_file)
return False
def handle_NoJobMessage(self, msg):
time.sleep(1)
self.mgr.broadcast_message(JobRequestMessage(msg={'allowed_users':ALLOWED_USERS}))
self.status = 'request resent'
return True
def handle_AckResultMessage(self, msg):
print 'ACK received'
self.mgr.broadcast_message(JobRequestMessage(msg={'allowed_users':ALLOWED_USERS}))
self.status = 'ACK Received...'
return True
def handle_KillMessage(self, msg):
self.running = False
return True
def run(self):
while self.running:
try:
self.mgr.tick()
except KeyboardInterrupt, SystemExit:
self.running = False
if __name__ == '__main__':
Worker().run()