Created
February 18, 2025 16:01
-
-
Save manuelarte/e33cec46cd8653a5e2731687041a051a to your computer and use it in GitHub Desktop.
gocron locker next run bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"errors" | |
"fmt" | |
"github.com/gin-gonic/gin" | |
"github.com/go-co-op/gocron/v2" | |
) | |
func main() { | |
s, _ := gocron.NewScheduler(gocron.WithDistributedLocker(AlwaysOnLocker{})) | |
j, _ := s.NewJob( | |
gocron.CronJob("*/5 * * * * *", true), | |
gocron.NewTask( | |
func(a string, b int) { | |
fmt.Println("Job running") | |
}, | |
"hello", | |
1, | |
), | |
) | |
fmt.Println(j.ID()) | |
// start the scheduler | |
s.Start() | |
r := gin.Default() | |
r.GET("/job", jobHandler(j)) | |
//listen and serve on 0.0.0.0:8080 | |
_ = r.Run() | |
_ = s.Shutdown() | |
} | |
func jobHandler(job gocron.Job) func(c *gin.Context) { | |
return func(c *gin.Context) { | |
lastRun, _ := job.LastRun() | |
nextRun, _ := job.NextRun() | |
c.JSON(200, gin.H{ | |
"lastRun": lastRun, | |
"nextRun": nextRun, | |
}) | |
} | |
} | |
var _ gocron.Locker = new(AlwaysOnLocker) | |
type AlwaysOnLocker struct{} | |
func (a AlwaysOnLocker) Lock(_ context.Context, _ string) (gocron.Lock, error) { | |
return nil, errors.New("AlwaysOnLocker") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment