增加定时任务 处理异常
This commit is contained in:
44
core/cron/crontab.go
Normal file
44
core/cron/crontab.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"energy-management-system/utils"
|
||||
"energy-management-system/utils/recovery"
|
||||
"fmt"
|
||||
"github.com/robfig/cron/v3"
|
||||
"reflect"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var servers server
|
||||
|
||||
type server struct {
|
||||
Cron []string
|
||||
Func []func()
|
||||
}
|
||||
|
||||
func (c *server) register(cron string, f func()) {
|
||||
c.Cron = append(c.Cron, cron)
|
||||
c.Func = append(c.Func, f)
|
||||
}
|
||||
|
||||
func CronRun() {
|
||||
c := cron.New()
|
||||
for k, v := range servers.Cron {
|
||||
eid, err := c.AddFunc(v, wrap(servers.Func[k]))
|
||||
fmt.Println("定时任务:", eid, "添加成功")
|
||||
utils.Exit(err, "添加定时器错误:")
|
||||
}
|
||||
c.Start()
|
||||
}
|
||||
|
||||
func wrap(f func()) func() {
|
||||
return func() {
|
||||
defer recovery.CronRecovery(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name())
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
// Register 参数:规则(s m h d m w),函数
|
||||
func Register(rule string, f func()) {
|
||||
servers.register(rule, f)
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package core
|
||||
package gorm
|
||||
|
||||
import (
|
||||
init_db_data "energy-management-system/core/init-db-data"
|
||||
"energy-management-system/global"
|
||||
"energy-management-system/model/init-db-data"
|
||||
peak_valley "energy-management-system/model/peak-valley"
|
||||
"fmt"
|
||||
"gorm.io/driver/postgres"
|
||||
@@ -1,31 +0,0 @@
|
||||
package init_db_data
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type InitDBFunc interface {
|
||||
Init() (err error)
|
||||
}
|
||||
|
||||
// 初始化数据库
|
||||
func initDB(InitDBFunctions ...InitDBFunc) (err error) {
|
||||
for _, v := range InitDBFunctions {
|
||||
err = v.Init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitDbData() {
|
||||
err := initDB(initPeakValleyTimeBlockData)
|
||||
if err != nil {
|
||||
fmt.Println("[-]初始化基础数据失败:", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
@@ -1,56 +0,0 @@
|
||||
package init_db_data
|
||||
|
||||
import (
|
||||
"energy-management-system/global"
|
||||
peak_valley_model "energy-management-system/model/peak-valley"
|
||||
"github.com/gookit/color"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type InitPeakValleyTimeBlockData struct{}
|
||||
|
||||
var initPeakValleyTimeBlockData = new(InitPeakValleyTimeBlockData)
|
||||
|
||||
// type api struct{}
|
||||
func buildData() (dataList []*peak_valley_model.PeakValleyTimeBlock) {
|
||||
// 一天有24小时,即1440分钟
|
||||
totalMinutes := 24 * 60
|
||||
// 时间块的持续时间:10分钟
|
||||
blockDuration := 10
|
||||
// 遍历一天中的所有时间块
|
||||
for i := 0; i < totalMinutes; i += blockDuration {
|
||||
startTime := i
|
||||
endTime := i + blockDuration
|
||||
dataList = append(dataList, &peak_valley_model.PeakValleyTimeBlock{
|
||||
StartTime: uint(startTime),
|
||||
EndTime: uint(endTime),
|
||||
})
|
||||
}
|
||||
return dataList
|
||||
}
|
||||
|
||||
// Init 初始化用户数据
|
||||
func (i *InitPeakValleyTimeBlockData) Init() error {
|
||||
return global.Db.Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
m := &peak_valley_model.PeakValleyTimeBlock{}
|
||||
var count int64
|
||||
|
||||
err := tx.Model(&peak_valley_model.PeakValleyTimeBlock{}).Count(&count).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if count == 144 {
|
||||
color.Danger.Println("\n[PGSQL] --> " + m.TableName() + " 表的初始数据已存在!")
|
||||
return nil
|
||||
}
|
||||
|
||||
dataList := buildData()
|
||||
if err := tx.Create(&dataList).Error; err != nil { // 遇到错误时回滚事务
|
||||
return err
|
||||
}
|
||||
color.Info.Println("\n[PGSQL] --> " + m.TableName() + "表初始数据成功!")
|
||||
return nil
|
||||
})
|
||||
}
|
||||
91
core/logger/logger.go
Normal file
91
core/logger/logger.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"energy-management-system/global"
|
||||
"fmt"
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
"github.com/sirupsen/logrus"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InitLogger() *global.Logger {
|
||||
L := logrus.New()
|
||||
path := "./log/log"
|
||||
file, _ := rotatelogs.New(
|
||||
path+".%Y%m%d",
|
||||
rotatelogs.WithLinkName(path),
|
||||
rotatelogs.WithMaxAge(time.Duration(100*24)*time.Hour), //自动删除
|
||||
rotatelogs.WithRotationTime(time.Duration(24*60)*time.Minute), //分割时间
|
||||
)
|
||||
L.SetOutput(file)
|
||||
L.SetFormatter(&logrus.JSONFormatter{})
|
||||
Log := &global.Logger{Logger: L, Heads: []string{}}
|
||||
global.Log = Log
|
||||
return Log
|
||||
}
|
||||
|
||||
var errs = &errFunc{lock: new(sync.Mutex)}
|
||||
|
||||
func ErrorRegister(f func(string)) {
|
||||
errs.register(f, false)
|
||||
}
|
||||
func ErrorRegisterOnly1(f func(string)) {
|
||||
errs.register(f, true)
|
||||
}
|
||||
func ErrorWx(e ...string) {
|
||||
errs.run(strings.Join(e, " "))
|
||||
global.Log.Error(e)
|
||||
}
|
||||
|
||||
type errFunc struct {
|
||||
f []func(string)
|
||||
lastTime int64
|
||||
lock *sync.Mutex
|
||||
}
|
||||
|
||||
func (e *errFunc) register(f func(string), only bool) {
|
||||
if only {
|
||||
e.f = []func(string){f}
|
||||
} else {
|
||||
e.f = append(e.f, f)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *errFunc) run(logs string) {
|
||||
if len(e.f) == 0 {
|
||||
return
|
||||
}
|
||||
if time.Now().Unix()-e.lastTime > 1 {
|
||||
e.lock.Lock()
|
||||
e.lastTime = time.Now().Unix()
|
||||
defer e.lock.Unlock()
|
||||
for _, v := range e.f {
|
||||
v(logs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func StackSend(skip int, e string) {
|
||||
e += "\n" + Stack(skip)
|
||||
if global.AppConf.IsDebug {
|
||||
fmt.Print(e)
|
||||
}
|
||||
errs.run(e)
|
||||
}
|
||||
|
||||
func Stack(skip int) (re string) {
|
||||
for i := skip; ; i++ {
|
||||
_, file, line, ok := runtime.Caller(i)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if !strings.Contains(file, "local/go/src") && !strings.Contains(file, "/go/pkg") {
|
||||
logs := fmt.Sprintf("%s:%d\n", file, line)
|
||||
re += logs
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
package core
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package core
|
||||
package viper
|
||||
|
||||
import (
|
||||
"energy-management-system/config"
|
||||
Reference in New Issue
Block a user