- 优化任务
continuous-integration/drone/push Build was killed Details
continuous-integration/drone/tag Build is failing Details

master v1.0.17
李光春 2 years ago
parent 8cc33fa88b
commit c785b13dac

@ -1,3 +1,7 @@
## v1.0.17
- 优化任务
## v1.0.16 ## v1.0.16
- 增加驱动 - 增加驱动

@ -7,9 +7,12 @@ import (
// EtcdConfig etcd配置 // EtcdConfig etcd配置
type EtcdConfig struct { type EtcdConfig struct {
Endpoints []string // 接口 []string{"http://127.0.0.1:2379"} Endpoints []string // 接口 []string{"http://127.0.0.1:2379"}
DialTimeout time.Duration // time.Second * 5 DialTimeout time.Duration // time.Second * 5
LocalIP string // 本机IP LocalIP string // 本机IP
Username string // 用户名
Password string // 密码
CustomDirectory string // 自定义目录,后面不需要/
} }
// Etcd etcd // Etcd etcd
@ -33,12 +36,28 @@ const (
JobWorkerDir = "/cron/workers/" JobWorkerDir = "/cron/workers/"
) )
func getJobSaveDir(e Etcd) string {
if e.CustomDirectory == "" {
return JobSaveDir
} else {
return JobSaveDir + e.CustomDirectory + "/"
}
}
func getJobWorkerDir(e Etcd) string {
if e.CustomDirectory == "" {
return JobWorkerDir
} else {
return JobWorkerDir + e.CustomDirectory + "/"
}
}
// GetWatchKey 监听的key // GetWatchKey 监听的key
func (e Etcd) GetWatchKey() string { func (e Etcd) GetWatchKey() string {
return JobSaveDir + e.LocalIP return getJobSaveDir(e) + e.LocalIP
} }
// IssueWatchKey 下发的key // IssueWatchKey 下发的key
func (e Etcd) IssueWatchKey(ip string) string { func (e Etcd) IssueWatchKey(ip string) string {
return JobSaveDir + ip return getJobSaveDir(e) + ip
} }

@ -19,11 +19,22 @@ func NewEtcdServer(config *EtcdConfig) (*Etcd, error) {
e.Endpoints = config.Endpoints e.Endpoints = config.Endpoints
e.DialTimeout = config.DialTimeout e.DialTimeout = config.DialTimeout
e.LocalIP = config.LocalIP e.LocalIP = config.LocalIP
e.Username = config.Username
e.Password = config.Password
e.CustomDirectory = config.CustomDirectory
e.Client, err = clientv3.New(clientv3.Config{ v3Config := clientv3.Config{
Endpoints: e.Endpoints, Endpoints: e.Endpoints,
DialTimeout: e.DialTimeout, DialTimeout: e.DialTimeout,
}) }
// 判断有没有配置用户信息
if e.Username != "" {
v3Config.Username = e.Username
v3Config.Password = e.Password
}
e.Client, err = clientv3.New(v3Config)
if err != nil { if err != nil {
return nil, errors.New("连接失败:" + err.Error()) return nil, errors.New("连接失败:" + err.Error())
} }
@ -49,20 +60,20 @@ func (e Etcd) ListWorkers() (workerArr []string, err error) {
workerArr = make([]string, 0) workerArr = make([]string, 0)
// 获取目录下所有Kv // 获取目录下所有Kv
if getResp, err = e.Kv.Get(context.TODO(), JobWorkerDir, clientv3.WithPrefix()); err != nil { if getResp, err = e.Kv.Get(context.TODO(), getJobWorkerDir(e), clientv3.WithPrefix()); err != nil {
return return
} }
// 解析每个节点的IP // 解析每个节点的IP
for _, kv = range getResp.Kvs { for _, kv = range getResp.Kvs {
// kv.Key : /cron/workers/192.168.2.1 // kv.Key : /cron/workers/192.168.2.1
workerIP = ExtractWorkerIP(string(kv.Key)) workerIP = e.ExtractWorkerIP(string(kv.Key))
workerArr = append(workerArr, workerIP) workerArr = append(workerArr, workerIP)
} }
return return
} }
// ExtractWorkerIP 提取worker的IP // ExtractWorkerIP 提取worker的IP
func ExtractWorkerIP(regKey string) string { func (e Etcd) ExtractWorkerIP(regKey string) string {
return strings.TrimPrefix(regKey, JobWorkerDir) return strings.TrimPrefix(regKey, getJobWorkerDir(e))
} }

@ -24,11 +24,22 @@ func NewEtcdWorker(config *EtcdConfig) (*Etcd, error) {
config.LocalIP = goip.GetOutsideIp() config.LocalIP = goip.GetOutsideIp()
} }
e.LocalIP = config.LocalIP e.LocalIP = config.LocalIP
e.Username = config.Username
e.Password = config.Password
e.CustomDirectory = config.CustomDirectory
e.Client, err = clientv3.New(clientv3.Config{ v3Config := clientv3.Config{
Endpoints: e.Endpoints, Endpoints: e.Endpoints,
DialTimeout: e.DialTimeout, DialTimeout: e.DialTimeout,
}) }
// 判断有没有配置用户信息
if e.Username != "" {
v3Config.Username = e.Username
v3Config.Password = e.Password
}
e.Client, err = clientv3.New(v3Config)
if err != nil { if err != nil {
return nil, errors.New("连接失败:" + err.Error()) return nil, errors.New("连接失败:" + err.Error())
} }
@ -59,7 +70,7 @@ func (e Etcd) RegisterWorker() {
for { for {
// 注册路径 // 注册路径
regKey = JobWorkerDir + e.LocalIP regKey = getJobWorkerDir(e) + e.LocalIP
log.Println("租约:", regKey) log.Println("租约:", regKey)
cancelFunc = nil cancelFunc = nil

@ -1,3 +1,3 @@
package gojobs package gojobs
const Version = "1.0.16" const Version = "1.0.17"

Loading…
Cancel
Save