voucher/internal/server/cron.go

59 lines
1.0 KiB
Go

package server
import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/transport"
"github.com/robfig/cron"
"voucher/internal/conf"
"voucher/internal/service"
)
var _ transport.Server = (*CronServer)(nil)
type CronServer struct {
conf *conf.Bootstrap
cron *cron.Cron
VoucherService *service.VoucherService
}
func NewCronServer(
conf *conf.Bootstrap,
cron *cron.Cron,
VoucherService *service.VoucherService,
) *CronServer {
return &CronServer{
conf: conf,
cron: cron,
VoucherService: VoucherService,
}
}
func (cs *CronServer) Start(ctx context.Context) error {
if !cs.conf.Cron.IsOpen {
log.Warn("cron 未开启...")
return nil
}
if err := cs.VoucherService.CronNotice(ctx); err != nil {
log.Error("cron order notice Start err: %v", err)
return err
}
return nil
}
func (cs *CronServer) Stop(_ context.Context) error {
if !cs.conf.Cron.IsOpen {
return nil
}
fmt.Printf("cron 关闭中...")
cs.cron.Stop()
return nil
}