How to log pool goroutine panic without blocking on task.Wait() #112
sandykellagher
started this conversation in
General
Replies: 1 comment 2 replies
-
|
Hi @sandykellagher! At the moment, the library doesn't provide a way to execute an arbitrary function when a task completes or panics, but it could be a nice addition. func taskLogger[R any](task func()) func() {
return func() {
defer func() {
if p := recover(); p != nil {
log.Printf("Task panicked: %v, %s\n", p, debug.Stack())
}
}()
// Execute task
task()
log.Println("Task completed")
}
}And you could use it as follows: pool := pond.NewPool(s.numWorkers)
for msg := range s.queue {
pool.Submit(taskLogger(func() {
doProcess(msg)
}))
}
// Stop the pool and wait for tasks to complete
pool.StopAndWait()Alternatively, if you don't want to stop the pool, you could use a group: pool := pond.NewPool(s.numWorkers)
group := pool.NewGroup()
for msg := range s.queue {
group.Submit(taskLogger(func() {
doProcess(msg)
}))
}
// Wait for tasks to complete
group.Wait()Please let me know if this workaround could work for your use case. But I think we could add something to the library to do this without a wrapper. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I have a worker pool processing tasks read from a channel
If
doProcessshould ever fail, eg panic, this is nicely wrapped and handled by the wrapper. But this means there is no log warning message of any kind.But if the code does a task.Wait() to check for an error (as shown below), then the items being pulled off the channel end up being processed in series rather than in parallel.
I could spawn a goroutine for each
pool.Submitbut that seems very heavy-handed.Is there an elegant way to log any task failure that might occur, while still submitting tasks in parallel. For example, a callback function that can be set. Failing that, arguably it'd be preferable not to wrap the panic, so that we get a log message if a code panic occurs.
Thanks
Sandy
Beta Was this translation helpful? Give feedback.
All reactions