diff --git a/L1/dockerfiles/cron/Dockerfile b/L1/dockerfiles/cron/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..62dd87b811836efc548e577508cca893b6c7bc01 --- /dev/null +++ b/L1/dockerfiles/cron/Dockerfile @@ -0,0 +1,15 @@ +FROM golang:1.20.3 + +ARG GOPROXY=https://goproxy.cn + +COPY . /src/ +RUN --mount=type=cache,target=/go/pkg \ + cd /src && go build main.go && strip main + +FROM hub.cstcloud.cn/scalebox/agent + +COPY --from=0 /src/main /app/bin/cron + +ENV ACTION_RUN=/app/bin/cron + +ENTRYPOINT ["goagent"] diff --git a/L1/dockerfiles/cron/Makefile b/L1/dockerfiles/cron/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..caecf67568d08048b271798c593b63cde98135cd --- /dev/null +++ b/L1/dockerfiles/cron/Makefile @@ -0,0 +1,13 @@ +IMAGE_NAME:=hub.cstcloud.cn/scalebox/cron + +build: + DOCKER_BUILDKIT=1 docker build --network=host -t $(IMAGE_NAME) . + +push: + docker push $(IMAGE_NAME) + +clean: + docker rmi $(IMAGE_NAME) + +run: + scalebox app create diff --git a/L1/dockerfiles/cron/README.md b/L1/dockerfiles/cron/README.md new file mode 100644 index 0000000000000000000000000000000000000000..4f085efee98a0c3d8e52a17ab8e056e4de675569 --- /dev/null +++ b/L1/dockerfiles/cron/README.md @@ -0,0 +1,23 @@ +# cron module + +## Introduction +The cron module is a scalebox public module similar to the cron function under UNIX, which periodically sends messages to each sub-module to start related timing operations. + +## Usage + +Define timing operations for multiple modules through the file cron.txt. + +In the cron.txt file, lines beginning with '#' are comment lines. + +Each line defines the timing operation for a module, including timing operation interval definition and module name, separated by commas. + +A sample cron.txt file is as follows: +``` +# comments for cron.txt +@every 1m,mod0 +@every 1m30s,mod1 +``` + +The definition part of the timing operation interval refers to: +[cron-doc](https://pkg.go.dev/github.com/robfig/cron) + diff --git a/L1/dockerfiles/cron/app.yaml b/L1/dockerfiles/cron/app.yaml new file mode 100644 index 0000000000000000000000000000000000000000..22f043268306be684465b1f6901acec17778200d --- /dev/null +++ b/L1/dockerfiles/cron/app.yaml @@ -0,0 +1,21 @@ +name: cron.scalebox +cluster: local +parameters: + initial_status: RUNNING + +jobs: + filelist: + base_image: hub.cstcloud.cn/scalebox/cron + schedule_mode: HEAD + parameters: + start_message: ANY + paths: + - ${PWD}/cron.txt:/cron.txt:ro + sink_jobs: + - mod0 + - mod1 + + mod0: + base_image: hub.cstcloud.cn/scalebox/agent + mod1: + base_image: hub.cstcloud.cn/scalebox/agent diff --git a/L1/dockerfiles/cron/cron.txt b/L1/dockerfiles/cron/cron.txt new file mode 100644 index 0000000000000000000000000000000000000000..1ad390b2238843efdde0783283c3ebfff3891db0 --- /dev/null +++ b/L1/dockerfiles/cron/cron.txt @@ -0,0 +1,3 @@ +# comments +@every 1m,redis-cli +# @every 1m30s,mod1 \ No newline at end of file diff --git a/L1/dockerfiles/cron/go.mod b/L1/dockerfiles/cron/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..b158fa34bebb9578562f0a717cff0f537cd14e03 --- /dev/null +++ b/L1/dockerfiles/cron/go.mod @@ -0,0 +1,5 @@ +module cron + +go 1.19 + +require github.com/robfig/cron/v3 v3.0.1 diff --git a/L1/dockerfiles/cron/go.sum b/L1/dockerfiles/cron/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..06678072cfbfd68e31f2ea52d0ce5c2861429bf8 --- /dev/null +++ b/L1/dockerfiles/cron/go.sum @@ -0,0 +1,2 @@ +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= diff --git a/L1/dockerfiles/cron/main.go b/L1/dockerfiles/cron/main.go new file mode 100644 index 0000000000000000000000000000000000000000..d81214d93d069544e534a30b18a3f36cf6c5ea9e --- /dev/null +++ b/L1/dockerfiles/cron/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "os/exec" + "strings" + "time" + + "github.com/robfig/cron/v3" +) + +type cronItem struct { + cronText string + modName string +} + +func main() { + var cronItems []cronItem + + file, err := os.Open("/cron.txt") + if err != nil { + fmt.Fprintf(os.Stderr, "Open file error:%v", err) + os.Exit(1) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + s := strings.TrimSpace(scanner.Text()) + if len(s) > 0 && s[0] != '#' { // not comment + ss := strings.Split(s, ",") + if len(ss) != 2 { + fmt.Fprintf(os.Stderr, "format error: %s\n", s) + continue + } + cronItems = append(cronItems, cronItem{cronText: ss[0], modName: ss[1]}) + } + } + + if err := scanner.Err(); err != nil { + fmt.Fprintf(os.Stderr, "scan file error:%v", err) + os.Exit(2) + } + + c := cron.New() + + for _, item := range cronItems { + // CAUTION: local variable needed + modName := item.modName + c.AddFunc(item.cronText, func() { + current := time.Now().Format("2006-01-02T15:04:05") + cmd := exec.Command("send-job-message", modName, current) + output, err := cmd.CombinedOutput() + if err != nil { + fmt.Fprintf(os.Stderr, "send-job-message error:%v", err) + } + fmt.Println(string(output)) + }) + } + + c.Start() + // block main goroutine + ch := make(chan struct{}) + <-ch +}