From 5ba4e7499bc0245da12c044f6b2f0e96f8ca486e Mon Sep 17 00:00:00 2001 From: GUO YANKE Date: Fri, 19 Jan 2024 14:55:13 +0800 Subject: [PATCH 1/2] feat: add env support in cron expression, #9 --- pkg/mrunners/runner_cron.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/mrunners/runner_cron.go b/pkg/mrunners/runner_cron.go index 183fa73..42092af 100644 --- a/pkg/mrunners/runner_cron.go +++ b/pkg/mrunners/runner_cron.go @@ -2,6 +2,7 @@ package mrunners import ( "context" + "os" "github.com/robfig/cron/v3" "github.com/yankeguo/minit/pkg/munit" @@ -40,8 +41,11 @@ 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(r.Unit.Cron, func() { + + _, err := cr.AddFunc(expr, func() { r.Print("triggered") if err := r.Exec.Execute(r.Unit.ExecuteOptions(r.Logger)); err != nil { r.Error("failed executing: " + err.Error()) From 8fbf567b22968232562e876e1b0aaca705a37edd Mon Sep 17 00:00:00 2001 From: GUO YANKE Date: Fri, 19 Jan 2024 15:09:09 +0800 Subject: [PATCH 2/2] fix: expand cron expr with environment variable in advance --- pkg/mrunners/runner_cron.go | 6 +----- pkg/munit/loader.go | 5 +++++ pkg/munit/loader_test.go | 2 ++ pkg/munit/testdata/test2.yml | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) 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