浏览代码

feat: gocron panic handler

weifeng 1 年之前
父节点
当前提交
31893ca983
共有 1 个文件被更改,包括 6 次插入0 次删除
  1. 6 0
      internal/server/task.go

+ 6 - 0
internal/server/task.go

@@ -19,8 +19,14 @@ func NewTask(log *log.Logger) *Task {
 	}
 	}
 }
 }
 func (t *Task) Start(ctx context.Context) error {
 func (t *Task) Start(ctx context.Context) error {
+	gocron.SetPanicHandler(func(jobName string, recoverData interface{}) {
+		t.log.Error("Task Panic", zap.String("job", jobName), zap.Any("recover", recoverData))
+	})
+
 	// eg: crontab task
 	// eg: crontab task
 	t.scheduler = gocron.NewScheduler(time.UTC)
 	t.scheduler = gocron.NewScheduler(time.UTC)
+	// if you are in China, you will need to change the time zone as follows
+	// t.scheduler = gocron.NewScheduler(time.FixedZone("PRC", 8*60*60))
 
 
 	_, err := t.scheduler.CronWithSeconds("0/3 * * * * *").Do(func() {
 	_, err := t.scheduler.CronWithSeconds("0/3 * * * * *").Do(func() {
 		t.log.Info("I'm a Task1.")
 		t.log.Info("I'm a Task1.")