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:
commit
2bc8955f9c
1 changed files with 55 additions and 28 deletions
|
|
@ -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") {
|
*
|
||||||
this.taskStarted = true;
|
* It will emit taskStarted if it finds an ongoing task to allow parent components
|
||||||
this.$emit("taskStarted");
|
* to act as expected if the task is started. It will then poll the action.
|
||||||
this.startPolling(task.id, task.links.find(t => t.rel == "self").href);
|
*
|
||||||
}
|
*/
|
||||||
|
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.$emit("taskStarted");
|
||||||
|
// 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,29 +289,33 @@ export default {
|
||||||
if (this.modalProgress) {
|
if (this.modalProgress) {
|
||||||
UIkit.modal(this.$refs.statusModal).show();
|
UIkit.modal(this.$refs.statusModal).show();
|
||||||
}
|
}
|
||||||
// Start the store polling TaskId for success
|
await this.pollOngoingTask(response.data.id, response.data.href);
|
||||||
response = await this.startPolling(response.data.id, response.data.href);
|
|
||||||
if (response.status == "completed") {
|
|
||||||
this.$emit("response", response);
|
|
||||||
this.$emit("completed", response.output);
|
|
||||||
} else if (response.status == "cancelled") {
|
|
||||||
this.$emit("cancelled", response);
|
|
||||||
this.modalNotify(`The action '${this.submitLabel}' was cancelled.`);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.$emit("error", error);
|
this.$emit("error", error);
|
||||||
} finally {
|
} finally {
|
||||||
// Reset taskRunning and taskId
|
this.onTaskEnd();
|
||||||
this.taskRunning = false;
|
|
||||||
this.taskStarted = false;
|
|
||||||
this.$emit("finished");
|
|
||||||
// Update the form data if we're self-updating
|
|
||||||
if (this.selfUpdate) {
|
|
||||||
this.getFormData();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async pollOngoingTask(taskId, taskUrl) {
|
||||||
|
// Start the store polling TaskId for success
|
||||||
|
const response = await this.startPolling(taskId, taskUrl);
|
||||||
|
if (response.status == "completed") {
|
||||||
|
this.$emit("response", response);
|
||||||
|
this.$emit("completed", response.output);
|
||||||
|
} else if (response.status == "cancelled") {
|
||||||
|
this.$emit("cancelled", response);
|
||||||
|
this.modalNotify(`The action '${this.submitLabel}' was cancelled.`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
onTaskEnd: function() {
|
||||||
|
// Reset taskRunning and taskId
|
||||||
|
this.taskRunning = false;
|
||||||
|
this.taskStarted = false;
|
||||||
|
this.$emit("finished");
|
||||||
|
},
|
||||||
|
|
||||||
startPolling: function(taskId, taskUrl) {
|
startPolling: function(taskId, taskUrl) {
|
||||||
if (this.taskRunning != true) {
|
if (this.taskRunning != true) {
|
||||||
// Starts polling an existing Action task
|
// Starts polling an existing Action task
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue