diff --git a/app/jobs/json_routes.py b/app/jobs/json_routes.py index 261fe3cc..50846bae 100644 --- a/app/jobs/json_routes.py +++ b/app/jobs/json_routes.py @@ -1,10 +1,10 @@ -from flask import (abort, current_app, jsonify) +from flask import abort, current_app, jsonify from flask_login import current_user, login_required from threading import Thread import os from app import db from app.decorators import admin_required, content_negotiation -from app.models import Job, JobInput, JobResult, JobStatus +from app.models import Job, JobStatus from . import bp @bp.route('/', methods=['DELETE']) @@ -36,6 +36,7 @@ def delete_job(job_id): @bp.route('//log') @login_required @admin_required +@content_negotiation(produces='application/json') def job_log(job_id): job = Job.query.get_or_404(job_id) if job.status not in [JobStatus.COMPLETED, JobStatus.FAILED]: @@ -43,11 +44,18 @@ def job_log(job_id): return response, 409 with open(os.path.join(job.path, 'pipeline_data', 'logs', 'pyflow_log.txt')) as log_file: log = log_file.read() - return log, 200, {'Content-Type': 'text/plain; charset=utf-8'} + response_data = { + 'message': '', + 'jobLog': log + } + response = jsonify(response_data) + response.status_code = 200 + return response @bp.route('//restart', methods=['POST']) @login_required +@content_negotiation(produces='application/json') def restart_job(job_id): def _restart_job(app, job_id): with app.app_context(): @@ -66,4 +74,10 @@ def restart_job(job_id): args=(current_app._get_current_object(), job_id) ) thread.start() - return {}, 202 + response_data = { + 'message': \ + f'Job "{job.title}" marked for restarting' + } + response = jsonify(response_data) + response.status_code = 202 + return response diff --git a/app/static/js/Requests/jobs/jobs.js b/app/static/js/Requests/jobs/jobs.js index f1179658..64e523db 100644 --- a/app/static/js/Requests/jobs/jobs.js +++ b/app/static/js/Requests/jobs/jobs.js @@ -21,3 +21,11 @@ Requests.jobs.entity.log = (jobId) => { }; return Requests.JSONfetch(input, init); } + +Requests.jobs.entity.restart = (jobId) => { + let input = `/jobs/${jobId}/restart`; + let init = { + method: 'POST' + }; + return Requests.JSONfetch(input, init); +} diff --git a/app/static/js/ResourceDisplays/JobDisplay.js b/app/static/js/ResourceDisplays/JobDisplay.js index df8eb7b5..6287d934 100644 --- a/app/static/js/ResourceDisplays/JobDisplay.js +++ b/app/static/js/ResourceDisplays/JobDisplay.js @@ -2,16 +2,6 @@ class JobDisplay extends ResourceDisplay { constructor(displayElement) { super(displayElement); this.jobId = this.displayElement.dataset.jobId; - this.displayElement - .querySelector('.action-button[data-action="get-log-request"]') - .addEventListener('click', (event) => { - Utils.getJobLogRequest(this.userId, this.jobId); - }); - this.displayElement - .querySelector('.action-button[data-action="restart-request"]') - .addEventListener('click', (event) => { - Utils.restartJobRequest(this.userId, this.jobId); - }); } init(user) { diff --git a/app/static/js/Utils.js b/app/static/js/Utils.js index 5461cd67..12bdea0d 100644 --- a/app/static/js/Utils.js +++ b/app/static/js/Utils.js @@ -173,138 +173,138 @@ class Utils { }); } - static deleteJobRequest(userId, jobId) { - return new Promise((resolve, reject) => { - let job; - try { - job = app.data.users[userId].jobs[jobId]; - } catch (error) { - job = {}; - } + // static deleteJobRequest(userId, jobId) { + // return new Promise((resolve, reject) => { + // let job; + // try { + // job = app.data.users[userId].jobs[jobId]; + // } catch (error) { + // job = {}; + // } - let confirmElement = modalElement.querySelector('.action-button[data-action="confirm"]'); - confirmElement.addEventListener('click', (event) => { - let jobTitle = job?.title; - fetch(`/jobs/${jobId}`, {method: 'DELETE', headers: {Accept: 'application/json'}}) - .then( - (response) => { - if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);} - if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);} - app.flash(`Job "${jobTitle}" marked for deletion`, 'job'); - resolve(response); - }, - (response) => { - app.flash('Something went wrong', 'error'); - reject(response); - } - ); - }); - modal.open(); - }); - } + // let confirmElement = modalElement.querySelector('.action-button[data-action="confirm"]'); + // confirmElement.addEventListener('click', (event) => { + // let jobTitle = job?.title; + // fetch(`/jobs/${jobId}`, {method: 'DELETE', headers: {Accept: 'application/json'}}) + // .then( + // (response) => { + // if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);} + // if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);} + // app.flash(`Job "${jobTitle}" marked for deletion`, 'job'); + // resolve(response); + // }, + // (response) => { + // app.flash('Something went wrong', 'error'); + // reject(response); + // } + // ); + // }); + // modal.open(); + // }); + // } - static getJobLogRequest(userId, jobId) { - return new Promise((resolve, reject) => { - fetch(`/jobs/${jobId}/log`, {method: 'GET', headers: {Accept: 'application/json, text/plain'}}) - .then( - (response) => { - if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);} - if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);} - return response.text(); - }, - (response) => { - app.flash('Something went wrong', 'error'); - reject(response); - } - ) - .then( - (text) => { - let modalElement = Utils.HTMLToElement( - ` - - ` - ); - document.querySelector('#modals').appendChild(modalElement); - let modal = M.Modal.init( - modalElement, - { - onCloseEnd: () => { - modal.destroy(); - modalElement.remove(); - } - } - ); - modal.open(); - resolve(text); - } - ); - }); - } + // static getJobLogRequest(userId, jobId) { + // return new Promise((resolve, reject) => { + // fetch(`/jobs/${jobId}/log`, {method: 'GET', headers: {Accept: 'application/json, text/plain'}}) + // .then( + // (response) => { + // if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);} + // if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);} + // return response.text(); + // }, + // (response) => { + // app.flash('Something went wrong', 'error'); + // reject(response); + // } + // ) + // .then( + // (text) => { + // let modalElement = Utils.HTMLToElement( + // ` + // + // ` + // ); + // document.querySelector('#modals').appendChild(modalElement); + // let modal = M.Modal.init( + // modalElement, + // { + // onCloseEnd: () => { + // modal.destroy(); + // modalElement.remove(); + // } + // } + // ); + // modal.open(); + // resolve(text); + // } + // ); + // }); + // } - static restartJobRequest(userId, jobId) { - return new Promise((resolve, reject) => { - let job; - try { - job = app.data.users[userId].jobs[jobId]; - } catch (error) { - job = {}; - } + // static restartJobRequest(userId, jobId) { + // return new Promise((resolve, reject) => { + // let job; + // try { + // job = app.data.users[userId].jobs[jobId]; + // } catch (error) { + // job = {}; + // } - let modalElement = Utils.HTMLToElement( - ` - - ` - ); - document.querySelector('#modals').appendChild(modalElement); - let modal = M.Modal.init( - modalElement, - { - dismissible: false, - onCloseEnd: () => { - modal.destroy(); - modalElement.remove(); - } - } - ); + // let modalElement = Utils.HTMLToElement( + // ` + // + // ` + // ); + // document.querySelector('#modals').appendChild(modalElement); + // let modal = M.Modal.init( + // modalElement, + // { + // dismissible: false, + // onCloseEnd: () => { + // modal.destroy(); + // modalElement.remove(); + // } + // } + // ); - let confirmElement = modalElement.querySelector('.action-button[data-action="confirm"]'); - confirmElement.addEventListener('click', (event) => { - let jobTitle = job?.title; - fetch(`/jobs/${jobId}/restart`, {method: 'POST', headers: {Accept: 'application/json'}}) - .then( - (response) => { - if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);} - if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);} - if (response.status === 409) {app.flash('Conflict', 'error'); reject(response);} - app.flash(`Job "${jobTitle}" restarted.`, 'job'); - resolve(response); - }, - (response) => { - app.flash('Something went wrong', 'error'); - reject(response); - } - ); - }); - modal.open(); - }); - } + // let confirmElement = modalElement.querySelector('.action-button[data-action="confirm"]'); + // confirmElement.addEventListener('click', (event) => { + // let jobTitle = job?.title; + // fetch(`/jobs/${jobId}/restart`, {method: 'POST', headers: {Accept: 'application/json'}}) + // .then( + // (response) => { + // if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);} + // if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);} + // if (response.status === 409) {app.flash('Conflict', 'error'); reject(response);} + // app.flash(`Job "${jobTitle}" restarted.`, 'job'); + // resolve(response); + // }, + // (response) => { + // app.flash('Something went wrong', 'error'); + // reject(response); + // } + // ); + // }); + // modal.open(); + // }); + // } static deleteUserRequest(userId) { return new Promise((resolve, reject) => { diff --git a/app/templates/jobs/job.html.j2 b/app/templates/jobs/job.html.j2 index 30bedaa1..b5055ce6 100644 --- a/app/templates/jobs/job.html.j2 +++ b/app/templates/jobs/job.html.j2 @@ -79,9 +79,9 @@
{% if current_user.is_administrator() %} - text_snippetLog + text_snippetLog {% endif %} - repeatRestart + repeatRestart deleteDelete
@@ -120,20 +120,31 @@

Do you really want to delete the Job {{job.title}}? All files will be permanently deleted!

-