Add unregister client and other improvements

This commit is contained in:
Brett Williams
2022-10-11 21:41:12 -07:00
parent fe95a3b760
commit f20275b1b4

View File

@@ -136,6 +136,11 @@ class RenderServer:
found_jobs = sorted(found_jobs, key=lambda a: a.priority, reverse=False)
return found_jobs
@classmethod
def job_with_id(cls, job_id):
found_job = next((x for x in cls.render_queue if x.id == job_id), None)
return found_job
@classmethod
def clear_history(cls):
to_remove = [x for x in cls.render_queue if x.render_status() in [RenderStatus.CANCELLED,
@@ -263,6 +268,7 @@ class RenderServer:
stats = {"timestamp": datetime.now().isoformat(),
"platform": platform.platform(),
"cpu_percent": psutil.cpu_percent(percpu=False),
"cpu_percent_per_cpu": psutil.cpu_percent(percpu=True),
"cpu_count": psutil.cpu_count(),
"memory_total": psutil.virtual_memory().total,
"memory_available": psutil.virtual_memory().available,
@@ -299,8 +305,13 @@ class RenderServer:
return success
@classmethod
def unregister_client(cls):
pass
def unregister_client(cls, hostname):
success = False
if hostname in cls.render_clients and hostname != cls.host_name:
cls.render_clients.remove(hostname)
logger.info(f"Client '{hostname}' successfully unregistered")
success = True
return success
@classmethod
def start(cls, background_thread=False):
@@ -318,6 +329,7 @@ class RenderServer:
app.config['UPLOAD_FOLDER'] = config['upload_folder']
app.config['MAX_CONTENT_PATH'] = config['max_content_path']
app.config['RESULT_STATIC_PATH'] = 'static/'
# Get hostname and render clients
cls.host_name = socket.gethostname()
@@ -351,7 +363,7 @@ class RenderServer:
@app.get('/jobs')
def jobs_json():
return jsonify([json.loads(x.json()) for x in RenderServer.render_queue if not x.archived])
return [json.loads(x.json()) for x in RenderServer.render_queue if not x.archived]
@app.get('/jobs/<status_val>')
@@ -377,6 +389,13 @@ def register_client():
return "Success" if x else "Fail"
@app.post('/unregister_client')
def unregister_client():
client_hostname = request.values['hostname']
x = RenderServer.unregister_client(client_hostname)
return "Success" if x else "Fail"
@app.get('/full_status')
def full_status():
full_results = {'timestamp': datetime.now().isoformat(), 'servers': {}}
@@ -415,12 +434,15 @@ def snapshot():
@app.post('/add_job')
def add_job():
"""Create new job and add to server render queue"""
renderer = request.json["renderer"]
input_path = request.json["input"]
output_path = request.json["output"]
renderer = request.json.get("renderer", None)
input_path = request.json.get("input", None)
output_path = request.json.get("output", None)
priority = request.json.get('priority', 2)
args = request.json.get('args', None)
force_start = request.json.get('force_start', False)
if None in [renderer, input_path, output_path]:
return {'error': 'missing required parameters'}, 400
try:
render_job = RenderWorkerFactory.create_worker(renderer, input_path, output_path, args)
@@ -439,9 +461,9 @@ def cancel_job():
job_id = request.args.get('id', None)
confirm = request.args.get('confirm', False)
if not job_id:
return jsonify({'error': 'job id not found'})
return {'error': 'job id not found'}, 400
elif not confirm:
return jsonify({'error': 'confirmation required'})
return {'error': 'confirmation required'}, 400
else:
found = [x for x in RenderServer.render_queue if x.id == job_id]
if len(found) > 1:
@@ -450,18 +472,18 @@ def cancel_job():
elif found:
success = RenderServer.cancel_job(found[0])
return jsonify({'result': success})
return jsonify({'error': 'job not found'})
return {'error': 'job not found'}, 400
@app.get('/clear_history')
def clear_history():
RenderServer.clear_history()
return jsonify({'result': True})
return {'result': True}
@app.route('/status')
def status():
return jsonify(RenderServer.status())
return RenderServer.status()
@app.route('/')