2 Star 0 Fork 0

hero/momo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
service.go 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
hero 提交于 2024-12-19 00:14 . upd:service
package model
import (
"log"
"time"
"gitee.com/linqwen/momo/base"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var SrvDb *gorm.DB
func init() {
var err error
SrvDb, err = gorm.Open(sqlite.Open("data/conf.db"), &gorm.Config{})
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
SrvDb.AutoMigrate(&ServiceEntity{})
go healthCheck()
}
func healthCheck() {
time.Sleep(100 * time.Millisecond)
// 使用 where 条件来查找并更新所有符合条件的服务实体
if err := SrvDb.Where("last_seen < ?", time.Now().Add(-120*time.Second)).Update("status", 0).Error; err != nil {
log.Printf("Failed to update service entities: %v", err)
}
}
type ServiceEntity struct {
base.IdTimeStampsModel
Name string `gorm:"column:name" json:"Name" form:"Name" comment:"Name"`
Host string `gorm:"column:host" json:"Host" form:"Host" comment:"Host"`
Port int `gorm:"column:port;default:0" json:"Port" form:"Port" comment:"Port"`
Admin string `gorm:"column:admin" json:"Admin" form:"Admin" comment:"Admin"`
ExePath string `gorm:"column:exe_path" json:"ExePath" form:"ExePath" comment:"ExePath"`
Args string `gorm:"column:args" json:"Args" form:"Args" comment:"Args"`
SystemdName string `gorm:"column:systemd_name" json:"SystemdName" form:"SystemdName" comment:"SystemdName"`
SystemdStatus string `gorm:"column:systemd_status" json:"SystemdStatus" form:"SystemdStatus" comment:"SystemdStatus"`
Status int `gorm:"column:status" json:"Status" form:"Status" comment:"状态"`
// LastSeen base.ISO8601Time `gorm:"column:last_seen" json:"LastSeen" form:"LastSeen" comment:"LastSeen"`
Mode string `gorm:"column:mode" json:"Mode" form:"Mode" comment:"Mode"`
Acl bool `gorm:"column:acl" json:"Acl" form:"Acl" comment:"Acl"`
Acs bool `gorm:"column:acs" json:"Acs" form:"Acs" comment:"Acs"`
Log bool `gorm:"column:log" json:"Log" form:"Log" comment:"Log"`
Cpu int `gorm:"column:cpu" json:"Cpu" form:"Cpu" comment:"Cpu"`
}
func (c ServiceEntity) TableName() string { return "srv_service" }
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/linqwen/momo.git
git@gitee.com:linqwen/momo.git
linqwen
momo
momo
v1.1.30

搜索帮助