- 优化获取下发地址
continuous-integration/drone/push Build is failing Details
continuous-integration/drone/tag Build is failing Details

master v1.0.51
李光春 2 years ago
parent 2db9b0f087
commit 622f6be827

@ -1,3 +1,3 @@
package gojobs
const Version = "1.0.50"
const Version = "1.0.51"

@ -5,13 +5,19 @@ import (
"errors"
"fmt"
"go.dtapp.net/gojobs/jobs_gorm_model"
"go.dtapp.net/gostring"
"log"
"math/rand"
"time"
)
// GetIssueAddress 获取下发地址
func (j *JobsGorm) GetIssueAddress(ctx context.Context, v *jobs_gorm_model.Task) (address string, err error) {
// workers 在线列表
// v 任务信息
// ---
// address 下发地址
// err 错误信息
func (j *JobsGorm) GetIssueAddress(workers []string, v *jobs_gorm_model.Task) (address string, err error) {
var (
currentIp = "" // 当前Ip
appointIpStatus = false // 指定Ip状态
@ -23,34 +29,34 @@ func (j *JobsGorm) GetIssueAddress(ctx context.Context, v *jobs_gorm_model.Task)
appointIpStatus = true
}
workers, err := j.GetSubscribeClientList(ctx)
if err != nil {
return address, errors.New(fmt.Sprintf("获取在线客户端列表失败:%s", err.Error()))
}
if len(workers) <= 0 {
return address, errors.New("没有客户端在线")
}
//workers, err := j.GetSubscribeClientList(ctx)
//if err != nil {
// return address, errors.New(fmt.Sprintf("获取在线客户端列表失败:%s", err.Error()))
//}
//if len(workers) <= 0 {
// return address, errors.New("没有客户端在线")
//}
// 只有一个客户端在线
if len(workers) == 1 {
if appointIpStatus == true {
// 判断是否指定某ip执行
if currentIp == workers[0] {
if gostring.Contains(currentIp, workers[0]) == true {
return j.config.cornPrefix + "_" + v.SpecifyIp, nil
}
return address, errors.New("执行的客户端不在线")
return address, errors.New(fmt.Sprintf("需要执行的[%s]客户端不在线", currentIp))
}
return j.config.cornPrefix + "_" + workers[0], nil
}
// 优先处理指定某ip执行
if appointIpStatus == true {
for _, wv := range workers {
if currentIp == wv {
return j.config.cornPrefix + "_" + wv, nil
for wk, wv := range workers {
if gostring.Contains(currentIp, wv) == true {
return j.config.cornPrefix + "_" + workers[wk], nil
}
}
return address, errors.New("执行的客户端不在线")
return address, errors.New(fmt.Sprintf("需要执行的[%s]客户端不在线", currentIp))
} else {
// 随机返回一个
zxIp := workers[j.random(0, len(workers))]
@ -87,6 +93,8 @@ func (j *JobsGorm) GetSubscribeClientList(ctx context.Context) ([]string, error)
}
// 随机返回一个
// min最小
// max最大
func (j *JobsGorm) random(min, max int) int {
if max-min <= 0 {
return 0

Loading…
Cancel
Save