Merge branch 'split-action-button-methods' into 'v3'

Split up the action button methods for monitoring and starting a task

Closes #442

See merge request openflexure/openflexure-microscope-server!428
This commit is contained in:
Julian Stirling 2025-10-30 20:37:03 +00:00
commit 2bc8955f9c

View file

@ -227,16 +227,39 @@ export default {
} }
}, },
checkExistingTasks: function() { /* Check if an existing task had already started when this mounts.
axios.get(this.submitUrl).then(response => { *
for (const task of response.data) { * This is called on mounted.
if (task.status == "pending" || task.status == "running") { *
* It will emit taskStarted if it finds an ongoing task to allow parent components
* to act as expected if the task is started. It will then poll the action.
*
*/
async checkExistingTasks() {
let response;
try {
response = await axios.get(this.submitUrl);
} catch (error) {
console.warn("checkExistingTasks: request failed", error);
return;
}
// Check for a task that is ongoing.
// We can't handle multiple tasks ongoing, so this picks the first.
const ongoingTask = response.data.find(t => ["pending", "running"].includes(t.status));
if (ongoingTask) {
// There is a started task
this.taskStarted = true; this.taskStarted = true;
this.$emit("taskStarted"); this.$emit("taskStarted");
this.startPolling(task.id, task.links.find(t => t.rel == "self").href); // Find its URL
const taskUrl = ongoingTask.links.find(t => t.rel == "self").href;
try {
await this.pollOngoingTask(ongoingTask.id, taskUrl);
} catch (error) {
this.$emit("error", error);
} finally {
this.onTaskEnd();
} }
} }
});
}, },
bootstrapTask: function() { bootstrapTask: function() {
@ -266,8 +289,17 @@ export default {
if (this.modalProgress) { if (this.modalProgress) {
UIkit.modal(this.$refs.statusModal).show(); UIkit.modal(this.$refs.statusModal).show();
} }
await this.pollOngoingTask(response.data.id, response.data.href);
} catch (error) {
this.$emit("error", error);
} finally {
this.onTaskEnd();
}
},
async pollOngoingTask(taskId, taskUrl) {
// Start the store polling TaskId for success // Start the store polling TaskId for success
response = await this.startPolling(response.data.id, response.data.href); const response = await this.startPolling(taskId, taskUrl);
if (response.status == "completed") { if (response.status == "completed") {
this.$emit("response", response); this.$emit("response", response);
this.$emit("completed", response.output); this.$emit("completed", response.output);
@ -275,18 +307,13 @@ export default {
this.$emit("cancelled", response); this.$emit("cancelled", response);
this.modalNotify(`The action '${this.submitLabel}' was cancelled.`); this.modalNotify(`The action '${this.submitLabel}' was cancelled.`);
} }
} catch (error) { },
this.$emit("error", error);
} finally { onTaskEnd: function() {
// Reset taskRunning and taskId // Reset taskRunning and taskId
this.taskRunning = false; this.taskRunning = false;
this.taskStarted = false; this.taskStarted = false;
this.$emit("finished"); this.$emit("finished");
// Update the form data if we're self-updating
if (this.selfUpdate) {
this.getFormData();
}
}
}, },
startPolling: function(taskId, taskUrl) { startPolling: function(taskId, taskUrl) {
@ -330,7 +357,7 @@ export default {
} }
// As LabThings Actions add the message from any raised exception to the log, the // As LabThings Actions add the message from any raised exception to the log, the
// last message in the log is the message from the Exception. // last message in the log is the message from the Exception.
//If the Exception was raised with no message, use a default. // If the Exception was raised with no message, use a default.
else { else {
message = message =
response.data.log.from_index(-1).message || response.data.log.from_index(-1).message ||
@ -340,7 +367,7 @@ export default {
reject(new Error(message)); reject(new Error(message));
} }
// If task ends without reporting an error // If task ends without reporting an error
// (NB this includes cancellation) // (Note: this includes cancellation)
else { else {
resolve(response.data); resolve(response.data);
} }