Closed ComLock closed 4 years ago
I think this should be possible. @aro?
We do not currently have a cancel function. I think it would be quite simple to implement it.
What I do now is to set some kind of cancel flag that is a private variable in the library. Then check the flag inside some loop in the task callback, so it can stop the task gracefully if requested.
Something like this:
var taskId;
exports.launchTask = function () {
taskId = taskLib.submit({
description: 'My task',
task: function () {
var id = taskId;
while (taskId === id) {
// do something
}
}
});
};
exports.cancelTask = function () {
taskId = null;
};
But this will not interrupt all kinds of execution, just when checked in the callback code. Also this is not cluster-safe.
Let's discuss how we could do this.
Is this still something we want to do? I think it looks fairly simple but we need a stoppable
flag for a task to prevent cancelling tasks like publishing and deletion.
We should keep this in the backlog, in terms of publishing the problem is really more of a "transaction" issue. Stopping tasks would normally not be done unless there are issues - one would never provide a "stop" action for publishing in the UI imho
Lets say for some reason one has an endless loop inside a task, this task then will continue "forever" consuming resources. Even when you stop the app, the task will continue running. In fact you will have to stop the Enonic XP server in order to kill the task. This is not a good situation in Production.
As a workaround I will use the taskName or taskId as the name for a node and write stop to that node. Then in the task loop I will check that node. This could potentially also be used for pause and resume (seperate loop with checknode sleep).
Depending upon how efficient it is to read a node versus calculating how long since last checked the node, I could check the node each iteration or some smart delay.
We'll leave it with your way of solving the issue.
Is it possible to cancel a task without restarting the Enonic XP server?
Or perhaps we need that functionality?