Better error handling when posting a new job

This commit is contained in:
Brett Williams
2024-07-29 14:50:14 -05:00
parent a0729d71f1
commit 6d33f262b3

View File

@@ -1,12 +1,12 @@
import json
import logging
import os
import socket
import threading
import time
import requests
from requests_toolbelt.multipart import MultipartEncoder, MultipartEncoderMonitor
from urllib.parse import urljoin
from src.utilities.misc_helper import is_localhost
from src.utilities.status_utils import RenderStatus
@@ -46,7 +46,7 @@ class RenderServerProxy:
self.__background_thread = None
self.__offline_flags = 0
self.update_cadence = 5
self.is_localhost = is_localhost(hostname)
self.is_localhost = bool(is_localhost(hostname))
# Cache some basic server info
self.system_cpu = None
@@ -190,27 +190,39 @@ class RenderServerProxy:
Returns:
Response: The response from the server.
"""
# bypass uploading file if posting to localhost
if self.is_localhost:
jobs_with_path = [{**item, "local_path": file_path} for item in job_list]
return requests.post(f'http://{LOOPBACK}:{self.port}/api/add_job', data=json.dumps(jobs_with_path),
headers={'Content-Type': 'application/json'})
try:
# Check if file exists
if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found: {file_path}")
# Prepare the form data
encoder = MultipartEncoder({
'file': (os.path.basename(file_path), open(file_path, 'rb'), 'application/octet-stream'),
'json': (None, json.dumps(job_list), 'application/json'),
})
# Bypass uploading file if posting to localhost
if self.is_localhost:
jobs_with_path = [{'local_path': file_path, **item} for item in job_list]
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
if callback:
monitor = MultipartEncoderMonitor(encoder, callback(encoder))
else:
monitor = MultipartEncoderMonitor(encoder)
# Prepare the form data for remote host
with open(file_path, 'rb') as file:
encoder = MultipartEncoder({
'file': (os.path.basename(file_path), file, 'application/octet-stream'),
'json': (None, json.dumps(job_list), 'application/json'),
})
# Send the request
headers = {'Content-Type': monitor.content_type}
return requests.post(f'http://{self.hostname}:{self.port}/api/add_job', data=monitor, headers=headers)
# Create a monitor that will track the upload progress
monitor = MultipartEncoderMonitor(encoder, callback) if callback else MultipartEncoderMonitor(encoder)
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):
hostname = LOOPBACK if self.is_localhost else self.hostname