Files
worker/app/models/selfcheck.go
Gleb Tv 2c7a0236da feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
2026-07-13 17:55:14 +03:00

56 строки
1.1 KiB
Go

package models
import (
"time"
"github.com/pkg/errors"
"rsgit.ru/rsmon/rsmon/app/models/concerns"
)
// SelfCheck provides functionality.
type SelfCheck struct {
concerns.Model
Kind string `gorm:"not null;uniqueIndex:selfchecks" json:"kind"`
Server *string `gorm:"uniqueIndex:selfchecks" json:"server"`
Info string `json:"info"`
LastCheck time.Time `json:"created_at"`
}
// LogCheck provides functionality.
func LogCheck(kind string) error {
m := SelfCheck{
Kind: kind,
Server: nil,
}
DB().FirstOrInit(&m, m)
m.LastCheck = time.Now()
return DB().Save(&m).Error
}
// IsOk checks if the selfcheck for the given kind ran recently.
func IsOk(kind string) (bool, string, error) {
m := SelfCheck{
Kind: kind,
Server: nil,
}
DB().First(&m, m)
if m.ID == 0 {
return false, "", errors.New("not run")
}
var ago time.Time
if kind == "exp" {
ago = time.Now().Add(-3 * time.Hour)
} else {
ago = time.Now().Add(-15 * time.Minute)
}
isOk := m.LastCheck.After(ago)
return isOk, m.LastCheck.Format(time.RFC3339), nil
}