Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canceling a pool #24

Closed
Christilut opened this issue Dec 24, 2020 · 5 comments
Closed

Canceling a pool #24

Christilut opened this issue Dec 24, 2020 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@Christilut
Copy link

I'm running a pool that takes a while, is there any way to cancel it?

I guess I can do a workaround like this:

.process(async data => {
  if (canceled) return

})

But that would still iterate all input elements for nothing.

@marcuspoehls
Copy link
Member

@Christilut Hey Christiaan, cancelling a pool is possible with a bit of manual work.

At this point, there’s no method allowing you to manually stop the pool. What you can do as a workaround is throwing a custom error, like CancelPoolError. You must throw this error from the pool.process() method. Then, you must rethrow the error within a custom error handler. Thrown errors inside of the pool.handleError method will be passed through to your application.

Here’s a sample setup that may work for you:

try {
  const errors = []

  const { results } = await PromisePool
    .for(users)
    .handleError(async (error, user) => {
      if (error instanceof CancelPoolError) {
        throw error // Uncaught errors will stop the promise pool
      }

      errors.push(error) // you must collect errors yourself
    })
    .process(async data => {
      if (canceled) {
        throw new CancelPoolError()
      }
      
      // your processing
    })

  await handleCollected(errors) // this may throw

  return { results }
} catch (error) {
  // this should only be reached when the CancelPoolError was thrown
}

I’m happy to review and support you with a pull request. I won’t have time to implement a pool.cancel() method this year. I will be able to work on the cancelling feature in the beginning of 2021

@marcuspoehls marcuspoehls self-assigned this Dec 24, 2020
@marcuspoehls marcuspoehls added the enhancement New feature or request label Dec 24, 2020
@Christilut
Copy link
Author

Thanks for the super quick response :)

I'll use your workaround for now, it does the trick nicely.

@marcuspoehls
Copy link
Member

Sweet!

I'll close this issue and open a new one for the "stopping the pool" feature.

@Christilut
Copy link
Author

Awesome! Appreciate the quick and helpful response :)

@marcuspoehls
Copy link
Member

@Christilut You’re welcome! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants