Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
oxabz committed Jul 6, 2021
1 parent 3226c6d commit ace5557
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
54 changes: 34 additions & 20 deletions FrontEnd/src/tools/MonitorJob.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,72 @@
import { toast } from 'react-toastify'
import {toast} from 'react-toastify'
import apis from '../services/apis'

export default class MonitorJob {

constructor(jobID, interval=1000){
constructor(jobID, interval = 1000) {
this.jobID = jobID
this.interval = interval
this.finishCallback = function(status){}
this.updateCallBack = function(progress){}
this.continue = false
this.finishCallback = function (status) {
}
this.updateCallBack = function (progress) {
}
}

onFinish(callback){
onFinish(callback) {
this.finishCallback = callback
}

onUpdate(callback){
onUpdate(callback) {
this.updateCallBack = callback
}

startMonitoringJob() {
this.intervalChcker = setInterval(() => this.jobMonitoring(this.jobID), this.interval)
this.continue = true;
this.jobMonitoring(this.jobID)
}

stopMonitoringJob() {
if(this.intervalChcker !== undefined) clearInterval(this.intervalChcker)
this.continue = false;
}

async cancelJob(){
await apis.jobs.cancelJob(this.jobID).catch(error => {toast.error(error.statusText)})
async cancelJob() {
await apis.jobs.cancelJob(this.jobID).catch(error => {
toast.error(error.statusText)
})
toast.success('Job Cancelled')
this.stopMonitoringJob()
}

async jobMonitoring(jobUID) {
let jobData
let queryStartTime = Date.now();

try{
jobData = await apis.jobs.getJobInfos(jobUID)
} catch(error){
try {
jobData = await apis.jobs.getJobInfos(jobUID)
} catch (error) {
console.error(error)
this.stopMonitoringJob()
toast.error('Monitoring Failed')
return
}

const currentStatus = jobData.State

this.updateCallBack(jobData.Progress)

if (currentStatus === MonitorJob.Success || currentStatus === MonitorJob.Failure ) {
this.stopMonitoringJob()
this.finishCallback(currentStatus)

if (this.continue) {
setTimeout(() => {
this.jobMonitoring(this.jobID)
}, this.interval - (Date.now() - queryStartTime))
}


if (currentStatus === MonitorJob.Success || currentStatus === MonitorJob.Failure) {
this.stopMonitoringJob()
this.finishCallback(currentStatus)
}


}
}

Expand Down
11 changes: 9 additions & 2 deletions FrontEnd/src/tools/MonitorTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export default class MonitorTask extends MonitorJob {
cancel() {
console.warn("export job supression not implemented")
}

async jobMonitoring(jobUuid) {
let queryStartTime = Date.now();

let task
try {
Expand All @@ -24,7 +25,13 @@ export default class MonitorTask extends MonitorJob {

this.updateCallBack(task)

if (task.state === 'completed'||task.state === 'failed') {
if (this.continue) {
setTimeout(() => {
this.jobMonitoring(this.jobID)
}, this.interval - (Date.now() - queryStartTime))
}

if (task.state === 'completed' || task.state === 'failed') {
this.stopMonitoringJob()
this.finishCallback(task)
}
Expand Down

0 comments on commit ace5557

Please sign in to comment.