批量空投
This commit is contained in:
74
internal/job/corns.go
Normal file
74
internal/job/corns.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package job
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"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,
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user