-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cron | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"time" | ||
|
||
"github.com/go-courier/logr" | ||
"github.com/innoai-tech/infra/pkg/configuration" | ||
"github.com/pkg/errors" | ||
"github.com/robfig/cron/v3" | ||
) | ||
|
||
type IntervalSchedule struct { | ||
Interval time.Duration | ||
} | ||
|
||
func (i IntervalSchedule) Next(t time.Time) time.Time { | ||
return t.Add(i.Interval) | ||
} | ||
|
||
type Job struct { | ||
Cron string `flag:",omitempty"` | ||
|
||
schedule cron.Schedule | ||
timer *time.Timer | ||
|
||
name string | ||
action func(ctx context.Context) | ||
} | ||
|
||
func (j *Job) SetDefaults() { | ||
if j.Cron == "" { | ||
// 每周一 | ||
// "https://crontab.guru/#0_0_*_*_1" | ||
j.Cron = "0 0 * * 1" | ||
} | ||
} | ||
|
||
func (j *Job) ApplyAction(name string, action func(ctx context.Context)) { | ||
j.name = name | ||
j.action = action | ||
} | ||
|
||
func (j *Job) Init(ctx context.Context) error { | ||
schedule, err := cron.ParseStandard(j.Cron) | ||
if err != nil { | ||
return errors.Wrapf(err, "parse cron failed: %s", j.Cron) | ||
} | ||
j.schedule = schedule | ||
return nil | ||
} | ||
|
||
var _ configuration.Server = (*Job)(nil) | ||
|
||
func (j *Job) Serve(ctx context.Context) error { | ||
ci := configuration.ContextInjectorFromContext(ctx) | ||
|
||
logr.FromContext(ctx).WithValues( | ||
slog.String("name", j.name), | ||
slog.String("cron", j.Cron), | ||
).Info("waiting") | ||
|
||
j.timer = time.NewTimer(5 * time.Second) | ||
|
||
for { | ||
now := time.Now() | ||
|
||
j.timer.Reset(j.schedule.Next(now).Sub(now)) | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case now = <-j.timer.C: | ||
if j.action != nil { | ||
go func() { | ||
j.action(ci.InjectContext(context.Background())) | ||
}() | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (j *Job) Shutdown(context.Context) error { | ||
if j.timer != nil { | ||
j.timer.Stop() | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cron | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestJob(t *testing.T) { | ||
job := &Job{} | ||
_ = job.Init(context.Background()) | ||
job.schedule = IntervalSchedule{ | ||
Interval: 50 * time.Millisecond, | ||
} | ||
|
||
t.Cleanup(func() { | ||
_ = job.Shutdown(context.Background()) | ||
}) | ||
|
||
v := int64(0) | ||
done := make(chan struct{}) | ||
|
||
job.ApplyAction("test", func(ctx context.Context) { | ||
defer func() { | ||
if atomic.LoadInt64(&v) >= 5 { | ||
done <- struct{}{} | ||
} | ||
}() | ||
|
||
atomic.AddInt64(&v, 1) | ||
fmt.Println(atomic.LoadInt64(&v)) | ||
}) | ||
|
||
go func() { | ||
_ = job.Serve(context.Background()) | ||
}() | ||
|
||
<-done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Package cron GENERATED BY gengo:runtimedoc | ||
DON'T EDIT THIS FILE | ||
*/ | ||
package cron | ||
|
||
// nolint:deadcode,unused | ||
func runtimeDoc(v any, names ...string) ([]string, bool) { | ||
if c, ok := v.(interface { | ||
RuntimeDoc(names ...string) ([]string, bool) | ||
}); ok { | ||
return c.RuntimeDoc(names...) | ||
} | ||
return nil, false | ||
} | ||
|
||
func (v IntervalSchedule) RuntimeDoc(names ...string) ([]string, bool) { | ||
if len(names) > 0 { | ||
switch names[0] { | ||
case "Interval": | ||
return []string{}, true | ||
|
||
} | ||
|
||
return nil, false | ||
} | ||
return []string{}, true | ||
} | ||
|
||
func (v Job) RuntimeDoc(names ...string) ([]string, bool) { | ||
if len(names) > 0 { | ||
switch names[0] { | ||
case "Cron": | ||
return []string{}, true | ||
|
||
} | ||
|
||
return nil, false | ||
} | ||
return []string{}, true | ||
} |