fix(stats): fix crash when messages task fails, add Sentry integration
This commit is contained in:
parent
7ffe83ab5e
commit
898d23d733
@ -4,10 +4,21 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := sentry.Init(sentry.ClientOptions{
|
||||
Dsn: os.Getenv("SENTRY_DSN"),
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log.Println("connecting to databases")
|
||||
connect_dbs()
|
||||
|
||||
@ -41,7 +52,26 @@ func withtime(name string, todo func()) func() {
|
||||
|
||||
func doforever(dur time.Duration, todo func()) {
|
||||
for {
|
||||
go todo()
|
||||
go wrapRecover(todo)
|
||||
time.Sleep(dur)
|
||||
}
|
||||
}
|
||||
|
||||
func wrapRecover(todo func()) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
if val, ok := err.(error); ok {
|
||||
sentry.CaptureException(val)
|
||||
} else {
|
||||
sentry.CaptureMessage(fmt.Sprint("unknown error", err))
|
||||
}
|
||||
|
||||
stack := strings.Split(string(debug.Stack()), "\n")
|
||||
stack = stack[7:]
|
||||
log.Println("error running tasks:", err.(error).Error())
|
||||
fmt.Println(strings.Join(stack, "\n"))
|
||||
}
|
||||
}()
|
||||
|
||||
todo()
|
||||
}
|
||||
|
@ -4,20 +4,9 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func task_main() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
stack := strings.Split(string(debug.Stack()), "\n")
|
||||
stack = stack[7:]
|
||||
log.Println("error running tasks:", err.(error).Error())
|
||||
fmt.Println(strings.Join(stack, "\n"))
|
||||
}
|
||||
}()
|
||||
|
||||
log.Println("running per-minute scheduled tasks")
|
||||
|
||||
update_db_meta()
|
||||
|
Loading…
Reference in New Issue
Block a user