Skip to content

Commit d2c9761

Browse files
committed
fix(middleware): avoid repeating job run request
Also blocking repeating run button press on UI
1 parent 90853cc commit d2c9761

File tree

6 files changed

+218
-198
lines changed

6 files changed

+218
-198
lines changed

src/main/data/middlewares/pipeline.ts

+75-58
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,19 @@ function startMonitor(j: Job, ws: Webservice, getState, dispatch) {
208208
...value,
209209
messages: ['removed to keep log cleaner'],
210210
})
211+
let updatedJob = {
212+
...j,
213+
jobData: value,
214+
}
211215
const finished = [
212216
JobStatus.ERROR,
213217
JobStatus.FAIL,
214218
JobStatus.SUCCESS,
215219
].includes(value.status)
216-
217220
if (finished) {
218221
clearInterval(monitor)
222+
updatedJob.state = JobState.ENDED
219223
}
220-
let updatedJob = { ...j }
221-
updatedJob.jobData = value
222224
const newJobName = `${
223225
updatedJob.jobData.nicename ??
224226
updatedJob.jobData.script.nicename
@@ -712,68 +714,83 @@ export function pipelineMiddleware({ getState, dispatch }) {
712714
case runJob.type:
713715
// Launch the job with the API and start monitoring its execution
714716
// Also change its state to submited
715-
let runJobInterval = null
716717
const jobToRun = action.payload as Job
717718
// Empty previous references to jobData results for re run
718719
if (jobToRun.jobData && jobToRun.jobData.results) {
719720
jobToRun.jobData.results = undefined
720721
}
721-
info('Launching job', JSON.stringify(jobToRun))
722-
const launchJobOn = pipelineAPI.launchJob(jobToRun)
723-
runJobInterval = setInterval(() => {
724-
if (webservice) {
725-
launchJobOn(webservice)
726-
.then((jobResponse) => {
727-
const updatedJob = {
728-
...jobToRun,
729-
state: JobState.SUBMITTED,
730-
} as Job
731-
if (
732-
jobResponse.type == 'JobRequestError' ||
733-
jobResponse.type == 'JobUnknownResponse'
734-
) {
735-
updatedJob.jobRequestError =
736-
jobResponse as JobRequestError
737-
} else {
738-
updatedJob.jobData = jobResponse as JobData
739-
// start a job monitor
740-
startMonitor(
741-
updatedJob,
742-
webservice,
743-
getState,
744-
dispatch
745-
)
746-
}
747-
clearInterval(runJobInterval)
748-
dispatch(updateJob(updatedJob))
749-
})
750-
.catch((e) => {
751-
clearInterval(runJobInterval)
752-
error(
753-
'error launching job',
754-
jobToRun.internalId,
755-
e
722+
if (
723+
jobToRun.state === JobState.SUBMITTED ||
724+
jobToRun.state === JobState.SUBMITTING
725+
) {
726+
info('Job is already launched', JSON.stringify(jobToRun))
727+
} else if (webservice) {
728+
info('Launching job', JSON.stringify(jobToRun))
729+
dispatch(
730+
updateJob({ ...jobToRun, state: JobState.SUBMITTING })
731+
)
732+
pipelineAPI
733+
.launchJob(jobToRun)(webservice)
734+
.then((jobResponse) => {
735+
const updatedJob = {
736+
...jobToRun,
737+
state: JobState.SUBMITTED,
738+
} as Job
739+
if (
740+
jobResponse.type == 'JobRequestError' ||
741+
jobResponse.type == 'JobUnknownResponse'
742+
) {
743+
updatedJob.jobRequestError =
744+
jobResponse as JobRequestError
745+
} else {
746+
updatedJob.jobData = jobResponse as JobData
747+
// start a job monitor
748+
startMonitor(
749+
updatedJob,
750+
webservice,
751+
getState,
752+
dispatch
756753
)
757-
dispatch(
758-
updateJob({
759-
...jobToRun,
760-
jobData: {
761-
...jobToRun.jobData,
762-
status: JobStatus.ERROR,
754+
}
755+
dispatch(updateJob(updatedJob))
756+
})
757+
.catch((e) => {
758+
error('error launching job', jobToRun.internalId, e)
759+
dispatch(
760+
updateJob({
761+
...jobToRun,
762+
jobData: {
763+
...jobToRun.jobData,
764+
status: JobStatus.ERROR,
765+
},
766+
errors: [
767+
{
768+
error:
769+
e instanceof ParserException
770+
? e.parsedText
771+
: String(e),
763772
},
764-
errors: [
765-
{
766-
error:
767-
e instanceof ParserException
768-
? e.parsedText
769-
: String(e),
770-
},
771-
],
772-
})
773-
)
774-
})
775-
}
776-
}, 1000)
773+
],
774+
})
775+
)
776+
})
777+
} else {
778+
error('No webservice available to run job', jobToRun)
779+
dispatch(
780+
updateJob({
781+
...jobToRun,
782+
jobData: {
783+
...jobToRun.jobData,
784+
status: JobStatus.ERROR,
785+
},
786+
errors: [
787+
{
788+
error: 'No webservice available',
789+
},
790+
],
791+
})
792+
)
793+
}
777794
break
778795
case requestStylesheetParameters.type:
779796
const job = action.payload as Job

src/renderer/components/JobDetailsPane/Settings.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ const { App } = window
1212

1313
export function Settings({ job }: { job: Job }) {
1414
const { pipeline } = useWindowStore()
15-
const scriptDetails = pipeline.scripts.filter(
16-
(s) => s.id == job.jobData.script.id
17-
)[0]
15+
const scriptId = job.jobData?.script?.id || job.script?.id
16+
const scriptDetails = pipeline.scripts.filter((s) => s.id == scriptId)[0]
1817
if (!scriptDetails) {
19-
return <p>Unrecognized script {job.jobData.script.id}</p>
18+
return <p>Unrecognized script {scriptId}</p>
2019
}
2120

2221
return (

src/renderer/components/JobDetailsPane/index.tsx

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
Details of a submitted job
33
*/
4-
import { Job, JobStatus } from '/shared/types'
4+
import { Job, JobState, JobStatus } from '/shared/types'
55
import { Messages } from './Messages'
66
import { Settings } from './Settings'
77
import { Results } from './Results'
@@ -17,12 +17,19 @@ const { App } = window
1717

1818
export function JobDetailsPane({ job }: { job: Job }) {
1919
const [canRunJob, setCanRunJob] = useState(false)
20+
const [isRerunning, setIsRerunning] = useState(false)
2021
const { settings } = useWindowStore()
2122

2223
//let probableLogLink = job?.jobData?.href ? `${job.jobData.href}/log` : ''
2324
useEffect(() => {
2425
setCanRunJob(settings?.downloadFolder?.trim() != '')
2526
}, [settings.downloadFolder])
27+
useEffect(() => {
28+
// In case the job is rejected and its state is reset to a previous
29+
setIsRerunning(
30+
[JobState.SUBMITTING, JobState.SUBMITTED].includes(job.state)
31+
)
32+
}, [job.state])
2633

2734
return job.jobRequestError ? (
2835
<>
@@ -61,11 +68,17 @@ export function JobDetailsPane({ job }: { job: Job }) {
6168
<p aria-live="polite">
6269
Status:&nbsp;
6370
<span
64-
className={`status ${readableStatus[
65-
job.jobData.status
66-
].toLowerCase()}`}
71+
className={`status ${
72+
job.jobData?.status
73+
? readableStatus[
74+
job.jobData.status
75+
].toLowerCase()
76+
: readableStatus.LAUNCHING.toLowerCase()
77+
}`}
6778
>
68-
{readableStatus[job.jobData.status]}{' '}
79+
{job.jobData?.status
80+
? readableStatus[job.jobData.status]
81+
: readableStatus.LAUNCHING}{' '}
6982
</span>
7083
</p>
7184
{job.jobData.progress ? (
@@ -146,8 +159,9 @@ export function JobDetailsPane({ job }: { job: Job }) {
146159
<button
147160
onClick={(e) => {
148161
App.store.dispatch(runJob(job))
162+
setIsRerunning(true)
149163
}}
150-
disabled={!canRunJob}
164+
disabled={!canRunJob || isRerunning}
151165
>
152166
Re-run job
153167
</button>

0 commit comments

Comments
 (0)