mirror of
https://github.com/blw1138/Zordon.git
synced 2025-12-17 08:48:13 +00:00
Better error handling when posting a new job
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import socket
|
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from requests_toolbelt.multipart import MultipartEncoder, MultipartEncoderMonitor
|
from requests_toolbelt.multipart import MultipartEncoder, MultipartEncoderMonitor
|
||||||
|
from urllib.parse import urljoin
|
||||||
|
|
||||||
from src.utilities.misc_helper import is_localhost
|
from src.utilities.misc_helper import is_localhost
|
||||||
from src.utilities.status_utils import RenderStatus
|
from src.utilities.status_utils import RenderStatus
|
||||||
@@ -46,7 +46,7 @@ class RenderServerProxy:
|
|||||||
self.__background_thread = None
|
self.__background_thread = None
|
||||||
self.__offline_flags = 0
|
self.__offline_flags = 0
|
||||||
self.update_cadence = 5
|
self.update_cadence = 5
|
||||||
self.is_localhost = is_localhost(hostname)
|
self.is_localhost = bool(is_localhost(hostname))
|
||||||
|
|
||||||
# Cache some basic server info
|
# Cache some basic server info
|
||||||
self.system_cpu = None
|
self.system_cpu = None
|
||||||
@@ -190,27 +190,39 @@ class RenderServerProxy:
|
|||||||
Returns:
|
Returns:
|
||||||
Response: The response from the server.
|
Response: The response from the server.
|
||||||
"""
|
"""
|
||||||
# bypass uploading file if posting to localhost
|
try:
|
||||||
if self.is_localhost:
|
# Check if file exists
|
||||||
jobs_with_path = [{**item, "local_path": file_path} for item in job_list]
|
if not os.path.exists(file_path):
|
||||||
return requests.post(f'http://{LOOPBACK}:{self.port}/api/add_job', data=json.dumps(jobs_with_path),
|
raise FileNotFoundError(f"File not found: {file_path}")
|
||||||
headers={'Content-Type': 'application/json'})
|
|
||||||
|
|
||||||
# Prepare the form data
|
# Bypass uploading file if posting to localhost
|
||||||
encoder = MultipartEncoder({
|
if self.is_localhost:
|
||||||
'file': (os.path.basename(file_path), open(file_path, 'rb'), 'application/octet-stream'),
|
jobs_with_path = [{'local_path': file_path, **item} for item in job_list]
|
||||||
'json': (None, json.dumps(job_list), 'application/json'),
|
job_data = json.dumps(jobs_with_path)
|
||||||
})
|
url = urljoin(f'http://{LOOPBACK}:{self.port}', '/api/add_job')
|
||||||
|
headers = {'Content-Type': 'application/json'}
|
||||||
|
return requests.post(url, data=job_data, headers=headers)
|
||||||
|
|
||||||
# Create a monitor that will track the upload progress
|
# Prepare the form data for remote host
|
||||||
if callback:
|
with open(file_path, 'rb') as file:
|
||||||
monitor = MultipartEncoderMonitor(encoder, callback(encoder))
|
encoder = MultipartEncoder({
|
||||||
else:
|
'file': (os.path.basename(file_path), file, 'application/octet-stream'),
|
||||||
monitor = MultipartEncoderMonitor(encoder)
|
'json': (None, json.dumps(job_list), 'application/json'),
|
||||||
|
})
|
||||||
|
|
||||||
# Send the request
|
# Create a monitor that will track the upload progress
|
||||||
headers = {'Content-Type': monitor.content_type}
|
monitor = MultipartEncoderMonitor(encoder, callback) if callback else MultipartEncoderMonitor(encoder)
|
||||||
return requests.post(f'http://{self.hostname}:{self.port}/api/add_job', data=monitor, headers=headers)
|
headers = {'Content-Type': monitor.content_type}
|
||||||
|
url = urljoin(f'http://{self.hostname}:{self.port}', '/api/add_job')
|
||||||
|
|
||||||
|
# Send the request with proper resource management
|
||||||
|
with requests.post(url, data=monitor, headers=headers) as response:
|
||||||
|
return response
|
||||||
|
|
||||||
|
except requests.ConnectionError as e:
|
||||||
|
logger.error(f"Connection error: {e}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"An error occurred: {e}")
|
||||||
|
|
||||||
def get_job_files(self, job_id, save_path):
|
def get_job_files(self, job_id, save_path):
|
||||||
hostname = LOOPBACK if self.is_localhost else self.hostname
|
hostname = LOOPBACK if self.is_localhost else self.hostname
|
||||||
|
|||||||
Reference in New Issue
Block a user