-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmain.go
More file actions
47 lines (36 loc) · 840 Bytes
/
Copy pathmain.go
File metadata and controls
47 lines (36 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"log/slog"
"os"
"time"
"github.com/robfig/cron/v3"
)
type cronLogger struct{}
func (t *cronLogger) Printf(format string, args ...interface{}) {
slog.Info(args[0].(string), append([]any{
"logGroup", "cron",
}, args[1:]...)...)
}
func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
})))
var err error
// main
spec := "0 0 0 * * *"
if len(os.Args) == 2 && os.Args[1] != "" {
spec = os.Args[1]
}
slog.Info("start", "spec", spec, "args", os.Args)
logger := &cronLogger{}
c := cron.New(cron.WithSeconds(), cron.WithLogger(cron.VerbosePrintfLogger(logger)))
c.Start()
_, err = c.AddFunc(spec, func() {
now := time.Now()
slog.Info("job triggered", "spec", spec, "now", now)
})
if err != nil {
panic(err)
}
select {}
}