Skip to content

Commit

Permalink
add immediate to cron
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeguo committed Jul 3, 2023
1 parent 2067cd9 commit cef5389
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ENV MINIT_MAIN_DIR="/work"
ENV MINIT_MAIN_NAME="main-program"
ENV MINIT_MAIN_GROUP="super-main"
ENV MINIT_MAIN_KIND="cron"
ENV MINIT_MAIN_IMMEDIATE=true
ENV MINIT_MAIN_CRON="* * * * *"
ENV MINIT_MAIN_CHARSET=gbk18030
```
Expand Down Expand Up @@ -139,6 +140,7 @@ command:
kind: cron
name: cron-demo
cron: "* * * * *" # cron expression, support extended syntax by https://github.com/robfig/cron
immediate: true # execute once on started
command:
- echo
- cron
Expand Down
5 changes: 4 additions & 1 deletion README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ ENTRYPOINT ["/minit"]
kind: cron
name: cron-sample
cron: "* * * * *"
immediate: true # 启动后立即执行一次
dir: /work # 指定工作目录
command:
- echo
Expand Down Expand Up @@ -182,7 +183,9 @@ MINIT_MAIN=redis-server /etc/redis.conf
MINIT_MAIN_DIR=/work
MINIT_MAIN_NAME=main-program
MINIT_MAIN_GROUP=super-main
MINIT_MAIN_ONCE=false
MINIT_MAIN_KIND=cron
MINIT_MAIN_CRON="* * * * *"
MINIT_MAIN_IMMEDIATE=true
MINIT_MAIN_CHARSET=gbk18030
```

Expand Down
6 changes: 6 additions & 0 deletions pkg/mrunners/runner_cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func (r *runnerCron) Do(ctx context.Context) {
r.Print("controller started")
defer r.Print("controller exited")

if r.Unit.Immediate {
if err := r.Exec.Execute(r.Unit.ExecuteOptions(r.Logger)); err != nil {
r.Error("failed executing: " + err.Error())
}
}

cr := cron.New(cron.WithLogger(cron.PrintfLogger(r.Logger)))
_, err := cr.AddFunc(r.Unit.Cron, func() {
r.Print("triggered")
Expand Down
22 changes: 14 additions & 8 deletions pkg/munit/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ func LoadEnv() (unit Unit, ok bool, err error) {
name = "env-main"
}

var cron string
var (
cron string
immediate bool
)

kind := strings.TrimSpace(os.Getenv("MINIT_MAIN_KIND"))

Expand All @@ -79,6 +82,8 @@ func LoadEnv() (unit Unit, ok bool, err error) {
err = errors.New("missing environment variable $MINIT_MAIN_CRON while $MINIT_MAIN_KIND is 'cron'")
return
}

immediate, _ = strconv.ParseBool(os.Getenv("MINIT_MAIN_IMMEDIATE"))
case "":
if once, _ := strconv.ParseBool(strings.TrimSpace(os.Getenv("MINIT_MAIN_ONCE"))); once {
kind = KindOnce
Expand All @@ -96,13 +101,14 @@ func LoadEnv() (unit Unit, ok bool, err error) {
}

unit = Unit{
Name: name,
Group: strings.TrimSpace(os.Getenv("MINIT_MAIN_GROUP")),
Kind: kind,
Cron: cron,
Command: cmds,
Dir: strings.TrimSpace(os.Getenv("MINIT_MAIN_DIR")),
Charset: strings.TrimSpace(os.Getenv("MINIT_MAIN_CHARSET")),
Name: name,
Group: strings.TrimSpace(os.Getenv("MINIT_MAIN_GROUP")),
Kind: kind,
Cron: cron,
Immediate: immediate,
Command: cmds,
Dir: strings.TrimSpace(os.Getenv("MINIT_MAIN_DIR")),
Charset: strings.TrimSpace(os.Getenv("MINIT_MAIN_CHARSET")),
}

ok = true
Expand Down
3 changes: 2 additions & 1 deletion pkg/munit/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type Unit struct {
Files []string `yaml:"files"` // files to process

// for 'cron' only
Cron string `yaml:"cron"` // cron syntax
Cron string `yaml:"cron"` // cron syntax
Immediate bool `yaml:"immediate"`
}

func (u Unit) RequireCommand() error {
Expand Down

0 comments on commit cef5389

Please sign in to comment.