enonic / xp

Enonic XP
https://enonic.com
GNU General Public License v3.0
202 stars 34 forks source link

lib task cancel/stop method #4881

Closed ComLock closed 4 years ago

ComLock commented 7 years ago

Is it possible to cancel a task without restarting the Enonic XP server?

Or perhaps we need that functionality?

srs commented 7 years ago

I think this should be possible. @aro?

aro commented 7 years ago

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.

srs commented 7 years ago

Let's discuss how we could do this.

alansemenov commented 6 years ago

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.

sigdestad commented 6 years ago

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

ComLock commented 6 years ago

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.

ComLock commented 6 years ago

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.

alansemenov commented 4 years ago

We'll leave it with your way of solving the issue.