Repository navigation
Expand file tree
/
Copy pathwork.go
More file actions
89 lines (78 loc) · 2.22 KB
/
Copy pathwork.go
File metadata and controls
89 lines (78 loc) · 2.22 KB
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
package web
import (
"encoding/json"
"log"
"sync/atomic"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/letsdebug/letsdebug"
)
var (
testsRun = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "letsdebug",
Name: "tests_run_total",
Help: "The total number of processed tests",
},
[]string{"method"})
testsFailed = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "letsdebug",
Name: "tests_failed_total",
Help: "The total number of tests encountering internal errors",
},
[]string{"method"})
)
type workRequest struct {
ID int
Domain string
Method string
Options options
}
func (s *server) runWorkers(numWorkers int) {
for i := 0; i < numWorkers; i++ {
go s.work()
}
}
func (s *server) work() {
defer func() {
log.Fatalln("worker exited abnormally")
}()
for req := range s.workCh {
log.Printf("Received notification: %+v", req)
atomic.AddInt32(&s.busyWorkers, 1)
// Ignore failure
_, _ = s.db.Exec(`UPDATE tests SET started_at = CURRENT_TIMESTAMP, status = 'Processing' WHERE id = $1;`, req.ID)
method := letsdebug.ValidationMethod(req.Method)
res, err := letsdebug.CheckWithOptions(req.Domain, method, letsdebug.Options{
HTTPExpectResponse: req.Options.HTTPExpectResponse,
HTTPRequestPath: req.Options.HTTPRequestPath,
})
isOk := false
if err == nil {
isOk = true
for _, p := range res {
if p.Severity != letsdebug.SeverityInfo && p.Severity != letsdebug.SeverityDebug {
isOk = false
break
}
}
} else {
isOk = false
}
testsRun.With(prometheus.Labels{"method": string(method)}).Inc()
result := resultView{Problems: res, IsOk: isOk}
if err != nil {
testsFailed.With(prometheus.Labels{"method": string(method)}).Inc()
result.Error = err.Error()
}
strResult, _ := json.Marshal(result)
if _, err := s.db.Exec(`UPDATE tests SET completed_at = CURRENT_TIMESTAMP, status = 'Complete', result = $2 WHERE id = $1;`,
req.ID, string(strResult)); err != nil {
log.Printf("Error storing test %d result: %v", req.ID, err)
continue
}
atomic.AddInt32(&s.busyWorkers, -1)
log.Printf("Test %d complete", req.ID)
}
}