Skip to content

Commit 599544a

Browse files
committed
refactor(jobs): 将任务配置移至config包并统一配置加载
- 将JobConfig结构体定义从jobs/types.go移动到config/config.go - 修改所有任务构造函数使用config.JobConfig类型参数 - 更新任务初始化逻辑从config.Conf.Jobs读取配置 - 修正product_category_repository.go中的SQL字段名拼写错误 - 更新README.md文档说明新的配置方式
1 parent 94a3331 commit 599544a

File tree

10 files changed

+96
-66
lines changed

10 files changed

+96
-66
lines changed

config/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"log"
1111
"os"
12+
"time"
1213

1314
"github.com/fsnotify/fsnotify"
1415
"github.com/spf13/viper"
@@ -29,6 +30,7 @@ type config struct {
2930
Jwt *JwtConfig `mapstructure:"jwt" json:"jwt"`
3031
RateLimit *RateLimitConfig `mapstructure:"rate-limit" json:"rateLimit"`
3132
UploadFile *UploadFile `mapstructure:"upload-file" json:"uploadFile"`
33+
Jobs *JobsConfig `mapstructure:"jobs" json:"jobs"`
3234
}
3335

3436
// 设置读取配置信息
@@ -120,3 +122,17 @@ type UploadFile struct {
120122
Bucket string `mapstructure:"bucket" json:"bucket"`
121123
Endpoint string `mapstructure:"endpoint" json:"endpoint"`
122124
}
125+
126+
type JobsConfig struct {
127+
Enabled bool `mapstructure:"enabled" json:"enabled"`
128+
List map[string]JobConfig `mapstructure:"list" json:"list"`
129+
}
130+
131+
type JobConfig struct {
132+
Name string `mapstructure:"name" json:"name"`
133+
Description string `mapstructure:"description" json:"description"`
134+
Schedule string `mapstructure:"schedule" json:"schedule"`
135+
Enabled bool `mapstructure:"enabled" json:"enabled"`
136+
Timeout time.Duration `mapstructure:"timeout" json:"timeout"`
137+
RetryCount int `mapstructure:"retry-count" json:"retryCount"`
138+
}

config/config.tmp.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,24 @@ upload-file:
9797
baidu:
9898
# 百度推送Token (请填写您的百度推送Token)
9999
push-token: your_baidu_push_token_here
100+
101+
# 定时任务配置
102+
jobs:
103+
# 全局启用开关
104+
enabled: true
105+
# 任务列表
106+
list:
107+
sitemap:
108+
name: sitemap
109+
description: "生成站点地图"
110+
schedule: "@every 1h" # 1小时执行一次
111+
enabled: true
112+
timeout: 300s # 5分钟超时
113+
retry-count: 3
114+
example:
115+
name: example
116+
description: "示例任务"
117+
schedule: "@every 30s"
118+
enabled: false
119+
timeout: 60s
120+
retry-count: 1

