diff --git a/core/async/asyncCheckEngineX.go b/core/async/asyncCheckEngineX.go index 0b06fde..44dc3b7 100644 --- a/core/async/asyncCheckEngineX.go +++ b/core/async/asyncCheckEngineX.go @@ -27,6 +27,12 @@ func NewAsyncCheckEngine() *AsyncCheckEngine { } func (e *AsyncCheckEngine) Start() { + // heart beat detection + if !e.heartbeat() { + logger.Errorln("cannot access target website successfully: http://api.ceye.io") + return + } + // build a request request, _ := http.NewRequest("GET", e.httpAPI, nil) client := http.Client{} @@ -66,6 +72,27 @@ func (e *AsyncCheckEngine) Start() { } } +// +// heartbeat +// @Description: check the health of http://api.ceye.io/ three times +// @receiver e +// @return bool +// +func (e *AsyncCheckEngine) heartbeat() bool { + request, _ := http.NewRequest("GET", e.httpAPI, nil) + client := http.Client{} + + for i := 0; i < 3; i++ { + response, err := client.Do(request) + if err != nil || response.StatusCode >= 500 { + logger.Debugln(err) + } else { + return true + } + } + return false +} + func (e *AsyncCheckEngine) check(token string) { logger.Infoln(fmt.Sprintf("[async check] token: %s", token)) diff --git a/core/async/asyncCheckEngineX_test.go b/core/async/asyncCheckEngineX_test.go index dce215f..364458f 100644 --- a/core/async/asyncCheckEngineX_test.go +++ b/core/async/asyncCheckEngineX_test.go @@ -1,6 +1,8 @@ package async -import "testing" +import ( + "testing" +) func TestAsyncCheckEngine_Start(t *testing.T) { type fields struct { @@ -24,3 +26,35 @@ func TestAsyncCheckEngine_Start(t *testing.T) { }) } } + +func TestAsyncCheckEngine_heartbeat(t *testing.T) { + type fields struct { + httpAPI string + lastRecordId string + } + tests := []struct { + name string + fields fields + want bool + }{ + { + name: "", + fields: fields{ + httpAPI: "http://api.ceye.io/v1/records?token=0920449a5ed8b9db7a287a66a6632498&type=http", + lastRecordId: "xxxxx", + }, + want: false, + }, // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := &AsyncCheckEngine{ + httpAPI: tt.fields.httpAPI, + lastRecordId: tt.fields.lastRecordId, + } + if got := e.heartbeat(); got != tt.want { + t.Errorf("heartbeat() = %v, want %v", got, tt.want) + } + }) + } +}