mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-14 06:57:35 +08:00
code refactor
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/errs"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/log"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/settings"
|
||||
@@ -27,7 +28,7 @@ var (
|
||||
)
|
||||
|
||||
// InitializeCronJobSchedulerContainer initializes the cron job scheduler according to the config
|
||||
func InitializeCronJobSchedulerContainer(config *settings.Config, startScheduler bool) error {
|
||||
func InitializeCronJobSchedulerContainer(ctx core.Context, config *settings.Config, startScheduler bool) error {
|
||||
var err error
|
||||
|
||||
Container.scheduler, err = gocron.NewScheduler(
|
||||
@@ -39,7 +40,7 @@ func InitializeCronJobSchedulerContainer(config *settings.Config, startScheduler
|
||||
return err
|
||||
}
|
||||
|
||||
Container.registerAllJobs(config)
|
||||
Container.registerAllJobs(ctx, config)
|
||||
|
||||
if startScheduler {
|
||||
Container.scheduler.Start()
|
||||
@@ -75,13 +76,13 @@ func (c *CronJobSchedulerContainer) SyncRunJobNow(jobName string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CronJobSchedulerContainer) registerAllJobs(config *settings.Config) {
|
||||
func (c *CronJobSchedulerContainer) registerAllJobs(ctx core.Context, config *settings.Config) {
|
||||
if config.EnableRemoveExpiredTokens {
|
||||
Container.registerIntervalJob(RemoveExpiredTokensJob)
|
||||
Container.registerIntervalJob(ctx, RemoveExpiredTokensJob)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CronJobSchedulerContainer) registerIntervalJob(job *CronJob) {
|
||||
func (c *CronJobSchedulerContainer) registerIntervalJob(ctx core.Context, job *CronJob) {
|
||||
gocronJob, err := c.scheduler.NewJob(
|
||||
job.Period.ToJobDefinition(),
|
||||
gocron.NewTask(job.doRun),
|
||||
@@ -93,9 +94,9 @@ func (c *CronJobSchedulerContainer) registerIntervalJob(job *CronJob) {
|
||||
c.allJobs = append(c.allJobs, job)
|
||||
c.allJobsMap[job.Name] = job
|
||||
c.allGocronJobsMap[job.Name] = gocronJob
|
||||
log.Infof("[cron_container.registerJob] job \"%s\" has been registered", job.Name)
|
||||
log.Debugf("[cron_container.registerJob] job \"%s\" gocron id is %s", job.Name, gocronJob.ID())
|
||||
log.Infof(ctx, "[cron_container.registerJob] job \"%s\" has been registered", job.Name)
|
||||
log.Debugf(ctx, "[cron_container.registerJob] job \"%s\" gocron id is %s", job.Name, gocronJob.ID())
|
||||
} else {
|
||||
log.Errorf("[cron_container.registerJob] job \"%s\" cannot be been registered, because %s", job.Name, err.Error())
|
||||
log.Errorf(ctx, "[cron_container.registerJob] job \"%s\" cannot be been registered, because %s", job.Name, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/duplicatechecker"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/settings"
|
||||
)
|
||||
@@ -33,13 +34,13 @@ func TestCronJobSchedulerContainerRegisterIntervalJob(t *testing.T) {
|
||||
Period: CronJobIntervalPeriod{
|
||||
Interval: 1 * time.Second,
|
||||
},
|
||||
Run: func() error {
|
||||
Run: func(c *core.CronContext) error {
|
||||
actualValue = true
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
container.registerIntervalJob(job)
|
||||
container.registerIntervalJob(core.NewNullContext(), job)
|
||||
container.scheduler.Start()
|
||||
|
||||
assert.Equal(t, 1, len(container.GetAllJobs()))
|
||||
@@ -73,13 +74,13 @@ func TestCronJobSchedulerContainerSyncRunJobNow(t *testing.T) {
|
||||
Period: CronJobIntervalPeriod{
|
||||
Interval: 24 * time.Hour,
|
||||
},
|
||||
Run: func() error {
|
||||
Run: func(c *core.CronContext) error {
|
||||
actualValue = true
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
container.registerIntervalJob(job)
|
||||
container.registerIntervalJob(core.NewNullContext(), job)
|
||||
|
||||
err = container.SyncRunJobNow("TestSyncRunJob")
|
||||
assert.Nil(t, err)
|
||||
@@ -115,17 +116,17 @@ func TestCronJobSchedulerContainerRepeatRun(t *testing.T) {
|
||||
Period: CronJobFixedTimePeriod{
|
||||
Time: runTime,
|
||||
},
|
||||
Run: func() error {
|
||||
Run: func(c *core.CronContext) error {
|
||||
runCount.Add(1)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
container.registerIntervalJob(job)
|
||||
container.registerIntervalJob(job)
|
||||
container.registerIntervalJob(job)
|
||||
container.registerIntervalJob(job)
|
||||
container.registerIntervalJob(job)
|
||||
container.registerIntervalJob(core.NewNullContext(), job)
|
||||
container.registerIntervalJob(core.NewNullContext(), job)
|
||||
container.registerIntervalJob(core.NewNullContext(), job)
|
||||
container.registerIntervalJob(core.NewNullContext(), job)
|
||||
container.registerIntervalJob(core.NewNullContext(), job)
|
||||
container.scheduler.Start()
|
||||
|
||||
time.Sleep(10 * time.Second)
|
||||
|
||||
+10
-8
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/duplicatechecker"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/log"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/utils"
|
||||
@@ -14,38 +15,39 @@ type CronJob struct {
|
||||
Name string
|
||||
Description string
|
||||
Period CronJobPeriod
|
||||
Run func() error
|
||||
Run func(*core.CronContext) error
|
||||
}
|
||||
|
||||
func (c *CronJob) doRun() {
|
||||
func (j *CronJob) doRun() {
|
||||
start := time.Now()
|
||||
c := core.NewCronJobContext(j.Name)
|
||||
|
||||
if duplicatechecker.Container.Current != nil {
|
||||
localAddr, err := utils.GetLocalIPAddressesString()
|
||||
|
||||
if err != nil {
|
||||
log.Warnf("[cron_job.doRun] job \"%s\" cannot get local ipv4 address, because %s", c.Name, err.Error())
|
||||
log.Warnf(c, "[cron_job.doRun] job \"%s\" cannot get local ipv4 address, because %s", j.Name, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
currentInfo := fmt.Sprintf("ip: %s, startTime: %d", localAddr, time.Now().Unix())
|
||||
found, runningInfo := duplicatechecker.Container.GetOrSetCronJobRunningInfo(c.Name, currentInfo, c.Period.GetInterval())
|
||||
found, runningInfo := duplicatechecker.Container.GetOrSetCronJobRunningInfo(j.Name, currentInfo, j.Period.GetInterval())
|
||||
|
||||
if found {
|
||||
log.Warnf("[cron_job.doRun] job \"%s\" is already running (%s)", c.Name, runningInfo)
|
||||
log.Warnf(c, "[cron_job.doRun] job \"%s\" is already running (%s)", j.Name, runningInfo)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err := c.Run()
|
||||
err := j.Run(c)
|
||||
|
||||
now := time.Now()
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("[cron_job.doRun] failed to run job \"%s\", because %s", c.Name, err.Error())
|
||||
log.Errorf(c, "[cron_job.doRun] failed to run job \"%s\", because %s", j.Name, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
cost := now.Sub(start).Nanoseconds() / 1e6
|
||||
log.Infof("[cron_job.doRun] run job \"%s\" successfully, cost %dms", c.Name, cost)
|
||||
log.Infof(c, "[cron_job.doRun] run job \"%s\" successfully, cost %dms", j.Name, cost)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
)
|
||||
|
||||
func TestCronJobNextRunTimeWithIntervalPeriod(t *testing.T) {
|
||||
@@ -20,7 +22,7 @@ func TestCronJobNextRunTimeWithIntervalPeriod(t *testing.T) {
|
||||
Period: CronJobIntervalPeriod{
|
||||
Interval: 2*time.Hour + 34*time.Minute + 56*time.Second,
|
||||
},
|
||||
Run: func() error {
|
||||
Run: func(c *core.CronContext) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
@@ -64,7 +66,7 @@ func TestCronJobNextRunTimeWithFixedHourPeriod(t *testing.T) {
|
||||
Period: CronJobFixedHourPeriod{
|
||||
Hour: 0,
|
||||
},
|
||||
Run: func() error {
|
||||
Run: func(c *core.CronContext) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
@@ -109,7 +111,7 @@ func TestCronJobNextRunTimeWithFixedTimePeriod(t *testing.T) {
|
||||
Period: CronJobFixedTimePeriod{
|
||||
Time: expectedTime,
|
||||
},
|
||||
Run: func() error {
|
||||
Run: func(c *core.CronContext) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/services"
|
||||
)
|
||||
|
||||
@@ -11,7 +12,7 @@ var RemoveExpiredTokensJob = &CronJob{
|
||||
Period: CronJobFixedHourPeriod{
|
||||
Hour: 0,
|
||||
},
|
||||
Run: func() error {
|
||||
return services.Tokens.DeleteAllExpiredTokens(nil)
|
||||
Run: func(c *core.CronContext) error {
|
||||
return services.Tokens.DeleteAllExpiredTokens(c)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package cron
|
||||
import (
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/log"
|
||||
)
|
||||
|
||||
@@ -12,22 +13,22 @@ type GocronLoggerAdapter struct {
|
||||
|
||||
// Debug logs debug log
|
||||
func (logger GocronLoggerAdapter) Debug(msg string, args ...any) {
|
||||
log.Debugf(msg, args...)
|
||||
log.Debugf(core.NewNullContext(), msg, args...)
|
||||
}
|
||||
|
||||
// Info logs info log
|
||||
func (logger GocronLoggerAdapter) Info(msg string, args ...any) {
|
||||
log.Infof(msg, args...)
|
||||
log.Infof(core.NewNullContext(), msg, args...)
|
||||
}
|
||||
|
||||
// Warn logs warn log
|
||||
func (logger GocronLoggerAdapter) Warn(msg string, args ...any) {
|
||||
log.Warnf(msg, args...)
|
||||
log.Warnf(core.NewNullContext(), msg, args...)
|
||||
}
|
||||
|
||||
// Error logs error log
|
||||
func (logger GocronLoggerAdapter) Error(msg string, args ...any) {
|
||||
log.Errorf(msg, args...)
|
||||
log.Errorf(core.NewNullContext(), msg, args...)
|
||||
}
|
||||
|
||||
// NewGocronLoggerAdapter returns a new GocronLoggerAdapter instance
|
||||
|
||||
Reference in New Issue
Block a user