Commit d9e41e73 authored by Zhang Xiaoli's avatar Zhang Xiaoli
Browse files

Add automatic trigger module - cron, complete testing and integration.

parent f7a0d67a
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"]
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
# 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)
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
# comments
@every 1m,redis-cli
# @every 1m30s,mod1
\ No newline at end of file
module cron
go 1.19
require github.com/robfig/cron/v3 v3.0.1
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=
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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment