Skip to content

Commit 28f6b17

Browse files
author
yumaojun03
committed
封装cron
1 parent 0b33575 commit 28f6b17

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/prometheus/client_golang v1.19.0
2727
github.com/redis/go-redis/extra/redisotel/v9 v9.0.5
2828
github.com/redis/go-redis/v9 v9.5.1
29+
github.com/robfig/cron/v3 v3.0.1
2930
github.com/rs/xid v1.5.0
3031
github.com/rs/zerolog v1.32.0
3132
github.com/segmentio/kafka-go v0.4.47

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLB
205205
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
206206
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
207207
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
208+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
209+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
208210
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
209211
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
210212
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=

ioc/config/mcron/cron.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package mcron
2+
3+
import (
4+
"github.com/infraboard/mcube/v2/ioc"
5+
"github.com/infraboard/mcube/v2/ioc/config/log"
6+
"github.com/robfig/cron/v3"
7+
"github.com/rs/zerolog"
8+
)
9+
10+
func init() {
11+
ioc.Default().Registry(defaultConfig)
12+
}
13+
14+
var defaultConfig = &config{
15+
cron: cron.New(cron.WithChain(
16+
cron.Recover(&LogWrapper{}),
17+
cron.SkipIfStillRunning(&LogWrapper{}),
18+
),
19+
cron.WithLogger(&LogWrapper{}),
20+
),
21+
}
22+
23+
type config struct {
24+
cron *cron.Cron
25+
ioc.ObjectImpl
26+
log *zerolog.Logger
27+
}
28+
29+
func (c *config) Name() string {
30+
return APP_NAME
31+
}
32+
33+
func (c *config) Priority() int {
34+
return PRIORITY
35+
}
36+
37+
func (c *config) Init() error {
38+
c.log = log.Sub(c.Name())
39+
c.cron.Start()
40+
return nil
41+
}

ioc/config/mcron/cron_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package mcron_test

ioc/config/mcron/interface.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package mcron
2+
3+
import (
4+
"github.com/infraboard/mcube/v2/ioc"
5+
"github.com/robfig/cron/v3"
6+
)
7+
8+
const (
9+
APP_NAME = "cron"
10+
PRIORITY = -199
11+
)
12+
13+
func Get() *cron.Cron {
14+
return ioc.Default().Get(APP_NAME).(*config).cron
15+
}
16+
17+
func RunAndAddFunc(spec string, cmd func()) (cron.EntryID, error) {
18+
cmd()
19+
20+
return Get().AddFunc(spec, cmd)
21+
}

ioc/config/mcron/log.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package mcron
2+
3+
import "github.com/infraboard/mcube/v2/ioc/config/log"
4+
5+
type LogWrapper struct {
6+
}
7+
8+
// Info logs routine messages about cron's operation.
9+
func (l *LogWrapper) Info(msg string, keysAndValues ...interface{}) {
10+
log.Sub(APP_NAME).Info().Msg(msg)
11+
}
12+
13+
// Error logs an error condition.
14+
func (l *LogWrapper) Error(err error, msg string, keysAndValues ...interface{}) {
15+
log.Sub(APP_NAME).Error().Msgf("%s, %s", err.Error(), msg)
16+
}

0 commit comments

Comments
 (0)