Skip to content

Commit d6880e1

Browse files
authored
Add harvester + farmer healthchecks (#83)
* Add harvester healthcheck * Update to have an endpoint for harvester with and without plots * Add farmer healthcheck
1 parent 502b972 commit d6880e1

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

internal/healthcheck/farmer.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package healthcheck
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"time"
7+
8+
"github.com/chia-network/go-chia-libs/pkg/types"
9+
log "github.com/sirupsen/logrus"
10+
)
11+
12+
func (h *Healthcheck) farmerReceive(resp *types.WebsocketResponse) {
13+
if resp.Command != "new_signage_point" {
14+
return
15+
}
16+
17+
newSP := &types.EventFarmerNewSignagePoint{}
18+
err := json.Unmarshal(resp.Data, newSP)
19+
if err != nil {
20+
log.Errorf("Error unmarshalling: %s\n", err.Error())
21+
return
22+
}
23+
24+
h.lastFarmerTime = time.Now()
25+
}
26+
27+
// farmerHealthcheck endpoint for the farmer service as a whole
28+
func (h *Healthcheck) farmerHealthcheck() func(http.ResponseWriter, *http.Request) {
29+
return func(w http.ResponseWriter, r *http.Request) {
30+
timeMetricHealthcheckHelper(h.lastFarmerTime, w, r)
31+
}
32+
}

internal/healthcheck/harvester.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package healthcheck
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"time"
7+
8+
"github.com/chia-network/go-chia-libs/pkg/types"
9+
log "github.com/sirupsen/logrus"
10+
)
11+
12+
func (h *Healthcheck) harvesterReceive(resp *types.WebsocketResponse) {
13+
if resp.Command != "farming_info" {
14+
return
15+
}
16+
17+
farmingInfo := &types.EventHarvesterFarmingInfo{}
18+
err := json.Unmarshal(resp.Data, farmingInfo)
19+
if err != nil {
20+
log.Errorf("Error unmarshalling: %s\n", err.Error())
21+
return
22+
}
23+
24+
h.lastHarvesterTime = time.Now()
25+
26+
if farmingInfo.TotalPlots == 0 {
27+
log.Errorf("No plots found. Not Ready!")
28+
return
29+
}
30+
31+
h.lastHarvesterTimeWithPlots = time.Now()
32+
}
33+
34+
// harvesterHealthcheckWithPlots endpoint for the harvester service requiring that at least one plot is found
35+
func (h *Healthcheck) harvesterHealthcheckWithPlots() func(http.ResponseWriter, *http.Request) {
36+
return func(w http.ResponseWriter, r *http.Request) {
37+
timeMetricHealthcheckHelper(h.lastHarvesterTimeWithPlots, w, r)
38+
}
39+
}
40+
41+
// harvesterHealthcheck endpoint for the harvester service as a whole
42+
func (h *Healthcheck) harvesterHealthcheck() func(http.ResponseWriter, *http.Request) {
43+
return func(w http.ResponseWriter, r *http.Request) {
44+
timeMetricHealthcheckHelper(h.lastHarvesterTime, w, r)
45+
}
46+
}

internal/healthcheck/healthcheck.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ type Healthcheck struct {
4040

4141
// Time we got a good response from the timelord
4242
lastTimelordTime time.Time
43+
44+
// Last time the farmer received new SP info from the full node
45+
lastFarmerTime time.Time
46+
47+
// Last time harvester looked for plots eligible to farm
48+
lastHarvesterTime time.Time
49+
50+
// Last time harvester looked for plots eligible to farm AND had > 0 plots
51+
lastHarvesterTimeWithPlots time.Time
4352
}
4453

4554
// NewHealthcheck returns a new instance of healthcheck
@@ -99,6 +108,9 @@ func (h *Healthcheck) StartServer() error {
99108
http.HandleFunc("/seeder/readiness", h.seederReadiness())
100109
http.HandleFunc("/timelord", h.timelordHealthcheck())
101110
http.HandleFunc("/timelord/readiness", h.timelordHealthcheck())
111+
http.HandleFunc("/farmer", h.farmerHealthcheck())
112+
http.HandleFunc("/harvester", h.harvesterHealthcheck())
113+
http.HandleFunc("/harvester/with-plots", h.harvesterHealthcheckWithPlots())
102114
return http.ListenAndServe(fmt.Sprintf(":%d", h.healthcheckPort), nil)
103115
}
104116

@@ -131,10 +143,6 @@ func (h *Healthcheck) walletReceive(resp *types.WebsocketResponse) {}
131143

132144
func (h *Healthcheck) crawlerReceive(resp *types.WebsocketResponse) {}
133145

134-
func (h *Healthcheck) harvesterReceive(resp *types.WebsocketResponse) {}
135-
136-
func (h *Healthcheck) farmerReceive(resp *types.WebsocketResponse) {}
137-
138146
func (h *Healthcheck) disconnectHandler() {
139147
log.Debug("Calling disconnect handlers")
140148
// @TODO should we mark unhealthy immediately?

0 commit comments

Comments
 (0)