feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
Этот коммит содержится в:
55
app/models/selfcheck.go
Обычный файл
55
app/models/selfcheck.go
Обычный файл
@@ -0,0 +1,55 @@
|
||||
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
|
||||
}
|
||||
Ссылка в новой задаче
Block a user