2022-04-28 16:35:02 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
|
2022-08-02 18:45:09 +00:00
|
|
|
"code.1533b4dc0.de/prskr/nurse/check"
|
|
|
|
"code.1533b4dc0.de/prskr/nurse/config"
|
|
|
|
"code.1533b4dc0.de/prskr/nurse/grammar"
|
|
|
|
"code.1533b4dc0.de/prskr/nurse/validation"
|
2022-04-28 16:35:02 +00:00
|
|
|
)
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
var _ check.SystemChecker = (*PingCheck)(nil)
|
2022-04-28 16:35:02 +00:00
|
|
|
|
|
|
|
type PingCheck struct {
|
|
|
|
redis.UniversalClient
|
2022-06-09 20:12:45 +00:00
|
|
|
validators validation.Validator[redis.Cmder]
|
2022-04-28 16:35:02 +00:00
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:22:21 +00:00
|
|
|
func (p PingCheck) Execute(ctx check.Context) error {
|
2022-04-28 16:35:02 +00:00
|
|
|
if p.Message == "" {
|
|
|
|
return p.Ping(ctx).Err()
|
|
|
|
}
|
2022-08-07 10:22:21 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
attemptCtx, cancel := ctx.AttemptContext()
|
|
|
|
err := p.executeAttempt(attemptCtx)
|
|
|
|
cancel()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p PingCheck) executeAttempt(ctx context.Context) error {
|
2022-04-28 16:35:02 +00:00
|
|
|
if resp, err := p.Do(ctx, "PING", p.Message).Text(); err != nil {
|
|
|
|
return err
|
|
|
|
} else if resp != p.Message {
|
|
|
|
return fmt.Errorf("expected value %s got %s", p.Message, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
func (p *PingCheck) UnmarshalCheck(c grammar.Check, lookup config.ServerLookup) error {
|
2022-04-28 16:35:02 +00:00
|
|
|
const (
|
|
|
|
serverOnlyArgCount = 1
|
|
|
|
serverAndMessageArgCount = 2
|
|
|
|
)
|
|
|
|
|
2022-06-09 15:20:06 +00:00
|
|
|
val, _ := GenericCommandValidatorFor("PONG")
|
2022-06-09 20:12:45 +00:00
|
|
|
|
|
|
|
validators := validation.Chain[redis.Cmder]{}
|
|
|
|
validators = append(validators, val)
|
|
|
|
|
|
|
|
p.validators = validators
|
2022-04-28 16:35:02 +00:00
|
|
|
|
|
|
|
init := c.Initiator
|
|
|
|
switch len(init.Params) {
|
|
|
|
case 0:
|
|
|
|
return grammar.ErrMissingServer
|
|
|
|
case serverAndMessageArgCount:
|
|
|
|
if msg, err := init.Params[1].AsString(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2022-06-09 15:20:06 +00:00
|
|
|
val, _ := GenericCommandValidatorFor(msg)
|
2022-06-09 20:12:45 +00:00
|
|
|
p.validators = validation.Chain[redis.Cmder]{val}
|
2022-04-28 16:35:02 +00:00
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case serverOnlyArgCount:
|
2022-05-08 09:00:22 +00:00
|
|
|
if cli, err := clientFromParam(init.Params[0], lookup); err != nil {
|
2022-04-28 16:35:02 +00:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
p.UniversalClient = cli
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return grammar.ErrAmbiguousParamCount
|
|
|
|
}
|
|
|
|
}
|