forked from farseer-go/fSchedule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverVO.go
89 lines (76 loc) · 3.02 KB
/
serverVO.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package fSchedule
import (
"github.com/farseer-go/collections"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/flog"
"github.com/farseer-go/utils/http"
"math/rand"
)
const tokenName = "FSchedule-ACCESS-TOKEN"
var defaultServer serverVO
type serverIndex = int
type serverAddress = string
// 服务端配置
type serverVO struct {
Address []string
Token string
}
// 随机一个服务端地址
func (receiver *serverVO) getAddress(ignoreIndex int) (serverAddress, serverIndex) {
count := len(receiver.Address)
if count == 1 {
return receiver.Address[0], 0
}
index := rand.Intn(count - 1)
// 如果随机到的索引值与要排除的索引值一样时,则重新随机
for ignoreIndex == index {
index = rand.Intn(count - 1)
}
return receiver.Address[index], index
}
type RegistryResponse struct {
ClientIp string // 客户端IP
ClientPort int // 客户端端口
}
// 服务端注册接口
func (receiver *serverVO) registry(bodyJson []byte) (core.ApiResponse[RegistryResponse], error) {
address, _ := receiver.getAddress(-1)
var apiResponse core.ApiResponse[RegistryResponse]
_, err := http.NewClient(address+"/api/registry").HeadAdd(tokenName, receiver.Token).Body(bodyJson).Timeout(5000).PostUnmarshal(&apiResponse)
if err != nil {
_ = flog.Errorf("客户端注册失败:%s", err.Error())
}
return apiResponse, err
}
// 服务端下线接口
func (receiver *serverVO) logout(bodyJson []byte) (core.ApiResponse[any], error) {
address, _ := receiver.getAddress(-1)
var apiResponse core.ApiResponse[any]
_, err := http.NewClient(address+"/api/logout").HeadAdd(tokenName, receiver.Token).Body(bodyJson).PostUnmarshal(&apiResponse)
return apiResponse, err
}
type TaskReportDTO struct {
Id int64 // 主键
TaskGroupId int64 // 任务组ID
Ver int // 任务版本
Name string // 实现Job的特性名称(客户端识别哪个实现类)
Data collections.Dictionary[string, string] // 数据
NextTimespan int64 // 下次执行时间
Progress int // 当前进度
Status TaskStatus // 执行状态
RunSpeed int64 // 执行速度
}
// 客户端回调
func (receiver *serverVO) taskReport(bodyJson []byte) (core.ApiResponse[any], error) {
address, _ := receiver.getAddress(-1)
var apiResponse core.ApiResponse[any]
_, err := http.NewClient(address+"/api/taskReport").HeadAdd(tokenName, receiver.Token).Body(bodyJson).PostUnmarshal(&apiResponse)
return apiResponse, err
}
// 上传日志
func (receiver *serverVO) logReport(bodyJson []byte) (core.ApiResponse[any], error) {
address, _ := receiver.getAddress(-1)
var apiResponse core.ApiResponse[any]
_, err := http.NewClient(address+"/api/logReport").HeadAdd(tokenName, receiver.Token).Body(bodyJson).PostUnmarshal(&apiResponse)
return apiResponse, err
}