-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
59 lines (51 loc) · 1.36 KB
/
health.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
package cct_index
import (
"encoding/json"
"fmt"
"github.com/xiaogogonuo/cct-spider/internal/cct_index/constant"
"github.com/xiaogogonuo/cct-spider/internal/cct_index/poster"
"github.com/xiaogogonuo/cct-spider/pkg/logger"
"os/exec"
"time"
)
type CurlResponse struct {
TimeStamp string `json:"timestamp"`
Status int `json:"status"`
Error string `json:"error"`
Message string `json:"message"`
Path string `json:"path"`
}
func RemoteConnect() bool {
cmd := exec.Command("curl", constant.Service)
out, err := cmd.Output()
if err != nil {
logger.Error(err.Error())
return false
}
var cr CurlResponse
if err = json.Unmarshal(out, &cr); err != nil {
logger.Error(err.Error())
return false
}
switch cr.Status {
case 404, 500:
return false
}
return true
}
// RemoteListen 每隔5分钟检测一次远程服务器接口是否健康
// 如果远程服务器接口异常,则下发邮件通知并停止应用,待远程服务器接口正常后重新启动爬虫服务
func RemoteListen(listener chan struct{}) {
for {
if !RemoteConnect() {
// 下发邮件
body := fmt.Sprintf("指标对应的远程接口连接失败:%s", constant.Service)
if err := poster.E163.Send(poster.Receivers, poster.Subject, body); err != nil {
logger.Error(err.Error())
}
listener <- struct{}{}
return
}
time.Sleep(ListenInterval)
}
}