Initial commit of index html and add ability to delete jobs

This commit is contained in:
Brett Williams
2022-12-07 18:24:02 -08:00
parent cc394932a1
commit fbaf2e3661
7 changed files with 252 additions and 38 deletions

View File

@@ -5,11 +5,12 @@ import logging
import os
import pathlib
import shutil
import json2html
from datetime import datetime
from zipfile import ZipFile
import requests
from flask import Flask, request, render_template, send_file, after_this_request, Response
from flask import Flask, request, render_template, send_file, after_this_request, Response, redirect, url_for
from werkzeug.utils import secure_filename
from lib.render_job import RenderJob
@@ -17,18 +18,33 @@ from lib.render_queue import RenderQueue
from utilities.render_worker import RenderWorkerFactory, string_to_status
logger = logging.getLogger()
server = Flask(__name__)
server = Flask(__name__, template_folder='../templates')
@server.route('/')
@server.route('/index')
def index():
return render_template('index.html', all_jobs=RenderQueue.job_queue, hostname=RenderQueue.host_name)
@server.route('/ui/job/<job_id>/full_details')
def job_detail(job_id):
found_job = RenderQueue.job_with_id(job_id)
if found_job:
table_html = json2html.json2html.convert(json=found_job.json())
return render_template('details.html', detail_table=table_html)
return f'Cannot find job with ID {job_id}', 400
@server.get('/api/jobs')
def jobs_json():
return [x.json_safe_copy() for x in RenderQueue.job_queue]
return [x.json() for x in RenderQueue.job_queue]
@server.get('/api/jobs/<status_val>')
def filtered_jobs_json(status_val):
state = string_to_status(status_val)
jobs = [x.json_safe_copy() for x in RenderQueue.jobs_with_status(state)]
jobs = [x.json() for x in RenderQueue.jobs_with_status(state)]
if jobs:
return jobs
else:
@@ -39,7 +55,7 @@ def filtered_jobs_json(status_val):
def get_job_status(job_id):
found_job = RenderQueue.job_with_id(job_id)
if found_job:
return found_job.json_safe_copy()
return found_job.json()
else:
return f'Cannot find job with ID {job_id}', 400
@@ -58,17 +74,16 @@ def get_job_logs(job_id):
return f'Cannot find job with ID {job_id}', 400
@server.get('/api/file_list/<job_id>')
@server.get('/api/job/<job_id>/file_list')
def get_file_list(job_id):
found_job = RenderQueue.job_with_id(job_id)
if found_job:
job_dir = os.path.dirname(found_job.worker.output_path)
return os.listdir(job_dir)
return '\n'.join(found_job.file_list())
else:
return f'Cannot find job with ID {job_id}', 400
@server.route('/api/download_all/<job_id>')
@server.route('/api/job/<job_id>/download_all')
def download_all(job_id):
zip_filename = None
@@ -83,7 +98,7 @@ def download_all(job_id):
if found_job:
output_dir = os.path.dirname(found_job.worker.output_path)
if os.path.exists(output_dir):
zip_filename = pathlib.Path(found_job.worker.input_path).stem + '.zip'
zip_filename = os.path.join('/tmp', pathlib.Path(found_job.worker.input_path).stem + '.zip')
with ZipFile(zip_filename, 'w') as zipObj:
for f in os.listdir(output_dir):
zipObj.write(filename=os.path.join(output_dir, f),
@@ -144,7 +159,7 @@ def full_status():
@server.get('/api/snapshot')
def snapshot():
server_status = RenderQueue.status()
server_jobs = [x.json_safe_copy() for x in RenderQueue.job_queue]
server_jobs = [x.json() for x in RenderQueue.job_queue]
server_data = {'status': server_status, 'jobs': server_jobs, 'timestamp': datetime.now().isoformat()}
return server_data
@@ -229,7 +244,7 @@ def add_job(job_params, remove_job_dir_on_failure=False):
render_job = RenderJob(renderer, input_path, output_path, args, priority, job_owner, client,
notify=False, custom_id=custom_id, name=name)
RenderQueue.add_to_render_queue(render_job, force_start=force_start)
return render_job.json_safe_copy()
return render_job.json()
except Exception as e:
err_msg = f"Error creating job: {str(e)}"
logger.exception(err_msg)
@@ -272,22 +287,51 @@ def add_job(job_params, remove_job_dir_on_failure=False):
return {'error': err_msg, 'code': 400}
@server.get('/api/cancel_job')
def cancel_job():
job_id = request.args.get('id', None)
confirm = request.args.get('confirm', False)
if not job_id:
return 'job id not found', 400
elif not confirm:
return 'confirmation required', 400
@server.get('/api/job/<job_id>/cancel')
def cancel_job(job_id):
found_job = RenderQueue.job_with_id(job_id)
if not found_job:
return f'Cannot find job with ID {job_id}', 400
elif not request.args.get('confirm', False):
return 'Confirmation required to cancel job', 400
if RenderQueue.cancel_job(found_job):
return redirect(url_for('index'))
else:
found = [x for x in RenderQueue.job_queue if x.id == job_id]
if len(found) > 1:
return f'multiple jobs found for ID {job_id}', 400
elif found:
success = RenderQueue.cancel_job(found[0])
return success
return 'job not found', 400
return "Unknown error", 500
@server.route('/api/job/<job_id>/delete', methods=['POST', 'GET'])
def delete_job(job_id):
try:
found_job = RenderQueue.job_with_id(job_id)
if not found_job:
return f'Cannot find job with ID {job_id}', 400
elif not request.args.get('confirm', False):
return 'Confirmation required to delete job', 400
# First, remove all render files and logs
files_to_delete = found_job.file_list()
files_to_delete.append(found_job.log_path())
for d in files_to_delete:
if os.path.exists(d):
os.remove(d)
# Check if we can remove the 'output' directory
output_dir = os.path.dirname(files_to_delete[0])
if server.config['UPLOAD_FOLDER'] in output_dir and os.path.exists(output_dir):
shutil.rmtree(output_dir)
# See if we own the input file (i.e. was it uploaded)
input_dir = os.path.dirname(found_job.worker.input_path)
if server.config['UPLOAD_FOLDER'] in input_dir and os.path.exists(input_dir):
shutil.rmtree(input_dir)
RenderQueue.delete_job(found_job)
return redirect(url_for('index'))
except Exception as e:
return "Unknown error", 500
@server.get('/api/clear_history')
@@ -313,11 +357,6 @@ def renderer_info():
return renderer_data
@server.route('/')
def default():
return "Server running"
@server.route('/upload')
def upload_file_page():
return render_template('upload.html', render_clients=RenderQueue.render_clients,