-
Notifications
You must be signed in to change notification settings - Fork 38
/
heartbeat.go
43 lines (38 loc) · 957 Bytes
/
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
package handlers
import (
"context"
"github.com/rs/zerolog/log"
"github.com/runabol/tork"
"github.com/runabol/tork/datastore"
"github.com/runabol/tork/middleware/node"
)
type heartbeatHandler struct {
ds datastore.Datastore
}
func NewHeartbeatHandler(ds datastore.Datastore) node.HandlerFunc {
h := &heartbeatHandler{
ds: ds,
}
return h.handle
}
func (h *heartbeatHandler) handle(ctx context.Context, n *tork.Node) error {
_, err := h.ds.GetNodeByID(ctx, n.ID)
if err == datastore.ErrNodeNotFound {
log.Info().
Str("node-id", n.ID).
Str("hostname", n.Hostname).
Msg("received first heartbeat")
return h.ds.CreateNode(ctx, n)
}
return h.ds.UpdateNode(ctx, n.ID, func(u *tork.Node) error {
// ignore "old" heartbeats
if u.LastHeartbeatAt.After(n.LastHeartbeatAt) {
return nil
}
u.LastHeartbeatAt = n.LastHeartbeatAt
u.CPUPercent = n.CPUPercent
u.Status = n.Status
u.TaskCount = n.TaskCount
return nil
})
}