Skip to content

Commit

Permalink
fix: expand cron expr with environment variable in advance
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeguo committed Jan 19, 2024
1 parent 5ba4e74 commit 8fbf567
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
6 changes: 1 addition & 5 deletions pkg/mrunners/runner_cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mrunners

import (
"context"
"os"

"github.com/robfig/cron/v3"
"github.com/yankeguo/minit/pkg/munit"
Expand Down Expand Up @@ -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())
Expand Down
5 changes: 5 additions & 0 deletions pkg/munit/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
2 changes: 2 additions & 0 deletions pkg/munit/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/munit/testdata/test2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ command:
---
name: task-4
kind: cron
cron: "@every 10s"
cron: "@every ${DEBUG_EVERY}"
command:
- echo
- cron
Expand Down

0 comments on commit 8fbf567

Please sign in to comment.