threads.js icon indicating copy to clipboard operation
threads.js copied to clipboard

Clear runningTasks/Queued Tasks

Open anna-rmrf opened this issue 5 years ago • 6 comments

It would be better if there was an option to cancel runningTasks and also an option to empty the queue

anna-rmrf avatar Aug 04 '20 19:08 anna-rmrf

Hey @aya1337,

sorry, didn't get around to respond. I guess you are talking about the pool.

Can you describe in a little more detail what that method(s) would look like / what behavior you would expect. Sounds like you want a pool.cancelAll() method.

andywer avatar Aug 16 '20 10:08 andywer

@andywer Pretty much methods like:

<pool>.clearQueue() would clear the entire pool queue (tasks that are still waiting for a worker to be free) <pool>.cancelTasks() I named it cancelTasks because perhaps if you don't provide any parameter, it can cancel all running Tasks? and you can cancel a specific task by providing its ID?

It would be very helpful because I'm using theads.js in a very large service and sometimes, the tasks never resolve. A way to actually clear things and have everything fresh would be very helpful instead of killing the entire pool and re-establishing it

anna-rmrf avatar Aug 19 '20 12:08 anna-rmrf

I guess cancelPending() and cancelAll() would be good names. You can already cancel arbitrary tasks today, but not all of them at once.

I am just wondering if more people have a need for this feature? Also, are you sure that clearing all tasks is the best approach? You could probably just cancel the task that's stuck after some time.

andywer avatar Aug 20 '20 09:08 andywer

The thing is, some tasks can take longer than others, and the pool has 6/7 workers. And i can't really check which specific task is running on a certain worker and be able to cancel it

anna-rmrf avatar Aug 28 '20 12:08 anna-rmrf

And i can't really check which specific task is running on a certain worker and be able to cancel it

I admit it's not super convenient, but can you not do it like that?

async function runJobOnPool(pool, cancelAfterMs = 30000) {
  const task = pool.queue(async worker => {
    /* ... */
  })

  let taskCompleted = false
  const timeout = setTimeout(() => task.cancel(), cancelAfterMs)

  const onTaskSettled = () => {
    taskCompleted = true
    clearTimeout(timeout)
  }

  task.then(onTaskSettled, onTaskSettled)
  return task
}

andywer avatar Aug 28 '20 14:08 andywer

Need the feature to cancel runningTask!

jane-rose avatar Jun 13 '22 07:06 jane-rose