Skip to content

Commit

Permalink
Merge branch 'master' of github.com:libi/dcron into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
dxyinme committed Oct 19, 2023
2 parents ff86fbc + 04bd2b2 commit e10900e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
47 changes: 47 additions & 0 deletions dcron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,43 @@ func runSecondNode(id string, wg *sync.WaitGroup, runningTime time.Duration, t *
wg.Done()
}

func runSecondNodeWithLogger(id string, wg *sync.WaitGroup, runningTime time.Duration, t *testing.T) {
redisCli := redis.NewClient(&redis.Options{
Addr: DefaultRedisAddr,
})
drv := driver.NewRedisDriver(redisCli)
dcr := dcron.NewDcronWithOption(t.Name(), drv,
// must use `WithPrintLogInfo` before `WithLogger`
// because we need to set up `cron` log level, it depends
// on ths value of this configuration.
dcron.WithPrintLogInfo(),
dcron.CronOptionSeconds(),
dcron.WithLogger(&dlog.StdLogger{
Log: log.New(os.Stdout, "["+id+"]", log.LstdFlags),
}),
dcron.CronOptionChain(cron.Recover(
cron.DefaultLogger,
)),
)
var err error
err = dcr.AddFunc("job1", "*/5 * * * * *", func() {
t.Log(time.Now())
})
require.Nil(t, err)
err = dcr.AddFunc("job2", "*/8 * * * * *", func() {
panic("test panic")
})
require.Nil(t, err)
err = dcr.AddFunc("job3", "*/2 * * * * *", func() {
t.Log("job3:", time.Now())
})
require.Nil(t, err)
dcr.Start()
<-time.After(runningTime)
dcr.Stop()
wg.Done()
}

func Test_SecondJobWithPanicAndMultiNodes(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(5)
Expand Down Expand Up @@ -202,5 +239,15 @@ func Test_WithClusterStableNodes(t *testing.T) {
go startFunc("3", time.Second*6, t)
go startFunc("4", time.Second*6, t)
go startFunc("5", time.Second*6, t)
}

func Test_SecondJobLog_Issue68(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(5)
go runSecondNodeWithLogger("1", wg, 45*time.Second, t)
go runSecondNodeWithLogger("2", wg, 45*time.Second, t)
go runSecondNodeWithLogger("3", wg, 45*time.Second, t)
go runSecondNodeWithLogger("4", wg, 45*time.Second, t)
go runSecondNodeWithLogger("5", wg, 45*time.Second, t)
wg.Wait()
}
10 changes: 9 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ func WithLogger(logger dlog.Logger) Option {
//set dcron logger
dcron.logger = logger
//set cron logger
f := cron.WithLogger(cron.PrintfLogger(logger))
var cronLogger cron.Logger

if dcron.logInfo {
cronLogger = cron.VerbosePrintfLogger(logger)
} else {
cronLogger = cron.PrintfLogger(logger)
}

f := cron.WithLogger(cronLogger)
dcron.crOptions = append(dcron.crOptions, f)
}
}
Expand Down

0 comments on commit e10900e

Please sign in to comment.