init repo
This commit is contained in:
51
watcher/corn_service.go
Normal file
51
watcher/corn_service.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package watcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/nose7en/ToyBoomServer/common"
|
||||
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
Run()
|
||||
Stop()
|
||||
}
|
||||
|
||||
type client struct {
|
||||
s gocron.Scheduler
|
||||
}
|
||||
|
||||
func NewClient(f func() error) Client {
|
||||
c := context.Background()
|
||||
s, err := gocron.NewScheduler()
|
||||
if err != nil {
|
||||
common.Logger(c).WithError(err).Fatalf("create scheduler error")
|
||||
}
|
||||
|
||||
_, err = s.NewJob(
|
||||
gocron.CronJob("*/30 * * * * *", true),
|
||||
gocron.NewTask(f),
|
||||
)
|
||||
f()
|
||||
if err != nil {
|
||||
common.Logger(c).WithError(err).Fatalf("create job error")
|
||||
}
|
||||
return &client{
|
||||
s: s,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *client) Run() {
|
||||
ctx := context.Background()
|
||||
common.Logger(ctx).Infof("start to run scheduler, interval: 30s")
|
||||
c.s.Start()
|
||||
}
|
||||
|
||||
func (c *client) Stop() {
|
||||
ctx := context.Background()
|
||||
if err := c.s.Shutdown(); err != nil {
|
||||
common.Logger(ctx).WithError(err).Errorf("shutdown scheduler error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user