增加定时任务 处理异常
This commit is contained in:
94
main.go
94
main.go
@@ -2,9 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"energy-management-system/core"
|
||||
"energy-management-system/core/cron"
|
||||
"energy-management-system/core/gorm"
|
||||
"energy-management-system/core/logger"
|
||||
"energy-management-system/core/viper"
|
||||
"energy-management-system/global"
|
||||
"energy-management-system/request"
|
||||
"energy-management-system/request/validator"
|
||||
"energy-management-system/router"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -16,19 +19,26 @@ import (
|
||||
|
||||
func init() {
|
||||
// 配置
|
||||
core.InitViper()
|
||||
viper.InitViper()
|
||||
// 日志
|
||||
logger.InitLogger()
|
||||
// 数据库
|
||||
core.InitGorm()
|
||||
gorm.InitGorm()
|
||||
// gin验证器
|
||||
request.InitTrans()
|
||||
validator.InitTrans()
|
||||
// 可选初始化表结构
|
||||
core.AutoMigrate(global.Db)
|
||||
gorm.AutoMigrate(global.Db)
|
||||
// 可选初始化表数据
|
||||
core.AutoInitDbData()
|
||||
gorm.AutoInitDbData()
|
||||
|
||||
}
|
||||
func main() {
|
||||
|
||||
cron.Register("0/1 * * * * ", func() {
|
||||
fmt.Println(time.Now())
|
||||
})
|
||||
cron.CronRun()
|
||||
|
||||
r := router.InitRouter()
|
||||
host := global.AppConf.Service.Http.Host
|
||||
port := global.AppConf.Service.Http.Port
|
||||
@@ -39,78 +49,8 @@ func main() {
|
||||
|
||||
// 优雅关闭
|
||||
gracefulShutdown(srv)
|
||||
|
||||
//// 定义一个切片来接收查询结果
|
||||
//var users []peaks_valleys.PeakValleyTimeBlock
|
||||
//
|
||||
//// 查询所有用户
|
||||
//result := global.Db.Find(&users)
|
||||
//if result.Error != nil {
|
||||
// fmt.Println("Error:", result.Error)
|
||||
//}
|
||||
//
|
||||
//// 打印查询结果
|
||||
//for _, user := range users {
|
||||
//
|
||||
// if user.BlockIndex < 20 {
|
||||
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
|
||||
// BlockId: user.BlockIndex,
|
||||
// Price: 35,
|
||||
// PeakValleyType: 5,
|
||||
// })
|
||||
// } else if user.BlockIndex < 40 {
|
||||
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
|
||||
// BlockId: user.BlockIndex,
|
||||
// Price: 45,
|
||||
// PeakValleyType: 4,
|
||||
// })
|
||||
// } else if user.BlockIndex < 60 {
|
||||
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
|
||||
// BlockId: user.BlockIndex,
|
||||
// Price: 55,
|
||||
// PeakValleyType: 3,
|
||||
// })
|
||||
// } else if user.BlockIndex < 80 {
|
||||
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
|
||||
// BlockId: user.BlockIndex,
|
||||
// Price: 65,
|
||||
// PeakValleyType: 2,
|
||||
// })
|
||||
// } else if user.BlockIndex < 100 {
|
||||
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
|
||||
// BlockId: user.BlockIndex,
|
||||
// Price: 85,
|
||||
// PeakValleyType: 1,
|
||||
// })
|
||||
// } else if user.BlockIndex <= 144 {
|
||||
// global.Db.Create(&peaks_valleys.PeakValleyTimeBlockPrice{
|
||||
// BlockId: user.BlockIndex,
|
||||
// Price: 35,
|
||||
// PeakValleyType: 5,
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// //fmt.Printf("ID: %d, Name: %s, Email: %s\n", user.BlockIndex)
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
//func loadConfig() (host, port string) {
|
||||
// host = viper.GetString("service.http.host")
|
||||
// if host == "" {
|
||||
// host = "localhost"
|
||||
// fmt.Println("Using default host:", host)
|
||||
// }
|
||||
//
|
||||
// port = viper.GetString("service.http.port")
|
||||
// if port == "" {
|
||||
// port = "9999"
|
||||
// fmt.Println("Using default port:", port)
|
||||
// }
|
||||
//
|
||||
// return host, port
|
||||
//}
|
||||
|
||||
func initServer(host, port string, handler http.Handler) *http.Server {
|
||||
return &http.Server{
|
||||
Addr: fmt.Sprintf("%s:%s", host, port),
|
||||
|
||||
Reference in New Issue
Block a user