Fix some issues around adding jobs through local rest call

This commit is contained in:
Brett Williams
2022-12-07 13:45:15 -08:00
parent 7a3fed1960
commit 89b7cb38f7
2 changed files with 11 additions and 12 deletions

View File

@@ -140,33 +140,32 @@ def add_job_handler():
try:
"""Create new job and add to server render queue"""
json_string = request.form.get('json', None)
uploaded_file = request.files.get('file', None)
if not json_string:
if not request.form.get('json', None) and not request.is_json:
return 'missing json data', 400
# handle uploaded files
uploaded_file = request.files.get('file', None)
uploaded_file_local_path = None
job_dir = None
if uploaded_file and uploaded_file.filename:
logger.info(f"Receiving uploaded file {uploaded_file.filename}")
new_id = RenderJob.generate_id()
job_dir = os.path.join(server.config['UPLOAD_FOLDER'], new_id + "-" + uploaded_file.filename)
if not os.path.exists(job_dir):
os.makedirs(job_dir)
job_dir = os.path.join(server.config['UPLOAD_FOLDER'], (uploaded_file.filename + "_" +
datetime.now().strftime("%Y.%m.%d_%H.%M.%S")))
os.makedirs(job_dir, exist_ok=True)
uploaded_file_local_path = os.path.join(job_dir, secure_filename(uploaded_file.filename))
uploaded_file.save(uploaded_file_local_path)
# convert job input paths for uploaded files and add jobs
jobs_list = json.loads(json_string)
json_string = request.form.get('json', None)
jobs_list = json.loads(json_string) if json_string else [request.json]
results = []
for job in jobs_list:
if uploaded_file_local_path:
job['input_path'] = uploaded_file_local_path
output_dir = os.path.join(job_dir, job.get('name', 'output'))
output_dir = os.path.join(job_dir, job.get('name', None) or 'output')
os.makedirs(output_dir, exist_ok=True)
job['output_path'] = os.path.join(output_dir, os.path.basename(job['output_path']))
remove_job_dir = len(jobs_list) == 1 # remove failed job dir for single file uploads only
remove_job_dir = len(jobs_list) == 1 and uploaded_file_local_path # remove failed job dir for single file uploads only
add_result = add_job(job, remove_job_dir_on_failure=remove_job_dir)
results.append(add_result)

View File

@@ -22,7 +22,7 @@ class RenderJob:
self.date_created = datetime.now()
self.scheduled_start = None
self.renderer = renderer
self.name = name or os.path.basename(input_path) + '_' + self.date_created.isoformat()
self.name = name or os.path.basename(input_path) + '_' + self.date_created.strftime("%Y.%m.%d_%H.%M.%S")
self.worker = RenderWorkerFactory.create_worker(renderer, input_path, output_path, args)
self.worker.log_path = os.path.join(os.path.dirname(input_path), self.name + '.log')
@@ -45,7 +45,7 @@ class RenderJob:
try:
job_dict = self.__dict__.copy()
job_dict['status'] = self.render_status().value
job_dict['file_hash'] = self.file_hash if isinstance(self.file_hash, str) else self.file_hash()
job_dict['file_hash'] = self.file_hash if self.file_hash and isinstance(self.file_hash, str) else self.file_hash()
job_dict['worker'] = self.worker.__dict__.copy()
job_dict['worker']['status'] = job_dict['status']