jasonlvhit / gocron

A Golang Job Scheduling Package.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add a panic-safe version of Do

preslavrachev opened this issue · comments

Panics in Go can crash the entire app if not handled correctly. As Go developers, we try to do as much as possible to catch our errors and provide a graceful escape, but no Go app is really panic-safe. Mainly, because we often rely on 3rd party code, whose error handling we cannot guarantee until it is too late.

My proposal is to add a second method alongside Do (e.g. DoSafely), which delegates to Do but makes sure to gracefully handle sudden panics, using the well-known panic-defer method:

defer func() {
    if err := recover(); err != nil {
        log.Printf("Internal panic occurred: %s", err)
    }
}()

This is similar to how the default implementation of http.Server handles unexpected panics, and in the case of gocron, will give users a chance to decide whether to use it or not.

P.S. I am preparing a small PR and will refer to this issue later today. Feel free to merge it, if you like the idea.

Cool! I agree and don't like the idea of panic-ing without proper handling too. Waiting for your PR

@Streppel PR is here. Feel free to check it out

@preslavrachev thank you for collaborating! Already merged your changes.