Skip to content

Commit

Permalink
feat: add maintain jobs configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
shichaoyuan committed Feb 20, 2023
1 parent 248b222 commit 163b25e
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 6 deletions.
2 changes: 2 additions & 0 deletions bootstrap/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/polarismesh/polaris/cache"
"github.com/polarismesh/polaris/common/log"
"github.com/polarismesh/polaris/config"
"github.com/polarismesh/polaris/maintain"
"github.com/polarismesh/polaris/namespace"
"github.com/polarismesh/polaris/plugin"
"github.com/polarismesh/polaris/service"
Expand All @@ -46,6 +47,7 @@ type Config struct {
Naming service.Config `yaml:"naming"`
Config config.Config `yaml:"config"`
HealthChecks healthcheck.Config `yaml:"healthcheck"`
Maintain maintain.Config `yaml:"maintain"`
Store store.Config `yaml:"store"`
Auth auth.Config `yaml:"auth"`
Plugin plugin.Config `yaml:"plugin"`
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func StartComponents(ctx context.Context, cfg *boot_config.Config) error {
}

// 初始化运维操作模块
if err := maintain.Initialize(ctx, namingSvr, healthCheckServer, s); err != nil {
if err := maintain.Initialize(ctx, &cfg.Maintain, namingSvr, healthCheckServer, s); err != nil {
return err
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/polarismesh/go-restful-openapi/v2 v2.0.0-20220928152401-083908d10219
github.com/prometheus/client_golang v1.12.2
github.com/robfig/cron/v3 v3.0.1
github.com/smartystreets/goconvey v1.6.4
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.8.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
29 changes: 29 additions & 0 deletions maintain/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package maintain

// Config maintain configuration
type Config struct {
Jobs []JobConfig `yaml:"jobs"`
}

// JobcConfig maintain job configuration
type JobConfig struct {
Name string `yaml:"name"`
Option map[string]interface{} `yaml:"option"`
}
10 changes: 5 additions & 5 deletions maintain/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ var (
)

// Initialize 初始化
func Initialize(ctx context.Context, namingService service.DiscoverServer, healthCheckServer *healthcheck.Server,
storage store.Store) error {
func Initialize(ctx context.Context, config *Config, namingService service.DiscoverServer,
healthCheckServer *healthcheck.Server, storage store.Store) error {

if finishInit {
return nil
}

err := initialize(ctx, namingService, healthCheckServer, storage)
err := initialize(ctx, config, namingService, healthCheckServer, storage)
if err != nil {
return err
}
Expand All @@ -50,8 +50,8 @@ func Initialize(ctx context.Context, namingService service.DiscoverServer, healt
return nil
}

func initialize(_ context.Context, namingService service.DiscoverServer, healthCheckServer *healthcheck.Server,
storage store.Store) error {
func initialize(_ context.Context, config *Config, namingService service.DiscoverServer,
healthCheckServer *healthcheck.Server, storage store.Store) error {

authServer, err := auth.GetAuthServer()
if err != nil {
Expand Down
47 changes: 47 additions & 0 deletions maintain/job/job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package job

var (
slots map[string]PolarisCleanJob = map[string]PolarisCleanJob{}
)

func init() {

}

func RegisterJob(j PolarisCleanJob) {
slots[j.Name()] = j
}

func GetAllRegister() map[string]PolarisCleanJob {
ret := make(map[string]PolarisCleanJob)

for k, v := range slots {
ret[k] = v
}

return ret
}

type PolarisCleanJob interface {
Init(cfg map[string]interface{})
Execute() func()
Name() string
Destory() error
}
68 changes: 68 additions & 0 deletions maintain/job/scheduler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package job

import "github.com/robfig/cron/v3"

// Scheduler
type Scheduler struct {
cron *cron.Cron
}

// CronJob 定时任务的通用接口
type CronJob interface {
CronSpec() string
Execute() func()
}

// NewDefaultScheduler
func NewDefaultScheduler() *Scheduler {
c := cron.New(cron.WithChain(
cron.Recover(cron.DefaultLogger)),
cron.WithParser(cron.NewParser(
cron.Minute|cron.Hour|cron.Dom|cron.Month|cron.Dow|cron.Descriptor)))
return &Scheduler{cron: c}
}

// AddJob
func (sc *Scheduler) AddJob(job CronJob) (int, error) {
id, err := sc.cron.AddFunc(job.CronSpec(), job.Execute())
if err != nil {
return 0, err
}
return int(id), nil
}

// AddFunc
func (sc *Scheduler) AddFunc(cron string, cmd func()) (int, error) {
id, err := sc.cron.AddFunc(cron, cmd)
if err != nil {
return 0, err
}
return int(id), nil
}

// Start
func (sc *Scheduler) Start() {
sc.cron.Start()
}

// Stop
func (sc *Scheduler) Stop() {
sc.cron.Stop()
}
14 changes: 14 additions & 0 deletions release/conf/polaris-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,20 @@ cache:
expireTimeAfterWrite: 3600
- name: faultDetectRule
# - name: l5 # Load L5 data
# Maintain configuration
maintain:
jobs:
# Clean up long term unhealthy instance
- name: DeleteUnHealthyInstance
option:
cronSpec: "0 * * * ?"
instanceDeleteTimeout: 60m
# Delete auto-created service without an instance
- name: DeleteEmptyAutoCreatedService
option:
cronSpec: "0 * * * ?"
serviceDeleteTimeout: 60m

# Storage configuration
store:
# Standalone file storage plugin
Expand Down
12 changes: 12 additions & 0 deletions test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"fmt"
"strings"
)

func main() {
a := "a/b"
tokens := strings.Split(a, "/")
fmt.Println(len(tokens))
}

0 comments on commit 163b25e

Please sign in to comment.