Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/issue 68 #69

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 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 All @@ -159,3 +196,14 @@ func Test_SecondJobWithStopAndSwapNode(t *testing.T) {
go runSecondNode("2", wg, 20*time.Second, t)
wg.Wait()
}

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