mirror of
https://github.com/eosswedenorg/antelope-api-healthcheck
synced 2026-07-04 12:03:43 +02:00
src/api/eosio_contract.go: use utils.Time
This commit is contained in:
parent
e5ddffbc5a
commit
f98e2ed794
1 changed files with 3 additions and 23 deletions
|
|
@ -3,15 +3,15 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"github.com/eosswedenorg/eosio-api-healthcheck/src/utils"
|
||||||
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
"github.com/eosswedenorg-go/haproxy/agentcheck"
|
||||||
contract_api "github.com/eosswedenorg-go/eos-contract-api-client"
|
contract_api "github.com/eosswedenorg-go/eos-contract-api-client"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EosioContract struct {
|
type EosioContract struct {
|
||||||
|
utils.Time
|
||||||
client contract_api.Client
|
client contract_api.Client
|
||||||
block_time float64
|
block_time float64
|
||||||
ts time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEosioContract(url string, block_time float64) EosioContract {
|
func NewEosioContract(url string, block_time float64) EosioContract {
|
||||||
|
|
@ -31,18 +31,6 @@ func (e EosioContract) LogInfo() LogParams {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EosioContract) SetTime(t time.Time) {
|
|
||||||
e.ts = t
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e EosioContract) GetTime() time.Time {
|
|
||||||
|
|
||||||
if e.ts.IsZero() {
|
|
||||||
return time.Now().In(time.UTC)
|
|
||||||
}
|
|
||||||
return e.ts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e EosioContract) Call() (agentcheck.Response, string) {
|
func (e EosioContract) Call() (agentcheck.Response, string) {
|
||||||
|
|
||||||
h, err := e.client.GetHealth()
|
h, err := e.client.GetHealth()
|
||||||
|
|
@ -54,16 +42,13 @@ func (e EosioContract) Call() (agentcheck.Response, string) {
|
||||||
// Check HTTP Status Code
|
// Check HTTP Status Code
|
||||||
if h.HTTPStatusCode > 299 {
|
if h.HTTPStatusCode > 299 {
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
|
|
||||||
msg := "Taking offline because %v was received from backend"
|
msg := "Taking offline because %v was received from backend"
|
||||||
return resp, fmt.Sprintf(msg, h.HTTPStatusCode)
|
return resp, fmt.Sprintf(msg, h.HTTPStatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check postgres
|
// Check postgres
|
||||||
if h.Data.Postgres.Status != "OK" {
|
if h.Data.Postgres.Status != "OK" {
|
||||||
|
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
|
|
||||||
msg := "Taking offline because Postgres reported '%s'"
|
msg := "Taking offline because Postgres reported '%s'"
|
||||||
return resp, fmt.Sprintf(msg, h.Data.Postgres.Status)
|
return resp, fmt.Sprintf(msg, h.Data.Postgres.Status)
|
||||||
}
|
}
|
||||||
|
|
@ -71,24 +56,19 @@ func (e EosioContract) Call() (agentcheck.Response, string) {
|
||||||
// Check redis
|
// Check redis
|
||||||
if h.Data.Redis.Status != "OK" {
|
if h.Data.Redis.Status != "OK" {
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
|
|
||||||
msg := "Taking offline because Redis reported '%s'"
|
msg := "Taking offline because Redis reported '%s'"
|
||||||
return resp, fmt.Sprintf(msg, h.Data.Redis.Status)
|
return resp, fmt.Sprintf(msg, h.Data.Redis.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate head block.
|
// Validate head block.
|
||||||
now := e.GetTime()
|
diff := e.GetTime().Sub(h.Data.Chain.HeadTime).Seconds()
|
||||||
diff := now.Sub(h.Data.Chain.HeadTime).Seconds()
|
|
||||||
|
|
||||||
if diff > e.block_time {
|
if diff > e.block_time {
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
|
|
||||||
msg := "Taking offline because head block is lagging %.0f seconds"
|
msg := "Taking offline because head block is lagging %.0f seconds"
|
||||||
return resp, fmt.Sprintf(msg, diff)
|
return resp, fmt.Sprintf(msg, diff)
|
||||||
} else if diff < -e.block_time {
|
} else if diff < -e.block_time {
|
||||||
|
|
||||||
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
resp := agentcheck.NewStatusMessageResponse(agentcheck.Down, "")
|
||||||
|
|
||||||
msg := "Taking offline because head block is %.0f seconds into the future"
|
msg := "Taking offline because head block is %.0f seconds into the future"
|
||||||
return resp, fmt.Sprintf(msg, diff)
|
return resp, fmt.Sprintf(msg, diff)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue