mirror of
https://github.com/blw1138/Zordon.git
synced 2025-12-17 16:58:12 +00:00
54 lines
2.0 KiB
Python
54 lines
2.0 KiB
Python
import os
|
|
import json
|
|
import requests
|
|
from lib.render_workers.base_worker import RenderStatus
|
|
|
|
status_colors = {RenderStatus.ERROR: "red", RenderStatus.CANCELLED: 'orange1', RenderStatus.COMPLETED: 'green',
|
|
RenderStatus.NOT_STARTED: "yellow", RenderStatus.SCHEDULED: 'purple',
|
|
RenderStatus.RUNNING: 'cyan'}
|
|
|
|
categories = [RenderStatus.RUNNING, RenderStatus.ERROR, RenderStatus.NOT_STARTED, RenderStatus.SCHEDULED,
|
|
RenderStatus.COMPLETED, RenderStatus.CANCELLED, RenderStatus.UNDEFINED]
|
|
|
|
|
|
class RenderServerProxy:
|
|
|
|
def __init__(self, hostname=None, server_port="8080"):
|
|
self.hostname = hostname
|
|
self.port = server_port
|
|
self.fetched_status_data = None
|
|
|
|
def connect(self):
|
|
status = self.request_data('status')
|
|
return status
|
|
|
|
def request_data(self, payload, timeout=5):
|
|
try:
|
|
req = requests.get(f'http://{self.hostname}:{self.port}/api/{payload}', timeout=timeout)
|
|
if req.ok:
|
|
return req.json()
|
|
except Exception as e:
|
|
pass
|
|
return None
|
|
|
|
def get_jobs(self):
|
|
all_jobs = self.request_data('jobs')
|
|
sorted_jobs = []
|
|
if all_jobs:
|
|
for status_category in categories:
|
|
found_jobs = [x for x in all_jobs if x['status'] == status_category.value]
|
|
if found_jobs:
|
|
sorted_jobs.extend(found_jobs)
|
|
return sorted_jobs
|
|
|
|
def get_data(self, timeout=5):
|
|
all_data = self.request_data('full_status', timeout=timeout)
|
|
return all_data
|
|
|
|
def post_job_to_server(self, input_path, job_list):
|
|
# Pack job data and submit to server
|
|
job_files = {'file': (os.path.basename(input_path), open(input_path, 'rb'), 'application/octet-stream'),
|
|
'json': (None, json.dumps(job_list), 'application/json')}
|
|
|
|
req = requests.post(f'http://{self.hostname}:{self.port}/api/add_job', files=job_files)
|
|
return req |