-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_instance.go
85 lines (65 loc) · 1.91 KB
/
worker_instance.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
package goworker
import (
"time"
)
func newDefaultWorkerInstance(id string, sn string) DefaultWorkerInstance {
return DefaultWorkerInstance{
Id: id,
ServiceName: sn,
CreatedAt: time.Now().Unix(),
HealthStatus: Yellow,
}
}
type HealthStatus int
const (
Green HealthStatus = 0
Yellow HealthStatus = 1
Red HealthStatus = 2
)
type DefaultWorkerInstance struct {
Id string `json:"id"`
ServiceName string `json:"serviceName"`
CreatedAt int64 `json:"createdAt"`
ShardIndex int `json:"shardIndex"`
HealthStatus HealthStatus `json:"healthStatus"`
Expiration int64 `json:"expiration"` //秒,服务到期时间,暂只有redis集群模式下 使用
}
func (d *DefaultWorkerInstance) GetId() string {
return d.Id
}
func (d *DefaultWorkerInstance) GetServiceName() string {
return d.ServiceName
}
func (d *DefaultWorkerInstance) GetCreatedAt() int64 {
return d.CreatedAt
}
func (d *DefaultWorkerInstance) GetShardIndex() int {
return d.ShardIndex
}
func (d *DefaultWorkerInstance) GetHealthStatus() HealthStatus {
return d.HealthStatus
}
func (d *DefaultWorkerInstance) GetExpiration() int64 {
return d.Expiration
}
func (d *DefaultWorkerInstance) UpdateShardIndex(i int) {
d.ShardIndex = i
}
func (d *DefaultWorkerInstance) UpdateExpiration(exp int64) {
d.Expiration = exp
}
func (d *DefaultWorkerInstance) UpdateHealthStatus(hs HealthStatus) {
d.HealthStatus = hs
}
type DefaultWorkerInstanceSlice []DefaultWorkerInstance
func (s DefaultWorkerInstanceSlice) Len() int { return len(s) }
func (s DefaultWorkerInstanceSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s DefaultWorkerInstanceSlice) Less(i, j int) bool {
if s[i].ShardIndex != s[j].ShardIndex {
return s[i].ShardIndex < s[j].ShardIndex
}
if s[i].Expiration != s[j].Expiration {
return s[i].Expiration < s[j].Expiration
}
return s[i].CreatedAt < s[j].CreatedAt
}