fix(stats): fix crash when messages task fails, add Sentry integration

This commit is contained in:
spiral 2022-11-23 05:17:56 +00:00
parent 7ffe83ab5e
commit 898d23d733
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
2 changed files with 31 additions and 12 deletions

View File

@ -4,10 +4,21 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"runtime/debug"
"strings"
"time" "time"
"github.com/getsentry/sentry-go"
) )
func main() { func main() {
err := sentry.Init(sentry.ClientOptions{
Dsn: os.Getenv("SENTRY_DSN"),
})
if err != nil {
panic(err)
}
log.Println("connecting to databases") log.Println("connecting to databases")
connect_dbs() connect_dbs()
@ -41,7 +52,26 @@ func withtime(name string, todo func()) func() {
func doforever(dur time.Duration, todo func()) { func doforever(dur time.Duration, todo func()) {
for { for {
go todo() go wrapRecover(todo)
time.Sleep(dur) 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()
}

View File

@ -4,20 +4,9 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"runtime/debug"
"strings"
) )
func task_main() { 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") log.Println("running per-minute scheduled tasks")
update_db_meta() update_db_meta()