mirror of
https://github.com/iflamed/schedule.git
synced 2025-12-24 12:38:01 +08:00
add logger interface
This commit is contained in:
17
scheduler.go
17
scheduler.go
@@ -4,7 +4,6 @@ package schedule
|
||||
import (
|
||||
"context"
|
||||
"github.com/golang-module/carbon/v2"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -20,6 +19,7 @@ type Scheduler struct {
|
||||
Next *NextTick
|
||||
limit *Limit
|
||||
count int32
|
||||
log Logger
|
||||
}
|
||||
|
||||
func NewScheduler(ctx context.Context, loc *time.Location) *Scheduler {
|
||||
@@ -30,6 +30,7 @@ func NewScheduler(ctx context.Context, loc *time.Location) *Scheduler {
|
||||
Next: &NextTick{},
|
||||
limit: &Limit{},
|
||||
count: 0,
|
||||
log: &DefaultLogger{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,12 +40,20 @@ func (s *Scheduler) Timezone(loc *time.Location) *Scheduler {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Scheduler) SetLogger(l Logger) *Scheduler {
|
||||
if l == nil {
|
||||
return s
|
||||
}
|
||||
s.log = l
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Scheduler) Start() {
|
||||
if atomic.LoadInt32(&s.count) > 0 {
|
||||
log.Printf("Wait for %d tasks finish... \n", s.count)
|
||||
s.log.Debugf("Wait for %d tasks finish... \n", s.count)
|
||||
}
|
||||
s.wg.Wait()
|
||||
log.Println("All tasks have been finished.")
|
||||
s.log.Debug("All tasks have been finished.")
|
||||
}
|
||||
|
||||
func (s *Scheduler) Call(t Task) {
|
||||
@@ -62,7 +71,7 @@ func (s *Scheduler) Call(t Task) {
|
||||
s.wg.Done()
|
||||
atomic.AddInt32(&s.count, -1)
|
||||
if r := recover(); r != nil {
|
||||
log.Println("Recovering schedule task from panic:", r)
|
||||
s.log.Error("Recovering schedule task from panic:", r)
|
||||
}
|
||||
}()
|
||||
t.Run(s.ctx)
|
||||
|
||||
@@ -756,6 +756,7 @@ func TestScheduler_When(t *testing.T) {
|
||||
|
||||
func TestScheduler_Call(t *testing.T) {
|
||||
s := NewScheduler(context.Background(), time.UTC)
|
||||
s.SetLogger(nil).SetLogger(&DefaultLogger{})
|
||||
dayTime := s.now.Format("15:04")
|
||||
ch := make(chan bool, 1)
|
||||
s.DailyAt(dayTime).When(func(ctx context.Context) bool {
|
||||
|
||||
22
task.go
22
task.go
@@ -3,6 +3,7 @@ package schedule
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -10,6 +11,12 @@ type Task interface {
|
||||
Run(ctx context.Context)
|
||||
}
|
||||
|
||||
type Logger interface {
|
||||
Error(msg string, e any)
|
||||
Debugf(msg string, n int32)
|
||||
Debug(msg string)
|
||||
}
|
||||
|
||||
type TaskFunc func(ctx context.Context)
|
||||
type WhenFunc func(ctx context.Context) bool
|
||||
|
||||
@@ -41,3 +48,18 @@ type Limit struct {
|
||||
IsBetween bool
|
||||
When WhenFunc
|
||||
}
|
||||
|
||||
type DefaultLogger struct {
|
||||
}
|
||||
|
||||
func (d *DefaultLogger) Error(msg string, r any) {
|
||||
log.Println(msg, r)
|
||||
}
|
||||
|
||||
func (d *DefaultLogger) Debug(msg string) {
|
||||
log.Println(msg)
|
||||
}
|
||||
|
||||
func (d *DefaultLogger) Debugf(msg string, i int32) {
|
||||
log.Printf(msg, i)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user