package job import ( "context" "errors" "github.com/robfig/cron/v3" "github.com/zeromicro/go-zero/core/logx" "nova_task/internal/job/check_points_stake" "nova_task/internal/job/earn" "nova_task/internal/job/holder" "nova_task/internal/job/stake_settle" "nova_task/internal/svc" ) // cronList 定时器Builder列表 var cronList = []func(context.Context, *svc.ServiceContext) cron.Job{ earn.NewCron, holder.NewCron, stake_settle.NewCron, //game_notify.NewCron, check_points_stake.NewCron, //tribally_report.NewCron, } type Corns struct { ctx context.Context cancel context.CancelFunc svcCtx *svc.ServiceContext c *cron.Cron } type Spec interface { Spec() string } // NewCorns 创建定时器服务 func newCorns(svcCtx *svc.ServiceContext) *Corns { ctx, cancel := context.WithCancel(context.Background()) var err error c := cron.New(cron.WithSeconds()) for _, cf := range cronList { cr := cf(ctx, svcCtx) if cs, ok := cr.(Spec); ok { spec := cs.Spec() if spec == "" { logx.Errorw("cron Corns spec is empty") continue } _, err = c.AddJob(spec, cr) logx.Must(err) continue } if cs, ok := cr.(cron.Schedule); ok { c.Schedule(cs, cr) continue } logx.Must(errors.New("cron Corns must implement either Spec or Schedule interface")) } return &Corns{ ctx: ctx, cancel: cancel, svcCtx: svcCtx, c: c, } } func (j *Corns) Start() { logx.Info("start cron Corns") j.c.Start() } func (j *Corns) Stop() { logx.Info("stop cron Corns") <-j.c.Stop().Done() logx.Info("cron Corns stopped") j.cancel() }