Monitor Scheduled Tasks in Go Gin Applications
GinThe Problem
Go Gin applications often run background goroutines for scheduled work. If these goroutines panic or hang, the web server keeps running but scheduled tasks stop.
The Solution
Use robfig/cron alongside Gin and add CronRabbit pings in each job function.
Pattern
Start the cron scheduler alongside your Gin server. Add monitoring pings inside each scheduled function.
Code Examples
Gin with cron monitoring
Gopackage main
import (
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
"net/http"
"time"
)
func main() {
client := &http.Client{Timeout: 10 * time.Second}
c := cron.New()
c.AddFunc("0 */6 * * *", func() {
client.Get("https://ping.cronrabbit.com/your-id/start")
if err := cleanupSessions(); err != nil {
client.Get("https://ping.cronrabbit.com/your-id/fail")
return
}
client.Get("https://ping.cronrabbit.com/your-id")
})
c.Start()
r := gin.Default()
r.Run(":8080")
}