-
Notifications
You must be signed in to change notification settings - Fork 47
Error Handling? #39
Comments
I tried this but this doesn't return the error that caused the job to fail jobPool.SetAfterFunc(func(j *jobs.Job) {
err := j.Error()
}) |
@pyros2097 thanks for bringing this up. The documentation for
This means that the function will not be executed if there is an error. I'm sorry if that is not clear. Do you think we could improve the wording? Secondly, I'm not sure if this use case warrants the addition of a new feature. It seems like you should be able to write your own error handler function and then call it whenever there is an error. Something like this: func handleErr(j *jobs.Job, err error) {
// Log the error and do whatever else you need to do here.
}
// When registering the job, we can handle the error manually.
welcomeEmailJobs, err := jobs.RegisterType("welcomeEmail", 3, func(user *User) error {
msg := fmt.Sprintf("Hello, %s! Thanks for signing up for foo.com.", user.Name)
if err := emails.Send(user.EmailAddress, msg); err != nil {
// Before returning the erorr, call handleErr
handleErr(err)
return err
}
}) You know more about your code than I do. Do you think this would work? Or do you think we need a new feature for this? |
@albrow Yeah I guess the Anyway some of our jobs are very large and there are ton's of errors happening in the process. And since your RegisterType func already expects an error to be returned whenever some thing goes wrong in the handler it can send this error and the job to a default error handler which will notify us of the error and provide us more context on it and not just an error. We use echo router and it has a really neat error handling mechanism . See echo router's default error handling mechanism DefaultHTTPErrorHandler. We get the error and even a context object on where it happend and this could help us more in fixing bugs in production. We could do the |
We also need to have a common panic handler for the jobs since recently one of the jobs panic'ked and killed our server. So for now I'm manually doing a defer HandlePanic() for it just to be safe in each job. edit: Oh wait your's does have a panic handler this happened because of this https://github.com/carlescere/scheduler forgot to switch this in the other branch |
Just a heads up, catchError := func(process func() (*Context, error)) error {
defer HandlePanic()
context, err := process()
if err != nil {
MakeStackTrace(context, err)
}
logger.Warn("Job Error", "err", err)
return err
}
jobs.RegisterType("email", 3, func(params *Message) error {
return catchError(func() (*Context, error) {
}
} This does make it easier a liitle bit. |
Is it possible to add a common error handler for all jobs that return an error so that I can say log my errors during development and send mails during production. Because right now in development I just return the error and don't know when and where the job failed.
The text was updated successfully, but these errors were encountered: