- 优化获取下发地址
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 package gojobs
const Version = "1.0.50" const Version = "1.0.51"

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

Loading…
Cancel
Save