mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 15:37:33 +08:00
support scheduled transaction (#2)
This commit is contained in:
@@ -80,6 +80,10 @@ func (c *CronJobSchedulerContainer) registerAllJobs(ctx core.Context, config *se
|
||||
if config.EnableRemoveExpiredTokens {
|
||||
Container.registerIntervalJob(ctx, RemoveExpiredTokensJob)
|
||||
}
|
||||
|
||||
if config.EnableCreateScheduledTransaction {
|
||||
Container.registerIntervalJob(ctx, CreateScheduledTransactionJob)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CronJobSchedulerContainer) registerIntervalJob(ctx core.Context, job *CronJob) {
|
||||
|
||||
@@ -20,7 +20,7 @@ type CronJob struct {
|
||||
|
||||
func (j *CronJob) doRun() {
|
||||
start := time.Now()
|
||||
c := core.NewCronJobContext(j.Name)
|
||||
c := core.NewCronJobContext(j.Name, j.Period.GetInterval())
|
||||
|
||||
if duplicatechecker.Container.Current != nil {
|
||||
localAddr, err := utils.GetLocalIPAddressesString()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
@@ -22,6 +23,11 @@ type CronJobFixedHourPeriod struct {
|
||||
Hour uint32
|
||||
}
|
||||
|
||||
// CronJobEvery15MinutesPeriod represents the period of execution at every 15 minutes
|
||||
type CronJobEvery15MinutesPeriod struct {
|
||||
Second uint32
|
||||
}
|
||||
|
||||
// CronJobFixedTimePeriod represents the period of execution at fixed time
|
||||
type CronJobFixedTimePeriod struct {
|
||||
Time time.Time
|
||||
@@ -52,6 +58,16 @@ func (p CronJobFixedHourPeriod) ToJobDefinition() gocron.JobDefinition {
|
||||
)
|
||||
}
|
||||
|
||||
// GetInterval returns the interval time of the period of CronJobEvery15MinutesPeriod
|
||||
func (p CronJobEvery15MinutesPeriod) GetInterval() time.Duration {
|
||||
return 15 * time.Minute
|
||||
}
|
||||
|
||||
// ToJobDefinition returns the gocron job definition of the period of CronJobEvery15MinutesPeriod
|
||||
func (p CronJobEvery15MinutesPeriod) ToJobDefinition() gocron.JobDefinition {
|
||||
return gocron.CronJob(fmt.Sprintf("%d */15 * * * *", p.Second), true)
|
||||
}
|
||||
|
||||
// GetInterval returns the interval time of the period of CronJobFixedTimePeriod
|
||||
func (p CronJobFixedTimePeriod) GetInterval() time.Duration {
|
||||
return 0
|
||||
|
||||
@@ -97,6 +97,61 @@ func TestCronJobNextRunTimeWithFixedHourPeriod(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestCronJobNextRunTimeWithEvery15MinutesPeriod(t *testing.T) {
|
||||
scheduler, err := gocron.NewScheduler(
|
||||
gocron.WithLocation(time.Local),
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
expectedSecond := uint32(23)
|
||||
|
||||
job := CronJob{
|
||||
Name: "TestCronJobWithEvery15MinutesPeriod",
|
||||
Description: "The test cron job",
|
||||
Period: CronJobEvery15MinutesPeriod{
|
||||
Second: expectedSecond,
|
||||
},
|
||||
Run: func(c *core.CronContext) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
assert.Equal(t, 15*time.Minute, job.Period.GetInterval())
|
||||
|
||||
gocronJob, err := scheduler.NewJob(
|
||||
job.Period.ToJobDefinition(),
|
||||
gocron.NewTask(job.doRun),
|
||||
gocron.WithName(job.Name),
|
||||
gocron.WithSingletonMode(gocron.LimitModeReschedule),
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
scheduler.Start()
|
||||
|
||||
nextRunTime, err := gocronJob.NextRun()
|
||||
assert.Nil(t, err)
|
||||
|
||||
nextMinuteTime := time.Now()
|
||||
|
||||
if (nextMinuteTime.Minute() == 0 || nextMinuteTime.Minute() == 15 || nextMinuteTime.Minute() == 30 || nextMinuteTime.Minute() == 45) && nextMinuteTime.Second() < int(expectedSecond) {
|
||||
// Do Nothing
|
||||
} else {
|
||||
nextMinute := ((nextMinuteTime.Minute() / 15) + 1) * 15
|
||||
minuteDiff := nextMinute - nextMinuteTime.Minute()
|
||||
nextMinuteTime = nextMinuteTime.Add(time.Duration(int64(minuteDiff) * int64(time.Minute)))
|
||||
}
|
||||
|
||||
assert.Equal(t, nextMinuteTime.Year(), nextRunTime.Year())
|
||||
assert.Equal(t, nextMinuteTime.Month(), nextRunTime.Month())
|
||||
assert.Equal(t, nextMinuteTime.Day(), nextRunTime.Day())
|
||||
assert.Equal(t, nextMinuteTime.Hour(), nextRunTime.Hour())
|
||||
assert.Equal(t, nextMinuteTime.Minute(), nextRunTime.Minute())
|
||||
assert.Equal(t, int(expectedSecond), nextRunTime.Second())
|
||||
|
||||
err = scheduler.Shutdown()
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestCronJobNextRunTimeWithFixedTimePeriod(t *testing.T) {
|
||||
scheduler, err := gocron.NewScheduler(
|
||||
gocron.WithLocation(time.Local),
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/services"
|
||||
)
|
||||
@@ -16,3 +18,15 @@ var RemoveExpiredTokensJob = &CronJob{
|
||||
return services.Tokens.DeleteAllExpiredTokens(c)
|
||||
},
|
||||
}
|
||||
|
||||
// CreateScheduledTransactionJob represents the cron job which periodically create transaction by scheduled transaction template
|
||||
var CreateScheduledTransactionJob = &CronJob{
|
||||
Name: "CreateScheduledTransaction",
|
||||
Description: "Periodically create transaction by scheduled transaction template.",
|
||||
Period: CronJobEvery15MinutesPeriod{
|
||||
Second: 0,
|
||||
},
|
||||
Run: func(c *core.CronContext) error {
|
||||
return services.Transactions.CreateScheduledTransactions(c, time.Now().Unix(), c.GetInterval())
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user