mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-21 10:14:26 +08:00
code refactor and add unit tests
This commit is contained in:
+18
-16
@@ -9,33 +9,35 @@ import (
|
||||
"github.com/mayswind/ezbookkeeping/pkg/utils"
|
||||
)
|
||||
|
||||
// CronJob represents the cron job instance
|
||||
type CronJob struct {
|
||||
Name string
|
||||
Description string
|
||||
Interval time.Duration
|
||||
Period CronJobPeriod
|
||||
Run func() error
|
||||
}
|
||||
|
||||
func (c *CronJob) doRun() {
|
||||
start := time.Now()
|
||||
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())
|
||||
return
|
||||
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())
|
||||
return
|
||||
}
|
||||
|
||||
currentInfo := fmt.Sprintf("ip: %s, startTime: %d", localAddr, time.Now().Unix())
|
||||
found, runningInfo := duplicatechecker.Container.GetOrSetCronJobRunningInfo(c.Name, currentInfo, c.Period.GetInterval())
|
||||
|
||||
if found {
|
||||
log.Warnf("[cron_job.doRun] job \"%s\" is already running (%s)", c.Name, runningInfo)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
currentInfo := fmt.Sprintf("ip: %s, startTime: %d", localAddr, time.Now().Unix())
|
||||
found, runningInfo := duplicatechecker.Container.GetOrSetCronJobRunningInfo(c.Name, currentInfo, c.Interval)
|
||||
|
||||
if found {
|
||||
log.Warnf("[cron_job.doRun] job \"%s\" is already running (%s)", c.Name, runningInfo)
|
||||
return
|
||||
}
|
||||
|
||||
err = c.Run()
|
||||
|
||||
duplicatechecker.Container.Current.RemoveCronJobRunningInfo(c.Name)
|
||||
err := c.Run()
|
||||
|
||||
now := time.Now()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user