mirror of
https://github.com/hibiken/asynq.git
synced 2026-06-14 07:56:58 +08:00
Protect handler call against panic
This commit is contained in:
14
processor.go
14
processor.go
@@ -88,7 +88,7 @@ func (p *processor) exec() {
|
||||
}
|
||||
<-p.sema // release token
|
||||
}()
|
||||
err := p.handler(task) // TODO(hibiken): maybe also handle panic?
|
||||
err := perform(p.handler, task)
|
||||
if err != nil {
|
||||
retryTask(p.rdb, msg, err)
|
||||
}
|
||||
@@ -103,3 +103,15 @@ func (p *processor) restore() {
|
||||
log.Printf("[ERROR] could not move tasks from %q to %q\n", inProgress, defaultQueue)
|
||||
}
|
||||
}
|
||||
|
||||
// perform calls the handler with the given task.
|
||||
// If the call returns without panic, it simply returns the value,
|
||||
// otherwise, it recovers from panic and returns an error.
|
||||
func perform(handler TaskHandler, task *Task) (err error) {
|
||||
defer func() {
|
||||
if x := recover(); x != nil {
|
||||
err = fmt.Errorf("panic: %v", x)
|
||||
}
|
||||
}()
|
||||
return handler(task)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user