2022-04-28 16:35:02 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-04 10:22:49 +00:00
|
|
|
"log/slog"
|
2022-04-28 16:35:02 +00:00
|
|
|
|
2023-03-01 17:22:31 +00:00
|
|
|
"github.com/redis/go-redis/v9"
|
2022-04-28 16:35:02 +00:00
|
|
|
|
2023-12-04 15:59:10 +00:00
|
|
|
"code.icb4dc0.de/prskr/nurse/internal/retry"
|
|
|
|
|
2022-09-22 09:46:36 +00:00
|
|
|
"code.icb4dc0.de/prskr/nurse/check"
|
|
|
|
"code.icb4dc0.de/prskr/nurse/config"
|
|
|
|
"code.icb4dc0.de/prskr/nurse/grammar"
|
|
|
|
"code.icb4dc0.de/prskr/nurse/validation"
|
2022-04-28 16:35:02 +00:00
|
|
|
)
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
var _ check.SystemChecker = (*GetCheck)(nil)
|
2022-04-28 16:35:02 +00:00
|
|
|
|
|
|
|
type GetCheck struct {
|
|
|
|
redis.UniversalClient
|
2022-06-09 20:12:45 +00:00
|
|
|
validators validation.Validator[redis.Cmder]
|
2022-04-28 16:35:02 +00:00
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:22:21 +00:00
|
|
|
func (g *GetCheck) Execute(ctx check.Context) error {
|
2023-12-04 15:59:10 +00:00
|
|
|
logger := slog.Default().With(
|
2023-12-04 10:22:49 +00:00
|
|
|
slog.String("check", "redis.GET"),
|
|
|
|
slog.String("key", g.Key),
|
|
|
|
)
|
|
|
|
|
2023-12-04 15:59:10 +00:00
|
|
|
return retry.Retry(ctx, ctx.AttemptCount(), ctx.AttemptTimeout(), func(ctx context.Context, attempt int) error {
|
|
|
|
logger.Debug("Execute check", slog.Int("attempt", attempt))
|
2022-08-07 10:22:21 +00:00
|
|
|
|
2023-12-04 15:59:10 +00:00
|
|
|
cmd := g.Get(ctx, g.Key)
|
2022-04-28 16:35:02 +00:00
|
|
|
|
2023-12-04 15:59:10 +00:00
|
|
|
if err := cmd.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-28 16:35:02 +00:00
|
|
|
|
2023-12-04 15:59:10 +00:00
|
|
|
return g.validators.Validate(cmd)
|
|
|
|
})
|
2022-04-28 16:35:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-08 09:00:22 +00:00
|
|
|
func (g *GetCheck) UnmarshalCheck(c grammar.Check, lookup config.ServerLookup) error {
|
2022-04-28 16:35:02 +00:00
|
|
|
const serverAndKeyArgsNumber = 2
|
|
|
|
inst := c.Initiator
|
|
|
|
if err := grammar.ValidateParameterCount(inst.Params, serverAndKeyArgsNumber); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2022-05-08 09:00:22 +00:00
|
|
|
if g.UniversalClient, err = clientFromParam(inst.Params[0], lookup); err != nil {
|
2022-04-28 16:35:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.Key, err = inst.Params[1].AsString(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:12:45 +00:00
|
|
|
if g.validators, err = registry.ValidatorsForFilters(c.Validators); err != nil {
|
2022-05-08 09:00:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-28 16:35:02 +00:00
|
|
|
return nil
|
|
|
|
}
|