Skip to content

Commit 9f7a141

Browse files
authored
Merge pull request #30 from rizalgowandy/arwego/feat/cronx_sidecar
Implement sidecar pattern on cronx
2 parents e58bc07 + 89f154f commit 9f7a141

File tree

19 files changed

+346
-895
lines changed

19 files changed

+346
-895
lines changed

.air.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tmp_dir = "bin"
77

88
[build]
99
# Just plain old shell command. You could use `make` as well.
10-
cmd = "go build -o ./bin/main ./pkg/cronx/example/with-library-server"
10+
cmd = "go build -o ./bin/main ./pkg/cronx/example"
1111
# Binary file yields from `cmd`.
1212
bin = "bin/main"
1313
# Customize binary.

.golangci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ linters:
7474
- depguard
7575
- dupl
7676
- errcheck
77-
- exhaustive
7877
- funlen
7978
- gochecknoinits
8079
- goconst
@@ -83,7 +82,6 @@ linters:
8382
- gofmt
8483
- goimports
8584
- golint
86-
- gomnd
8785
- goprintffuncname
8886
- gosec
8987
- gosimple
@@ -120,6 +118,8 @@ linters:
120118
# - prealloc
121119
# - testpackage
122120
# - wsl
121+
# - gomnd
122+
# - exhaustive
123123

124124
issues:
125125
# Excluding configuration per-path, per-linter, per-text and per-source

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ require (
1717
github.com/jackc/pgx/v4 v4.13.0
1818
github.com/json-iterator/go v1.1.11
1919
github.com/labstack/echo/v4 v4.5.0
20-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
21-
github.com/modern-go/reflect2 v1.0.1 // indirect
2220
github.com/nsqio/go-nsq v1.0.8
2321
github.com/pingcap/go-ycsb v0.0.0-20210129115622-04d8656123e4
2422
github.com/pkg/errors v0.9.1

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030I
1919
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
2020
github.com/Shopify/sarama v1.26.0 h1:C+zFi+/NJdfeJgZWbu+WaLgk4NcsbmqfFTKsoJmR39U=
2121
github.com/Shopify/sarama v1.26.0/go.mod h1:y/CFFTO9eaMTNriwu/Q+W4eioLqiDMGkA1W+gmdfj8w=
22+
github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc=
2223
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
2324
github.com/ThreeDotsLabs/watermill v1.0.2/go.mod h1:vZCPh7eN0P7r2qKau4SfmcUZ83+3JXWkRl4BiWUlqFw=
2425
github.com/ThreeDotsLabs/watermill v1.2.0-rc.6 h1:wYybtpC+LMQCC8j3/D1BQPobWh0rZ4ZX57i3+zy7t8I=
@@ -95,7 +96,9 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
9596
github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64=
9697
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg=
9798
github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
99+
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
98100
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
101+
github.com/frankban/quicktest v1.4.1 h1:Wv2VwvNn73pAdFIVUQRXYDFp31lXKbqblIXo/Q5GPSg=
99102
github.com/frankban/quicktest v1.4.1/go.mod h1:36zfPVQyHxymz4cH7wlDmVwDrJuljRB60qkgn7rorfQ=
100103
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
101104
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
@@ -685,6 +688,7 @@ gopkg.in/jcmturner/aescts.v1 v1.0.1 h1:cVVZBK2b1zY26haWB4vbBiZrfFQnfbTVrE3xZq6hr
685688
gopkg.in/jcmturner/aescts.v1 v1.0.1/go.mod h1:nsR8qBOg+OucoIW+WMhB3GspUQXq9XorLnQb9XtvcOo=
686689
gopkg.in/jcmturner/dnsutils.v1 v1.0.1 h1:cIuC1OLRGZrld+16ZJvvZxVJeKPsvd5eUIvxfoN5hSM=
687690
gopkg.in/jcmturner/dnsutils.v1 v1.0.1/go.mod h1:m3v+5svpVOhtFAP/wSz+yzh4Mc0Fg7eRhxkJMWSIz9Q=
691+
gopkg.in/jcmturner/goidentity.v3 v3.0.0 h1:1duIyWiTaYvVx3YX2CYtpJbUFd7/UuPYCfgXtQ3VTbI=
688692
gopkg.in/jcmturner/goidentity.v3 v3.0.0/go.mod h1:oG2kH0IvSYNIu80dVAyu/yoefjq1mNfM5bm88whjWx4=
689693
gopkg.in/jcmturner/gokrb5.v7 v7.2.3/go.mod h1:l8VISx+WGYp+Fp7KRbsiUuXTTOnxIc3Tuvyavf11/WM=
690694
gopkg.in/jcmturner/gokrb5.v7 v7.4.0 h1:93nj3P1OfL8Nv5h8ItQaslmskOqa4ykG5zouRht3Ffo=

pkg/cronx/cronx.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/rizalgowandy/gdk/pkg/errorx/v2"
9-
"github.com/rizalgowandy/gdk/pkg/logx"
109
"github.com/robfig/cron/v3"
1110
)
1211

@@ -29,41 +28,58 @@ var (
2928
)
3029

3130
// Default creates a cron with default config.
31+
// HTTP server is built in as side car by default.
3232
func Default(interceptors ...Interceptor) {
33-
New(defaultConfig, interceptors...)
33+
Custom(defaultConfig, interceptors...)
3434
}
3535

