Skip to content

Commit

Permalink
feat: task cron config
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmzr committed May 17, 2024
1 parent 7f1318d commit 62c4a7f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ func TestPostProcessInitializerFailed(t *testing.T) {
func TestPostProcessTask(t *testing.T) {
initializers = make(map[string]Initializer)
tasks = make([]Task, 0)
RegisterTask("test", "0 0/1 * * * *", func() {})
viper.Set("task.cron.test", "0 0/1 * * * *")
RegisterTask("test", func() {})
if PostProcess() != nil {
t.Fail()
}
Expand All @@ -140,7 +141,7 @@ func TestPostProcessTask(t *testing.T) {
func TestPostProcessTaskFailed(t *testing.T) {
initializers = make(map[string]Initializer)
tasks = make([]Task, 0)
RegisterTask("test", "", func() {})
RegisterTask("testFailed", func() {})
if PostProcess() == nil {
t.Fail()
}
Expand Down
11 changes: 6 additions & 5 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"fmt"

"github.com/robfig/cron/v3"
"github.com/spf13/viper"
)

type Task struct {
Name string
Cron string
Handler func()
}

var tasks = make([]Task, 0)

func RegisterTask(name, cron string, handler func()) {
tasks = append(tasks, Task{Name: name, Cron: cron, Handler: handler})
func RegisterTask(name string, handler func()) {
tasks = append(tasks, Task{Name: name, Handler: handler})
}

func runTask() error {
Expand All @@ -25,8 +25,9 @@ func runTask() error {
}
cron := cron.New(cron.WithSeconds())
for _, task := range tasks {
Log(fmt.Sprintf("Add task [%s]: [%s]", task.Cron, task.Name))
_, err := cron.AddFunc(task.Cron, task.Handler)
configCron := viper.GetString("task.cron." + task.Name)
Log(fmt.Sprintf("Add task [%s]: [%s]", configCron, task.Name))
_, err := cron.AddFunc(configCron, task.Handler)
if err != nil {
return err
}
Expand Down

0 comments on commit 62c4a7f

Please sign in to comment.