voucher/internal/service/cron.go

56 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"context"
"github.com/go-kratos/kratos/v2/log"
"time"
)
func (s *VoucherService) CronOrderNotice(ctx context.Context) error {
c, ok := s.bc.Cron.CommandMap["orderNotice"]
if !ok {
log.Warn("orderNotice定时任务未找到")
return nil
}
if !c.IsOpen {
log.Warn("orderNotice定时任务未开启")
return nil
}
if len(c.Command) == 0 {
log.Error("orderNotice定时任务 command is empty")
return nil
}
// 每五秒: */5 * * * * ?
// 每隔1分钟执行一次"0 */1 * * * ?"
// 每天23点执行一次"0 0 23 * * ?"
// 每天凌晨1点执行一次"0 0 1 * * ?"
// 每月1号凌晨1点执行一次"0 0 1 1 * ?"
// 在26分、29分、33分执行一次"0 26,29,33 * * * ?"
// 每天的0点、13点、18点、21点都执行一次"0 0 0,13,18,21 * * ?"
return s.cron.AddFunc(c.Command, func() {
s.OrderNotice(ctx)
})
}
func (s *VoucherService) OrderNotice(ctx context.Context) {
start := time.Now()
if err := s.VoucherBiz.Notice(ctx); err != nil {
log.Error("订单定时通知,执行失败,err: %v", err)
}
end := time.Now()
elapsed := end.Sub(start)
log.Warnf("订单定时通知,开始执行时间%s,执行结束时间%s,代码块执行耗时: %s", start.Format(time.DateTime), end.Format(time.DateTime), elapsed)
return
}