36-
// New creates a cron with custom config.
37-
func New(config Config, interceptors ...Interceptor) {
36+
// New creates a cron without HTTP server built in.
37+
func New(interceptors ...Interceptor) {
38+
Custom(Config{}, interceptors...)
39+
}
40+
41+
// Custom creates a cron with custom config.
42+
// For advance user, allow custom modification.
43+
func Custom(config Config, interceptors ...Interceptor) {
3844
// If there is invalid config use the default config instead.
3945
if config.Location == nil {
4046
config.Location = defaultConfig.Location
4147
}
42-
if config.Address == "" {
43-
config.Address = defaultConfig.Address
44-
}
4548

4649
// Create new command controller and start the underlying jobs.
4750
commandController = NewCommandController(config, interceptors...)
51+
52+
// Check if client want to start a server to serve json and frontend.
53+
if config.Address != "" {
54+
go NewSideCarServer(commandController)
55+
}
4856
}
4957

5058
// Schedule sets a job to run at specific time.
5159
// Example:
5260
// @every 5m
5361
// 0 */10 * * * * => every 10m
5462
func Schedule(spec string, job JobItf) error {
55-
return schedule(spec, job, 1, 1)
63+
return schedule(spec, "", job, 1, 1)
5664
}
5765

58-
func schedule(spec string, job JobItf, waveNumber, totalWave int64) error {
66+
// ScheduleWithName sets a job to run at specific time with a Job name
67+
// Example:
68+
// @every 5m
69+
// 0 */10 * * * * => every 10m
70+
func ScheduleWithName(name, spec string, job JobItf) error {
71+
return schedule(spec, name, job, 1, 1)
72+
}
73+
74+
func schedule(spec, jobName string, job JobItf, waveNumber, totalWave int64) error {
5975
if commandController == nil || commandController.Commander == nil {
60-
return errorx.E("cronx has not been initialized")
76+
return errorx.New("cronx has not been initialized")
6177
}
6278

6379
// Check if spec is correct.
6480
schedule, err := commandController.Parser.Parse(spec)
6581
if err != nil {
66-
downJob := NewJob(job, waveNumber, totalWave)
82+
downJob := NewJob(job, jobName, waveNumber, totalWave)
6783
downJob.Status = StatusCodeDown
6884
downJob.Error = err.Error()
6985
commandController.UnregisteredJobs = append(
@@ -73,7 +89,7 @@ func schedule(spec string, job JobItf, waveNumber, totalWave int64) error {
7389
return err
7490
}
7591

76-
j := NewJob(job, waveNumber, totalWave)
92+
j := NewJob(job, jobName, waveNumber, totalWave)
7793
j.EntryID = commandController.Commander.Schedule(schedule, j)
7894
return nil
7995
}
@@ -88,14 +104,14 @@ func schedule(spec string, job JobItf, waveNumber, totalWave int64) error {
88104
// This input schedules the job to run 3 times.
89105
func Schedules(spec, separator string, job JobItf) error {
90106
if spec == "" {
91-
return errorx.E("invalid specification")
107+
return errorx.New("invalid specification")
92108
}
93109
if separator == "" {
94-
return errorx.E("invalid separator")
110+
return errorx.New("invalid separator")
95111
}
96112
schedules := strings.Split(spec, separator)
97113
for k, v := range schedules {
98-
if err := schedule(v, job, int64(k+1), int64(len(schedules))); err != nil {
114+
if err := schedule(v, "", job, int64(k+1), int64(len(schedules))); err != nil {
99115
return err
100116
}
101117
}
@@ -111,7 +127,7 @@ func Every(duration time.Duration, job JobItf) {
111127
return
112128
}
113129

114-
j := NewJob(job, 1, 1)
130+
j := NewJob(job, "", 1, 1)
115131
j.EntryID = commandController.Commander.Schedule(cron.Every(duration), j)
116132
}
117133

@@ -121,13 +137,7 @@ func Stop() {
121137
return
122138
}
123139

124-
// Stop cron jobs.
125-
ctx := commandController.Commander.Stop()
126-
ctx = logx.ContextWithRequestID(ctx)
127-
select {
128-
case <-ctx.Done():
129-
case <-time.After(TimeoutDuration):
130-
}
140+
commandController.Commander.Stop()
131141
}
132142

133143
// GetEntries returns all the current registered jobs.
@@ -178,7 +188,7 @@ func GetStatusJSON() map[string]interface{} {
178188
return commandController.StatusJSON()
179189
}
180190

181-
// GetInfo returns command controller basic information.
191+
// GetInfo returns current cron check basic information.
182192
func GetInfo() map[string]interface{} {
183193
if commandController == nil {
184194
return nil
@@ -187,15 +197,6 @@ func GetInfo() map[string]interface{} {
187197
return commandController.Info()
188198
}
189199

190-
// Serve creates an HTTP server.
191-
func Serve() {
192-
if commandController == nil {
193-
return
194-
}
195-
196-
NewServer(commandController)
197-
}
198-
199200
// Func is a type to allow callers to wrap a raw func.
200201
// Example:
201202
// cronx.Schedule("@every 5m", cronx.Func(myFunc))

0 commit comments

Comments
 (0)