Skip to content

Commit e7ce9b9

Browse files
authored
Refactor/admin job (polarismesh#1056)
1 parent d8bcc3e commit e7ce9b9

File tree

5 files changed

+32
-29
lines changed

5 files changed

+32
-29
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ require (
8585
require (
8686
github.com/DATA-DOG/go-sqlmock v1.5.0
8787
github.com/polarismesh/specification v1.2.1
88-
github.com/robfig/cron/v3 v3.0.1
8988
)
9089

9190
replace gopkg.in/yaml.v2 => gopkg.in/yaml.v2 v2.2.2

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
321321
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
322322
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219 h1:XnFyNUWnciM6zgXaz6tm+Egs35rhoD0KGMmKh4gCdi0=
323323
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219/go.mod h1:4WhwBysTom9Eoy0hQ4W69I0FmO+T0EpjEW9/5sgHoUk=
324-
github.com/polarismesh/specification v1.2.1-alpha.1 h1:0JC+lPssydCRJtUg/mQmwcqBz3SJ3+IegJtf1nHyT54=
325-
github.com/polarismesh/specification v1.2.1-alpha.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU=
326324
github.com/polarismesh/specification v1.2.1 h1:NcFoinO+aSWIOIyKTAhvEbjaPvZAAm5/DwJAL2FGdXs=
327325
github.com/polarismesh/specification v1.2.1/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU=
328326
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
@@ -348,8 +346,6 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
348346
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
349347
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
350348
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
351-
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
352-
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
353349
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
354350
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
355351
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=

maintain/job/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ package job
2121
type JobConfig struct {
2222
Name string `yaml:"name"`
2323
Enable bool `yaml:"enable"`
24-
CronSpec string `yaml:"cronSpec"`
24+
Interval string `yaml:"interval"`
2525
Option map[string]interface{} `yaml:"option"`
2626
}

maintain/job/job.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ package job
2020
import (
2121
"context"
2222
"fmt"
23-
24-
"github.com/robfig/cron/v3"
23+
"time"
2524

2625
"github.com/polarismesh/polaris/cache"
2726
commonlog "github.com/polarismesh/polaris/common/log"
@@ -36,8 +35,8 @@ var log = commonlog.GetScopeOrDefaultByName(commonlog.DefaultLoggerName)
3635
type MaintainJobs struct {
3736
jobs map[string]maintainJob
3837
startedJobs map[string]maintainJob
39-
scheduler *cron.Cron
4038
storage store.Store
39+
cancel context.CancelFunc
4140
}
4241

4342
// NewMaintainJobs
@@ -53,13 +52,14 @@ func NewMaintainJobs(namingServer service.DiscoverServer, cacheMgn *cache.CacheM
5352
storage: storage},
5453
},
5554
startedJobs: map[string]maintainJob{},
56-
scheduler: newCron(),
5755
storage: storage,
5856
}
5957
}
6058

6159
// StartMaintainJobs
6260
func (mj *MaintainJobs) StartMaintianJobs(configs []JobConfig) error {
61+
ctx, cancel := context.WithCancel(context.Background())
62+
mj.cancel = cancel
6363
for _, cfg := range configs {
6464
if !cfg.Enable {
6565
log.Infof("[Maintain][Job] job (%s) not enable", cfg.Name)
@@ -83,33 +83,27 @@ func (mj *MaintainJobs) StartMaintianJobs(configs []JobConfig) error {
8383
log.Errorf("[Maintain][Job][%s] start leader election err: %v", cfg.Name, err)
8484
return err
8585
}
86-
_, err = mj.scheduler.AddFunc(cfg.CronSpec, newCronCmd(cfg.Name, job, mj.storage))
86+
dur, err := time.ParseDuration(cfg.Interval)
8787
if err != nil {
88-
log.Errorf("[Maintain][Job] job (%s) fail to start, err: %v", cfg.Name, err)
89-
return fmt.Errorf("[Maintain][Job] job (%s) fail to start", cfg.Name)
88+
log.Errorf("[Maintain][Job][%s] parse job exec interval err: %v", cfg.Name, err)
89+
return err
9090
}
91+
runAdminJob(ctx, cfg.Name, dur, job, mj.storage)
9192
mj.startedJobs[cfg.Name] = job
9293
}
93-
mj.scheduler.Start()
9494
return nil
9595
}
9696

9797
// StopMaintainJobs
9898
func (mj *MaintainJobs) StopMaintainJobs() {
99-
ctx := mj.scheduler.Stop()
100-
<-ctx.Done()
99+
if mj.cancel != nil {
100+
mj.cancel()
101+
}
101102
mj.startedJobs = map[string]maintainJob{}
102103
}
103104

104-
func newCron() *cron.Cron {
105-
return cron.New(cron.WithChain(
106-
cron.Recover(cron.DefaultLogger)),
107-
cron.WithParser(cron.NewParser(
108-
cron.Minute|cron.Hour|cron.Dom|cron.Month|cron.Dow|cron.Descriptor)))
109-
}
110-
111-
func newCronCmd(name string, job maintainJob, storage store.Store) func() {
112-
return func() {
105+
func runAdminJob(ctx context.Context, name string, interval time.Duration, job maintainJob, storage store.Store) {
106+
f := func() {
113107
if !storage.IsLeader(store.ElectionKeyMaintainJobPrefix + name) {
114108
log.Infof("[Maintain][Job][%s] I am follower", name)
115109
job.clear()
@@ -118,8 +112,19 @@ func newCronCmd(name string, job maintainJob, storage store.Store) func() {
118112
log.Infof("[Maintain][Job][%s] I am leader, job start", name)
119113
job.execute()
120114
log.Infof("[Maintain][Job][%s] I am leader, job end", name)
121-
122115
}
116+
117+
ticker := time.NewTicker(interval)
118+
go func(ctx context.Context) {
119+
for {
120+
select {
121+
case <-ctx.Done():
122+
return
123+
case <-ticker.C:
124+
f()
125+
}
126+
}
127+
}(ctx)
123128
}
124129

125130
type maintainJob interface {

release/conf/polaris-server.yaml

+6-3
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,22 @@ maintain:
395395
# Clean up long term unhealthy instance
396396
- name: DeleteUnHealthyInstance
397397
enable: false
398-
cronSpec: "0 0 * * ?"
398+
# job exec interval. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
399+
interval: 24h
399400
option:
400401
instanceDeleteTimeout: 60m
401402
# Delete auto-created service without an instance
402403
- name: DeleteEmptyAutoCreatedService
403404
enable: false
404-
cronSpec: "*/10 * * * ?"
405+
# job exec interval. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
406+
interval: 1h
405407
option:
406408
serviceDeleteTimeout: 30m
407409
# Clean soft deleted instances
408410
- name: CleanDeletedInstances
409411
enable: true
410-
cronSpec: "0 0 * * 1"
412+
# job exec interval. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
413+
interval: 24h
411414

412415
# Storage configuration
413416
store:

0 commit comments

Comments
 (0)