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.
3232func 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
5462func 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.
89105func 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.
182192func 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