Skip to content

Commit eff6e9c

Browse files
committed
feat: show loading screen for active task if scheduled/running
Signed-off-by: Edward Ly <[email protected]>
1 parent 39ee4be commit eff6e9c

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

src/assistant.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,55 @@ export async function openAssistantForm({
137137
view.$on('load-task', (task) => {
138138
if (!view.loading) {
139139
console.debug('[assistant] loading task', task)
140+
cancelTaskPolling()
141+
140142
view.selectedTaskTypeId = task.type
141143
view.inputs = task.input
142144
view.outputs = task.status === TASK_STATUS_STRING.successful ? task.output : null
143145
view.selectedTaskId = task.id
144146
lastTask = task
147+
148+
if ([TASK_STATUS_STRING.scheduled, TASK_STATUS_STRING.running].includes(task?.status)) {
149+
getTask(task.id).then(response => {
150+
const updatedTask = response.data?.ocs?.data?.task
151+
152+
if (![TASK_STATUS_STRING.scheduled, TASK_STATUS_STRING.running].includes(updatedTask?.status)) {
153+
view.selectedTaskTypeId = updatedTask.type
154+
view.inputs = updatedTask.input
155+
view.outputs = updatedTask.status === TASK_STATUS_STRING.successful ? updatedTask.output : null
156+
view.selectedTaskId = updatedTask.id
157+
lastTask = updatedTask
158+
return
159+
}
160+
161+
view.loading = true
162+
view.showSyncTaskRunning = true
163+
view.progress = null
164+
view.expectedRuntime = (updatedTask?.completionExpectedAt - updatedTask?.scheduledAt) || null
165+
166+
const setProgress = (progress) => {
167+
view.progress = progress
168+
}
169+
pollTask(updatedTask.id, setProgress).then(finishedTask => {
170+
console.debug('pollTask.then', finishedTask)
171+
if (finishedTask.status === TASK_STATUS_STRING.successful) {
172+
view.outputs = finishedTask?.output
173+
view.selectedTaskId = finishedTask?.id
174+
} else if (finishedTask.status === TASK_STATUS_STRING.failed) {
175+
showError(t('assistant', 'Your task with ID {id} has failed', { id: finishedTask.id }))
176+
console.error('[assistant] Task failed', finishedTask)
177+
view.outputs = null
178+
}
179+
// resolve(finishedTask)
180+
view.loading = false
181+
view.showSyncTaskRunning = false
182+
}).catch(error => {
183+
console.debug('[assistant] poll error', error)
184+
})
185+
}).catch(error => {
186+
console.error(error)
187+
})
188+
}
145189
}
146190
})
147191
view.$on('new-task', () => {
@@ -172,6 +216,7 @@ export async function openAssistantForm({
172216
view.$destroy()
173217
})
174218
view.$on('back-to-assistant', () => {
219+
cancelTaskPolling()
175220
view.showScheduleConfirmation = false
176221
view.showSyncTaskRunning = false
177222
view.loading = false
@@ -436,11 +481,55 @@ export async function openAssistantTask(task, { isInsideViewer = undefined, acti
436481
})
437482
view.$on('load-task', (task) => {
438483
if (!view.loading) {
484+
cancelTaskPolling()
485+
439486
view.selectedTaskTypeId = task.type
440487
view.inputs = task.input
441488
view.outputs = task.status === TASK_STATUS_STRING.successful ? task.output : null
442489
view.selectedTaskId = task.id
443490
lastTask = task
491+
492+
if ([TASK_STATUS_STRING.scheduled, TASK_STATUS_STRING.running].includes(task?.status)) {
493+
getTask(task.id).then(response => {
494+
const updatedTask = response.data?.ocs?.data?.task
495+
496+
if (![TASK_STATUS_STRING.scheduled, TASK_STATUS_STRING.running].includes(updatedTask?.status)) {
497+
view.selectedTaskTypeId = updatedTask.type
498+
view.inputs = updatedTask.input
499+
view.outputs = updatedTask.status === TASK_STATUS_STRING.successful ? updatedTask.output : null
500+
view.selectedTaskId = updatedTask.id
501+
lastTask = updatedTask
502+
return
503+
}
504+
505+
view.loading = true
506+
view.showSyncTaskRunning = true
507+
view.progress = null
508+
view.expectedRuntime = (updatedTask?.completionExpectedAt - updatedTask?.scheduledAt) || null
509+
510+
const setProgress = (progress) => {
511+
view.progress = progress
512+
}
513+
pollTask(updatedTask.id, setProgress).then(finishedTask => {
514+
console.debug('pollTask.then', finishedTask)
515+
if (finishedTask.status === TASK_STATUS_STRING.successful) {
516+
view.outputs = finishedTask?.output
517+
view.selectedTaskId = finishedTask?.id
518+
} else if (finishedTask.status === TASK_STATUS_STRING.failed) {
519+
showError(t('assistant', 'Your task with ID {id} has failed', { id: finishedTask.id }))
520+
console.error('[assistant] Task failed', finishedTask)
521+
view.outputs = null
522+
}
523+
// resolve(finishedTask)
524+
view.loading = false
525+
view.showSyncTaskRunning = false
526+
}).catch(error => {
527+
console.debug('[assistant] poll error', error)
528+
})
529+
}).catch(error => {
530+
console.error(error)
531+
})
532+
}
444533
}
445534
})
446535
view.$on('new-task', () => {
@@ -469,6 +558,7 @@ export async function openAssistantTask(task, { isInsideViewer = undefined, acti
469558
view.$destroy()
470559
})
471560
view.$on('back-to-assistant', () => {
561+
cancelTaskPolling()
472562
view.showScheduleConfirmation = false
473563
view.showSyncTaskRunning = false
474564
view.loading = false

0 commit comments

Comments
 (0)