mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-18 00:34:28 +08:00
code refactor
This commit is contained in:
+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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user