package models import ( "time" "github.com/pkg/errors" "rocketgit.ru/rsmon/worker/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 }