-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientAgent.py
executable file
·235 lines (194 loc) · 6.71 KB
/
clientAgent.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
#!.pyenv/bin/python
#from gevent import monkey
# monkey.patch_all()
import os
import sys
from subprocess import Popen, PIPE
from utils.general import readConfig, now, getSystemInfo, getBenchmark
from utils.client_tools import getServerUrl, connectToServer, getImageInfo, getRenderTools, MAC
import psutil
from utils.decorators import only_one
import datetime
import time
import requests
import ujson
import json
from requests import HTTPError, ConnectionError
from datetime import timedelta
import re
from copy import copy
import anydbm
CONFIG = readConfig()
# celery setup
ldbpath = 'lcache.db'
db = anydbm.open(ldbpath, 'c')
if not db.keys():
print '*' * 80
print " Seems it's first time you are running Flipfarm worker"
slaveName = raw_input(' Please Enter a name for this machine: ')
print ' Now let me benchmark "%s" for speed and performance. please wait ...' % slaveName.title()
qmark = getBenchmark()
db['slaveName'] = slaveName.title()
db['qmark'] = str(qmark)
print '*' * 80
slaveName = db['slaveName']
qmark = int(db['qmark'])
db.close()
redis_host = CONFIG.get('server').get('redis_host')
redis_port = CONFIG.get('server').get('redis_port')
BROKER_URL = 'redis://{host}:{port}/11'.format(host=redis_host,
port=int(redis_port))
CELERY_RESULT_BACKEND = 'redis://{host}:{port}/11'.\
format(host=redis_host, port=int(redis_port))
from celery import Celery
ca = Celery('clientAgent', broker=BROKER_URL, backend=CELERY_RESULT_BACKEND)
ca.config_from_object('clientAgentConfig')
def updateTaskInfo(task_id, **kw):
payload = {'_id': task_id, 'data': kw}
return connectToServer('/api/updateTask', data=payload)
def tasklog(task_id, log):
payload = {'_id': task_id, 'log': log}
return connectToServer('/api/tasklog', data=payload)
def parse_prman_output(line):
'''first lets catch errors'''
pat = re.compile(r'(R[\d]+).*\{([\w]+)\}(.*) \((.*)\)')
text = re.findall(pat, line)
if len(text):
return text[-1] # cause its a single topple
'''then try to find percentage'''
pat = re.compile(r'R[\d]*.* ([\d]+)%')
percent = re.findall(pat, line)
if percent:
return percent[-1]
def getTaskStatus(task_id):
'''Get latest task status from server'''
payload = {'_id': task_id}
return connectToServer('/api/taskStatus', data=payload)
@ca.task(name='clientAgent.execute')
#@only_one(key='happy1')
def execute(cmd, task, directory='.', target=None):
"""
Execute render and parse output and update task info
Target is final render output.
"""
#ctid = Celery.AsyncResult.task_id
stime = time.time()
tuuid = task.split('-')[0]
'''chack task status and make sure its good to continue'''
updateTaskInfo(tuuid, progress=0, started_on=now(), slave_name=slaveName)
'''Error handeling'''
if not os.path.isdir(directory):
updateTaskInfo(tuuid, status='failed', failed_on=now())
log = {
'code': 'FF001',
'type': 'ERROR',
'description': 'FlipFarm: Directory "%s" not found' % directory,
'brief': 'Directory not found'
}
tasklog(tuuid, log)
print json.dumps(log, indent=4, sort_keys=True)
return
p = psutil.Popen(cmd.split(), stdout=PIPE, stderr=PIPE,
bufsize=16, cwd=directory)
updateTaskInfo(tuuid, status='on progress', pid=p.pid)
processInfo = {
'Task': task,
'Command': cmd,
'Directory': directory,
'PID': p.pid,
'Target': target
}
print '*' * 80
print json.dumps(processInfo, indent=4, sort_keys=True)
print '*' * 80
print '\n'
has_output = False
progress = None
for line in iter(p.stderr.readline, b''):
has_output = True
result = parse_prman_output(line)
if result:
try:
'''Progressing ...'''
progress = int(result)
print '>>> {t} {prog}% completed.'.format(t=task, prog=progress),
except (TypeError, ValueError):
'''we probabaly have some error codes'''
code, typ, desc, brief = result
log = {
'code': code,
'type': typ,
'description': desc,
'brief': brief
}
print json.dumps(log, indent=4, sort_keys=True)
tasklog(tuuid, log)
if typ in ['ERROR', 'SEVERE'] and code not in []: # TODO
updateTaskInfo(tuuid, status='failed', failed_on=now())
p.kill()
return
if progress: # update every 5 percent
updateTaskInfo(tuuid, status='on progress', progress=progress)
if progress == 100: # completed
updateTaskInfo(tuuid, status='completed',
progress=100, finished_on=now())
else:
pass # for now
if not has_output:
print 'NO OUTPUT'
updateTaskInfo(tuuid, status='completed', progress=100)
p.stdout.close()
p.stderr.close()
p.wait()
if target and 'exr' in target: # for prman ##TODO
iminfo = getImageInfo(target)
updateTaskInfo(tuuid, target_info=iminfo)
# f.write('########## %s | FlipFarm Log ##########\n' % datetime.datetime.now())
# p.wait() # wait for the subprocess to exit
etime = time.time()
# f.write('\n\n{"time":%s}\n'%(etime-stime))
#update_task_status(task.split('-')[0], 'completed')
return {
'pid': p.pid,
'time': etime - stime,
'start': stime,
'end': etime,
'command': cmd,
'task': task,
'slave': slaveName
}
@ca.task(name='clientAgent.ping')
def ping():
''' This method pings server'''
systemInfo = getSystemInfo()
'''Now lets read browser database info which is send to us'''
payload = copy(systemInfo)
payload['render_tools'] = getRenderTools()
payload['os'] = sys.platform
payload['identity'] = slaveName
payload['qmark'] = qmark
payload['MAC'] = MAC
data = connectToServer(
'/api/ping', data=ujson.dumps(payload), packit=False)
if data:
return 'PONG'
@ca.task(name='clientAgent.getCommand')
def runCommand(cmd):
''' gets a command and runs it on client '''
import os
os.system(cmd)
print 'happy %s' % cmd
@ca.task(name='clientAgent.simpleTest')
def simpleTest():
print 'I am happening'
return True
if __name__ == '__main__':
'''Lets ping before start'''
argv = [
'worker',
'--loglevel=INFO',
'--hostname=%s' % (MAC),
'--concurrency=1',
'--pidfile=clientAgent.pid',
]
ca.worker_main(argv)