internal/app/jobs/README.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,20 @@ func InitJobs() error {
9393

9494
### 3. 配置任务
9595

96-
`config.go` 中添加任务配置:
97-
98-
```go
99-
func DefaultJobsConfig() *JobsConfig {
100-
return &JobsConfig{
101-
Enabled: true,
102-
Jobs: map[string]JobConfig{
103-
// ... 其他任务配置 ...
104-
"my_custom": {
105-
Name: "my_custom",
106-
Description: "我的自定义任务",
107-
Schedule: "@every 30s",
108-
Enabled: true,
109-
Timeout: 2 * time.Minute,
110-
RetryCount: 3,
111-
},
112-
},
113-
}
114-
}
96+
在配置文件 `config/config.yml` (或 `config.local.yml`) 中添加任务配置:
97+
98+
```yaml
99+
jobs:
100+
enabled: true
101+
list:
102+
# ... 其他任务配置 ...
103+
my_custom:
104+
name: "my_custom"
105+
description: "我的自定义任务"
106+
schedule: "@every 30s"
107+
enabled: true
108+
timeout: 120s
109+
retry-count: 3
115110
```
116111
117112
## API 接口

internal/app/jobs/base_job.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package jobs
77

88
import (
99
"context"
10+
"gotribe-admin/config"
1011
"time"
1112
)
1213

@@ -22,14 +23,14 @@ type BaseJob struct {
2223
}
2324

2425
// NewBaseJob 创建基础任务
25-
func NewBaseJob(config JobConfig, executor func(ctx context.Context) error) *BaseJob {
26+
func NewBaseJob(jobConfig config.JobConfig, executor func(ctx context.Context) error) *BaseJob {
2627
return &BaseJob{
27-
name: config.Name,
28-
description: config.Description,
29-
schedule: config.Schedule,
30-
enabled: config.Enabled,
31-
timeout: config.Timeout,
32-
retryCount: config.RetryCount,
28+
name: jobConfig.Name,
29+
description: jobConfig.Description,
30+
schedule: jobConfig.Schedule,
31+
enabled: jobConfig.Enabled,
32+
timeout: jobConfig.Timeout,
33+
retryCount: jobConfig.RetryCount,
3334
executor: executor,
3435
}
3536
}

internal/app/jobs/config.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,28 @@ package jobs
77

88
import (
99
"time"
10-
)
1110

12-
// JobsConfig 任务配置
13-
type JobsConfig struct {
14-
Enabled bool `yaml:"enabled" json:"enabled"`
15-
Jobs map[string]JobConfig `yaml:"jobs" json:"jobs"`
16-
}
11+
"gotribe-admin/config"
12+
)
1713

1814
// DefaultJobsConfig 默认任务配置
19-
func DefaultJobsConfig() *JobsConfig {
20-
return &JobsConfig{
21-
Enabled: true,
22-
Jobs: map[string]JobConfig{
15+
func DefaultJobsConfig() *config.JobsConfig {
16+
return &config.JobsConfig{
17+
Enabled: false,
18+
List: map[string]config.JobConfig{
2319
"sitemap": {
2420
Name: "sitemap",
2521
Description: "生成站点地图",
26-
Schedule: "@every 1m", // 改为24小时执行一次
27-
Enabled: true,
22+
Schedule: "@every 1h",
23+
Enabled: false,
2824
Timeout: 5 * time.Minute,
2925
RetryCount: 3,
3026
},
3127
"example": {
3228
Name: "example",
3329
Description: "示例任务",
3430
Schedule: "@every 30s",
35-
Enabled: false, // 默认禁用
31+
Enabled: false,
3632
Timeout: 1 * time.Minute,
3733
RetryCount: 1,
3834
},
@@ -41,17 +37,20 @@ func DefaultJobsConfig() *JobsConfig {
4137
}
4238

4339
// GetJobConfig 获取任务配置
44-
func (c *JobsConfig) GetJobConfig(jobName string) (JobConfig, bool) {
45-
config, exists := c.Jobs[jobName]
46-
return config, exists
40+
func GetJobConfig(c *config.JobsConfig, jobName string) (config.JobConfig, bool) {
41+
if c == nil || c.List == nil {
42+
return config.JobConfig{}, false
43+
}
44+
conf, exists := c.List[jobName]
45+
return conf, exists
4746
}
4847

4948
// IsJobEnabled 检查任务是否启用
50-
func (c *JobsConfig) IsJobEnabled(jobName string) bool {
51-
if !c.Enabled {
49+
func IsJobEnabled(c *config.JobsConfig, jobName string) bool {
50+
if c == nil || !c.Enabled {
5251
return false
5352
}
5453

55-
config, exists := c.GetJobConfig(jobName)
56-
return exists && config.Enabled
54+
conf, exists := GetJobConfig(c, jobName)
55+
return exists && conf.Enabled
5756
}

internal/app/jobs/example_job.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"time"
1111

12+
"gotribe-admin/config"
1213
"gotribe-admin/internal/pkg/common"
1314
)
1415

@@ -18,9 +19,9 @@ type ExampleJob struct {
1819
}
1920

2021
// NewExampleJob 创建示例任务
21-
func NewExampleJob(config JobConfig) *ExampleJob {
22+
func NewExampleJob(jobConfig config.JobConfig) *ExampleJob {
2223
job := &ExampleJob{}
23-
job.BaseJob = NewBaseJob(config, job.execute)
24+
job.BaseJob = NewBaseJob(jobConfig, job.execute)
2425
return job
2526
}
2627

internal/app/jobs/init.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
package jobs
77

88
import (
9+
"gotribe-admin/config"
910
"gotribe-admin/internal/pkg/common"
1011
)
1112

1213
// InitJobs 初始化所有任务
1314
func InitJobs() error {
1415
common.Log.Info("Initializing jobs...")
1516

16-
// 获取默认配置
17-
config := DefaultJobsConfig()
17+
// 获取配置
18+
jobsConfig := config.Conf.Jobs
19+
if jobsConfig == nil {
20+
jobsConfig = DefaultJobsConfig()
21+
}
1822

1923
// 注册站点地图任务
20-
if config.IsJobEnabled("sitemap") {
21-
sitemapConfig, _ := config.GetJobConfig("sitemap")
24+
if IsJobEnabled(jobsConfig, "sitemap") {
25+
sitemapConfig, _ := GetJobConfig(jobsConfig, "sitemap")
2226
sitemapJob := NewSitemapJob(sitemapConfig)
2327
if err := RegisterJob(sitemapJob); err != nil {
2428
common.Log.Errorf("Failed to register sitemap job: %v", err)
@@ -27,8 +31,8 @@ func InitJobs() error {
2731
}
2832

2933
// 注册示例任务
30-
if config.IsJobEnabled("example") {
31-
exampleConfig, _ := config.GetJobConfig("example")
34+
if IsJobEnabled(jobsConfig, "example") {
35+
exampleConfig, _ := GetJobConfig(jobsConfig, "example")
3236
exampleJob := NewExampleJob(exampleConfig)
3337
if err := RegisterJob(exampleJob); err != nil {
3438
common.Log.Errorf("Failed to register example job: %v", err)

internal/app/jobs/sitemap_job.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package jobs
88
import (
99
"context"
1010

11+
"gotribe-admin/config"
1112
"gotribe-admin/internal/app/repository"
1213
"gotribe-admin/internal/pkg/common"
1314
"gotribe-admin/internal/pkg/model"
@@ -21,9 +22,9 @@ type SitemapJob struct {
2122
}
2223

2324
// NewSitemapJob 创建站点地图任务
24-
func NewSitemapJob(config JobConfig) *SitemapJob {
25+
func NewSitemapJob(jobConfig config.JobConfig) *SitemapJob {
2526
job := &SitemapJob{}
26-
job.BaseJob = NewBaseJob(config, job.execute)
27+
job.BaseJob = NewBaseJob(jobConfig, job.execute)
2728
return job
2829
}
2930

internal/app/jobs/types.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,7 @@ type Job interface {
5757
IsEnabled() bool
5858
}
5959

60-
// JobConfig 任务配置
61-
type JobConfig struct {
62-
Name string `yaml:"name" json:"name"`
63-
Description string `yaml:"description" json:"description"`
64-
Schedule string `yaml:"schedule" json:"schedule"`
65-
Enabled bool `yaml:"enabled" json:"enabled"`
66-
Timeout time.Duration `yaml:"timeout" json:"timeout"`
67-
RetryCount int `yaml:"retry_count" json:"retry_count"`
68-
}
60+
// JobConfig definition moved to config package
6961

7062
// JobManager 任务管理器接口
7163
type JobManager interface {

internal/app/repository/product_category_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (cr ProductCategoryRepository) CreateProductCategory(productCategory *model
7272

7373
// 更新分类
7474
func (cr ProductCategoryRepository) UpdateProductCategoryByID(productCategoryID string, productCategory *model.ProductCategory) error {
75-
err := common.DB.Model(productCategory).Where("productCategory_id = ?", productCategoryID).Updates(productCategory).Error
75+
err := common.DB.Model(productCategory).Where("product_category_id = ?", productCategoryID).Updates(productCategory).Error
7676
return err
7777
}
7878

0 commit comments

Comments
 (0)