diff --git a/pkg/mrunners/runner_cron.go b/pkg/mrunners/runner_cron.go index 42092af..183fa73 100644 --- a/pkg/mrunners/runner_cron.go +++ b/pkg/mrunners/runner_cron.go @@ -2,7 +2,6 @@ package mrunners import ( "context" - "os" "github.com/robfig/cron/v3" "github.com/yankeguo/minit/pkg/munit" @@ -41,11 +40,8 @@ func (r *runnerCron) Do(ctx context.Context) { } } - expr := os.ExpandEnv(r.Unit.Cron) - cr := cron.New(cron.WithLogger(cron.PrintfLogger(r.Logger))) - - _, err := cr.AddFunc(expr, func() { + _, err := cr.AddFunc(r.Unit.Cron, func() { r.Print("triggered") if err := r.Exec.Execute(r.Unit.ExecuteOptions(r.Logger)); err != nil { r.Error("failed executing: " + err.Error()) diff --git a/pkg/munit/loader.go b/pkg/munit/loader.go index 6cb3c03..dacb401 100644 --- a/pkg/munit/loader.go +++ b/pkg/munit/loader.go @@ -126,6 +126,11 @@ func (ld *Loader) Load(opts LoadOptions) (output []Unit, skipped []Unit, err err continue } + // eval cron + if unit.Cron != "" { + unit.Cron = os.ExpandEnv(unit.Cron) + } + // replicas if unit.Count > 1 { for i := 0; i < unit.Count; i++ { diff --git a/pkg/munit/loader_test.go b/pkg/munit/loader_test.go index 51da196..e5ed5e9 100644 --- a/pkg/munit/loader_test.go +++ b/pkg/munit/loader_test.go @@ -10,6 +10,7 @@ import ( func TestNewLoader(t *testing.T) { os.Setenv("MINIT_ENABLE", "@default") os.Setenv("MINIT_DISABLE", "task-3,task-5") + os.Setenv("DEBUG_EVERY", "10s") ld := NewLoader() units, skipped, err := ld.Load(LoadOptions{ Dir: "testdata", @@ -19,6 +20,7 @@ func TestNewLoader(t *testing.T) { require.Len(t, units, 1) require.Len(t, skipped, 4) require.Equal(t, "task-4", units[0].Name) + require.Equal(t, "@every 10s", units[0].Cron) } func TestDupOrMakeMap(t *testing.T) { diff --git a/pkg/munit/testdata/test2.yml b/pkg/munit/testdata/test2.yml index 5ee53a8..5523534 100644 --- a/pkg/munit/testdata/test2.yml +++ b/pkg/munit/testdata/test2.yml @@ -7,7 +7,7 @@ command: --- name: task-4 kind: cron -cron: "@every 10s" +cron: "@every ${DEBUG_EVERY}" command: - echo - cron