Skip to content
This repository was archived by the owner on Feb 20, 2026. It is now read-only.

Commit 887fcee

Browse files
committed
Add health check endpoint so uptime tools can watch scrapers
1 parent c2724e6 commit 887fcee

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,20 @@ You can do that with the following shell variable:
161161
```sh
162162
export RTCV_SCRAPER_CLIENT_ENV='{}'
163163
```
164+
165+
## Health check service
166+
167+
For monitoring the health of a scraper you can start a small web service that will only return a 200 if the scraper is running.
168+
169+
```sh
170+
export RTCV_SCRAPER_CLIENT_HEALTH_CHECK_PORT=2000
171+
rtcv_scraper_client ..
172+
```
173+
174+
You should now be able to check if the scraper is running by executing the following command
175+
176+
```sh
177+
curl -s -D - http://localhost:2000
178+
# HTTP/1.1 200 OK
179+
# ...
180+
```

health_check.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net"
7+
8+
"github.com/valyala/fasthttp"
9+
)
10+
11+
func startHealthCheckServer(port string) {
12+
requestHandler := func(ctx *fasthttp.RequestCtx) {
13+
ctx.Response.AppendBody([]byte("true"))
14+
ctx.Response.Header.Set("Content-Type", "application/json")
15+
}
16+
17+
s := &fasthttp.Server{Handler: requestHandler}
18+
19+
address := "0.0.0.0:" + port
20+
l, err := net.Listen("tcp4", address)
21+
if err != nil {
22+
log.Fatalf("Error, unable to start health check service at \"%s\" error %s", address, err.Error())
23+
}
24+
25+
fmt.Println("running health check service at", address)
26+
27+
err = s.Serve(l)
28+
if err != nil {
29+
log.Fatal("Error in health check service Serve: " + err.Error())
30+
}
31+
}

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ func main() {
155155

156156
useAddress := startWebserver(env, api, loginUsers)
157157

158+
healthCheckPort := os.Getenv("RTCV_SCRAPER_CLIENT_HEALTH_CHECK_PORT")
159+
if healthCheckPort != "" {
160+
go startHealthCheckServer(healthCheckPort)
161+
}
162+
158163
fmt.Println("running scraper..")
159164

160165
if len(os.Args) <= 1 {

webserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package main
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"log"
76
"net"
7+
"strconv"
88
"strings"
99
"time"
1010

@@ -122,7 +122,7 @@ func startWebserver(env Env, api *API, loginUsers []EnvUser) string {
122122
log.Fatal("Could not find a free port to start the webserver")
123123
}
124124

125-
address := fmt.Sprintf("127.0.0.1:%d", portAttempt)
125+
address := "127.0.0.1:" + strconv.Itoa(portAttempt)
126126

127127
l, err := net.Listen("tcp4", address)
128128
if err != nil {

0 commit comments

Comments
 (0)