Skip to content

Commit e616f5e

Browse files
committed
#### Version 0.9.3
* Opt: 完善config模块,优化config注入模式 * Opt: 增加ConfigHandle函数定义,用于应用自定义config加载实现(如从redis\mysql等源读取配置) * feature: 新增LoadFileConfig用于逐步替换原LoadConfig,主要用于读取xml\json\yaml格式配置文件 * feature: 新增LoadConfigHandler用于应用自定义配置加载方式的注入 * 2019-12-20 09:00 at ShangHai
1 parent 250de28 commit e616f5e

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

config.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ type (
3030
HandlerData string `xml:"handlerdata,attr" yaml:"handlerdata"`
3131
} `xml:"tasks>task" yaml:"tasks"`
3232
}
33+
34+
ConfigHandle func(configSource string) (*AppConfig, error)
3335
)
3436

3537
//初始化配置文件(xml)
36-
func InitConfig(configFile string) *AppConfig {
38+
func XmlConfigHandler(configFile string) *AppConfig {
3739
realFile, exists := lookupFile(configFile)
3840
if !exists {
3941
panic("Task:Config:InitConfig 配置文件[" + configFile + "]无法解析 - 无法寻找到指定配置文件")
@@ -56,7 +58,7 @@ func InitConfig(configFile string) *AppConfig {
5658
}
5759

5860
//初始化配置文件(json)
59-
func InitJsonConfig(configFile string) *AppConfig {
61+
func JsonConfigHandler(configFile string) *AppConfig {
6062
realFile, exists := lookupFile(configFile)
6163
if !exists {
6264
panic("Task:Config:InitConfig 配置文件[" + configFile + "]无法解析 - 无法寻找到指定配置文件")
@@ -78,7 +80,7 @@ func InitJsonConfig(configFile string) *AppConfig {
7880
}
7981

8082
//初始化配置文件(yaml)
81-
func InitYamlConfig(configFile string) *AppConfig {
83+
func YamlConfigHandler(configFile string) *AppConfig {
8284
realFile, exists := lookupFile(configFile)
8385
if !exists {
8486
panic("Task:Config:InitConfig 配置文件[" + configFile + "]无法解析 - 无法寻找到指定配置文件")

example/loadfromconfig/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ func main() {
3232
RegisterTask(service)
3333

3434
//step 3: load config file
35-
service.LoadConfig("d:/gotmp/dottask/task.conf")
35+
service.LoadFileConfig("c:/gotmp/dottask/task.conf")
3636

3737
fmt.Println(time.Now().String(), " => Begin Task")
3838
//step 4: start all task
3939
service.StartAllTask()
4040

41-
fmt.Println(service.PrintAllCronTask())
41+
fmt.Println(service.PrintAllTasks())
4242

4343
for {
4444
time.Sleep(time.Hour)

example/loadfromjson/task.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ func main() {
3030
RegisterHandler("Loop_Config", Loop_Config).
3131

3232
//step 3: load config file
33-
LoadConfig("d:/gotmp/task/task.json.conf", ConfigType_Json).
33+
LoadFileConfig("c:/gotmp/task/task.json.conf", ConfigType_Json).
3434

3535
//step 4: start all task
3636
StartAllTask()
3737

38-
fmt.Println(service.PrintAllCronTask())
38+
fmt.Println(service.PrintAllTasks())
3939

40-
for true {
40+
for {
41+
time.Sleep(time.Hour)
4142
}
4243
}

example/loadfromyaml/task.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ func main() {
2929
RegisterHandler("Loop_Config", Loop_Config).
3030

3131
//step 3: load config file
32-
LoadConfig("d:/gotmp/task/task.yaml", ConfigType_Yaml).
32+
LoadFileConfig("d:/gotmp/task/task.yaml", ConfigType_Yaml).
3333

3434
//step 4: start all task
3535
StartAllTask()
3636

37-
fmt.Println(service.PrintAllCronTask())
37+
fmt.Println(service.PrintAllTasks())
3838

39-
for true {
39+
for {
40+
time.Sleep(time.Hour)
4041
}
4142
}

tasks.go

+30-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package task
33
import (
44
"encoding/json"
55
"fmt"
6+
"os"
67
"runtime/debug"
78
"strconv"
89
"sync"
@@ -99,21 +100,35 @@ func (service *TaskService) SetOnEndHandler(handler TaskHandle) {
99100
}
100101

101102
// LoadConfig 如果指定配置文件,初始化配置
103+
// Deprecated: Use the LoadFileConfig instead
102104
func (service *TaskService) LoadConfig(configFile string, confType ...interface{}) *TaskService {
105+
return service.LoadFileConfig(configFile, confType...)
106+
}
107+
108+
// LoadFileConfig 如果指定配置文件,初始化配置
109+
func (service *TaskService) LoadFileConfig(configFile string, confType ...interface{}) *TaskService {
103110
cType := ConfigType_Xml
104111
if len(confType) > 0 && confType[0] == ConfigType_Json {
105112
cType = ConfigType_Json
106113
}
107114
if len(confType) > 0 && confType[0] == ConfigType_Yaml {
108115
cType = ConfigType_Yaml
109116
}
117+
var config *AppConfig
110118
if cType == ConfigType_Json {
111-
service.Config = InitJsonConfig(configFile)
119+
config = JsonConfigHandler(configFile)
112120
} else if cType == ConfigType_Yaml {
113-
service.Config = InitYamlConfig(configFile)
121+
config = YamlConfigHandler(configFile)
114122
} else {
115-
service.Config = InitConfig(configFile)
123+
config = XmlConfigHandler(configFile)
116124
}
125+
service.applyConfig(config)
126+
return service
127+
}
128+
129+
// applyConfig apply task config with AppConfig
130+
func (service *TaskService) applyConfig(config *AppConfig) *TaskService {
131+
service.Config = config
117132
if service.logger == nil {
118133
if service.Config.Global.LogPath != "" {
119134
service.SetLogger(NewFileLogger(service.Config.Global.LogPath))
@@ -155,6 +170,18 @@ func (service *TaskService) LoadConfig(configFile string, confType ...interface{
155170
return service
156171
}
157172

173+
// LoadConfigHandler load config handler and init task config
174+
func (service *TaskService) LoadConfigHandler(configHandler ConfigHandle, configSource string) *TaskService {
175+
config, err := configHandler(configSource)
176+
if err != nil {
177+
panic("Task:LoadConfigHandler 配置源[" + configSource + "]解析失败: " + err.Error())
178+
os.Exit(1)
179+
} else {
180+
service.applyConfig(config)
181+
}
182+
return service
183+
}
184+
158185
// RegisterHandler register handler by name
159186
func (service *TaskService) RegisterHandler(name string, handler TaskHandle) *TaskService {
160187
service.handlerMutex.Lock()

version.MD

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## devfeel/dottask
22

3+
#### Version 0.9.3
4+
* Opt: 完善config模块,优化config注入模式
5+
* Opt: 增加ConfigHandle函数定义,用于应用自定义config加载实现(如从redis\mysql等源读取配置)
6+
* feature: 新增LoadFileConfig用于逐步替换原LoadConfig,主要用于读取xml\json\yaml格式配置文件
7+
* feature: 新增LoadConfigHandler用于应用自定义配置加载方式的注入
8+
* 2019-12-20 09:00 at ShangHai
9+
310
#### Version 0.9.2
411
* Opt: CounterOutputHttpHandler、TaskOutputHttpHandler输出UI优化
512
* About HttpOutput:

0 commit comments

Comments
 (0)