-
Notifications
You must be signed in to change notification settings - Fork 36
/
heartbeat.go
143 lines (121 loc) · 3.48 KB
/
heartbeat.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package workers
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/digitalocean/go-workers2/storage"
)
type HeartbeatInfo struct {
Hostname string `json:"hostname"`
StartedAt int64 `json:"started_at"`
Pid int `json:"Pid"`
Tag string `json:"tag"`
Concurrency int `json:"concurrency"`
Queues []string `json:"queues"`
Labels []string `json:"labels"`
Identity string `json:"identity"`
}
type HeartbeatWorkerMsgWrapper struct {
Queue string `json:"Queue"`
Payload string `json:"payload"`
RunAt int64 `json:"run_at"`
Tid string `json:"Tid"`
}
type HeartbeatWorkerMsg struct {
Retry int `json:"retry"`
Queue string `json:"Queue"`
Backtrace bool `json:"backtrace"`
Class string `json:"class"`
Args *Args `json:"args"`
Jid string `json:"jid"`
CreatedAt int64 `json:"created_at"`
EnqueuedAt int64 `json:"enqueued_at"`
}
type afterHeartbeatFunc func(heartbeat *storage.Heartbeat, manager *Manager, staleMessageUpdates []*staleMessageUpdate) error
func GenerateProcessNonce() (string, error) {
bytes := make([]byte, 12)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func (m *Manager) buildHeartbeat(heartbeatTime time.Time, ttl time.Duration) (*storage.Heartbeat, error) {
queues := []string{}
concurrency := 0
busy := 0
pid := os.Getpid()
var workerHeartbeats []storage.WorkerHeartbeat
for _, w := range m.workers {
queues = append(queues, w.queue)
concurrency += w.concurrency // add up all concurrency here because it can be specified on a per-worker basis.
busy += len(w.inProgressMessages())
w.runnersLock.Lock()
for _, r := range w.runners {
workerHeartbeat := storage.WorkerHeartbeat{
Pid: pid,
Tid: r.tid,
Queue: w.queue,
InProgressQueue: w.inProgressQueue,
}
workerHeartbeats = append(workerHeartbeats, workerHeartbeat)
}
w.runnersLock.Unlock()
}
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
if m.opts.ManagerDisplayName != "" {
hostname = hostname + ":" + m.opts.ManagerDisplayName
}
tag := "default"
if m.opts.Namespace != "" {
tag = strings.ReplaceAll(m.opts.Namespace, ":", "")
}
heartbeatID, err := m.getHeartbeatID()
if err != nil {
return nil, err
}
heartbeatInfo := &HeartbeatInfo{
Hostname: hostname,
StartedAt: m.startedAt.UTC().Unix(),
Pid: pid,
Tag: tag,
Concurrency: concurrency,
Queues: queues,
Labels: []string{},
Identity: heartbeatID,
}
heartbeatInfoJson, err := json.Marshal(heartbeatInfo)
if err != nil {
return nil, err
}
heartbeat := &storage.Heartbeat{
Identity: heartbeatID,
Beat: heartbeatTime.UTC().Unix(),
Quiet: false,
Busy: busy,
RSS: 0, // rss is not currently supported
Info: string(heartbeatInfoJson),
Pid: pid,
ActiveManager: m.IsActive(),
WorkerHeartbeats: workerHeartbeats,
Ttl: ttl,
}
if m.opts.Heartbeat != nil && m.opts.Heartbeat.PrioritizedManager != nil {
heartbeat.ManagerPriority = m.opts.Heartbeat.PrioritizedManager.ManagerPriority
}
return heartbeat, nil
}
func (m *Manager) getHeartbeatID() (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}
pid := os.Getpid()
return fmt.Sprintf("%s:%d:%s", hostname, pid, m.processNonce), nil
}