Skip to content

Commit 31370ec

Browse files
authored
Merge pull request #31 from opsre:feat_custom_cron
✨ feat(cron): 新增定时任务配置功能
2 parents 3311757 + aaafdaf commit 31370ec

File tree

5 files changed

+80
-11
lines changed

5 files changed

+80
-11
lines changed

config.example.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
cron_config:
2+
# ↓↓↓ -------------------------- 1. 域名记录同步间隔(秒),默认30秒
3+
domain_record_sync_interval: 30
4+
# ↓↓↓ -------------------------- 2. 证书信息同步间隔(秒),默认3600秒(1小时)
5+
cert_info_sync_interval: 3600
6+
# ↓↓↓ ---------------------------- 3. 自定义监控的域名记录列表,如果没有可留空
17
custom_records:
28
- "www.baidu.com"
39
- "wiki.eryajf.net"
410
cloud_providers:
5-
# ↓↓↓ -------------------------- 1. DNS提供商Tencent,请勿更改此行,如无需腾讯云的配置,可删除此段配置至 aliyun,该字段会作为标签注入到指标中
11+
# ↓↓↓ -------------------------- 4. DNS提供商Tencent,请勿更改此行,如无需腾讯云的配置,可删除此段配置至 aliyun,该字段会作为标签注入到指标中
612
tencent:
7-
# ↓↓↓ ------------------------ 2. 固定配置,无需更改
13+
# ↓↓↓ ------------------------ 5. 固定配置,无需更改
814
accounts:
9-
# ↓↓↓ ---------------------- 3. 腾讯云账号配置,可配置多个,给每个账号取个名字,如 t1、t2,该字段会作为标签注入到指标中
15+
# ↓↓↓ ---------------------- 6. 腾讯云账号配置,可配置多个,给每个账号取个名字,如 t1、t2,该字段会作为标签注入到指标中
1016
- name: t1
1117
secretId: "xxxxx"
1218
secretKey: "xxxxx"

docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ services:
3535

3636
# 示例配置
3737
configs:
38+
# 定时任务配置(可选,不配置则使用默认值)
39+
cron_config:
40+
domain_record_sync_interval: 30 # 域名记录同步间隔(秒),默认30秒
41+
cert_info_sync_interval: 3600 # 证书信息同步间隔(秒),默认3600秒(1小时)
3842
# 云解析配置,以阿里云DNS为例
3943
cloud_dns_exporter_config:
4044
content: |

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,4 @@ require (
7979
golang.org/x/sys v0.23.0 // indirect
8080
google.golang.org/protobuf v1.33.0 // indirect
8181
gopkg.in/yaml.v2 v2.4.0
82-
8382
)

pkg/export/cron_cache.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,51 @@ import (
1515

1616
// InitCron 初始化定时任务
1717
func InitCron() {
18+
cronConfig := public.GetCronConfig()
1819
c := cron.New(cron.WithSeconds())
19-
_, _ = c.AddFunc("*/30 * * * * *", func() {
20-
loading()
20+
21+
// 域名记录同步任务
22+
domainRecordSyncSpec := convertSecondsToCron(cronConfig.DomainRecordSyncInterval)
23+
_, _ = c.AddFunc(domainRecordSyncSpec, func() {
24+
loadingDomainRecord()
2125
})
22-
loading()
23-
_, _ = c.AddFunc("03 03 03 * * *", func() {
26+
loadingDomainRecord() // 启动时立即执行一次
27+
28+
// 证书信息同步任务
29+
certInfoSyncSpec := convertSecondsToCron(cronConfig.CertInfoSyncInterval)
30+
_, _ = c.AddFunc(certInfoSyncSpec, func() {
2431
loadingCert()
2532
loadingCustomRecordCert()
2633
})
27-
loadingCert()
34+
loadingCert() // 启动时立即执行一次
2835
loadingCustomRecordCert()
2936

3037
c.Start()
38+
39+
logger.Info(fmt.Sprintf("Cron initialized - domain record sync: every %d seconds, cert info sync: every %d seconds",
40+
cronConfig.DomainRecordSyncInterval, cronConfig.CertInfoSyncInterval))
41+
}
42+
43+
// convertSecondsToCron 将秒数转换为 cron 表达式
44+
func convertSecondsToCron(seconds int) string {
45+
if seconds < 60 {
46+
// 小于60秒:每N秒执行一次
47+
return fmt.Sprintf("*/%d * * * * *", seconds)
48+
} else if seconds < 3600 {
49+
// 小于1小时:每N分钟执行一次
50+
minutes := seconds / 60
51+
return fmt.Sprintf("0 */%d * * * *", minutes)
52+
} else if seconds < 86400 {
53+
// 小于1天:每N小时执行一次
54+
hours := seconds / 3600
55+
return fmt.Sprintf("0 0 */%d * * *", hours)
56+
} else {
57+
// 大于等于1天:每天凌晨3点执行
58+
return "0 0 3 * * *"
59+
}
3160
}
3261

33-
func loading() {
62+
func loadingDomainRecord() {
3463
var wg sync.WaitGroup
3564
var mu sync.Mutex
3665

public/public.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const (
3333
DomainList string = "domain_list"
3434
RecordList string = "record_list"
3535
RecordCertInfo string = "record_cert_info"
36+
// Cron Config Defaults
37+
DefaultDomainRecordSyncInterval = 30 // 默认域名记录同步间隔(秒)
38+
DefaultCertInfoSyncInterval = 3600 // 默认证书信息同步间隔(秒),24小时
3639
)
3740

3841
var (
@@ -49,9 +52,16 @@ type Account struct {
4952
SecretKey string `yaml:"secretKey"`
5053
}
5154

55+
// CronConfig 定时任务配置
56+
type CronConfig struct {
57+
DomainRecordSyncInterval int `yaml:"domain_record_sync_interval"` // 域名记录同步间隔(秒)
58+
CertInfoSyncInterval int `yaml:"cert_info_sync_interval"` // 证书信息同步间隔(秒)
59+
}
60+
5261
// Config 表示配置文件的结构
5362
type Configuration struct {
54-
CustomRecords []string `yaml:"custom_records"`
63+
CronConfig *CronConfig `yaml:"cron_config"`
64+
CustomRecords []string `yaml:"custom_records"`
5565
CloudProviders map[string]struct {
5666
Accounts []map[string]string `yaml:"accounts"`
5767
} `yaml:"cloud_providers"`
@@ -90,3 +100,24 @@ func InitCache() {
90100
func GetID() string {
91101
return xid.New().String()
92102
}
103+
104+
// GetCronConfig 获取定时任务配置,如果未配置则返回默认值
105+
func GetCronConfig() CronConfig {
106+
if Config.CronConfig == nil {
107+
return CronConfig{
108+
DomainRecordSyncInterval: DefaultDomainRecordSyncInterval,
109+
CertInfoSyncInterval: DefaultCertInfoSyncInterval,
110+
}
111+
}
112+
113+
config := *Config.CronConfig
114+
// 如果配置了但值为0或负数,使用默认值
115+
if config.DomainRecordSyncInterval <= 0 {
116+
config.DomainRecordSyncInterval = DefaultDomainRecordSyncInterval
117+
}
118+
if config.CertInfoSyncInterval <= 0 {
119+
config.CertInfoSyncInterval = DefaultCertInfoSyncInterval
120+
}
121+
122+
return config
123+
}

0 commit comments

Comments
 (0)