feat: complete settings
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package cron_service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/unknwon/com"
|
||||
"message-nest/models"
|
||||
"message-nest/pkg/constant"
|
||||
"message-nest/pkg/logging"
|
||||
"message-nest/service/send_message_service"
|
||||
)
|
||||
|
||||
type CronService struct {
|
||||
}
|
||||
|
||||
var ClearLogsTaskId cron.EntryID
|
||||
|
||||
// 清除日志的定时任务
|
||||
func ClearLogs() {
|
||||
var logOutput []string
|
||||
var errStr string
|
||||
status := 1
|
||||
|
||||
sm := send_message_service.SendMessageService{TaskID: constant.CleanLogsTaskId}
|
||||
logging.Logger.Error("开始清除日志")
|
||||
logOutput = append(logOutput, "开始清除日志")
|
||||
|
||||
setting, err := models.GetSettingByKey(constant.LogsCleanSectionName, constant.LogsCleanKeepKeyName)
|
||||
if err != nil {
|
||||
errStr = fmt.Sprintf("获取日志的保留数失败,原因:%s", err)
|
||||
logging.Logger.Error(errStr)
|
||||
sm.MarkStatus(errStr, &status)
|
||||
logOutput = append(logOutput, errStr)
|
||||
}
|
||||
|
||||
keepNum := com.StrTo(setting.Value).MustInt()
|
||||
err = models.DeleteOutDateLogs(keepNum)
|
||||
if err != nil {
|
||||
errStr = fmt.Sprintf("删除日志失败,原因:%s", err)
|
||||
logging.Logger.Error(errStr)
|
||||
sm.MarkStatus(errStr, &status)
|
||||
logOutput = append(logOutput, errStr)
|
||||
} else {
|
||||
errStr = fmt.Sprintf("删除日志成功,保留数目:%d", keepNum)
|
||||
logging.Logger.Error(errStr)
|
||||
logOutput = append(logOutput, errStr)
|
||||
}
|
||||
sm.RecordSendLog(logOutput, status)
|
||||
}
|
||||
|
||||
// 启动注册清除任务定时任务
|
||||
func (cs *CronService) InitLogsCronRun() {
|
||||
setting, err := models.GetSettingByKey(constant.LogsCleanSectionName, constant.LogsCleanCronKeyName)
|
||||
if err != nil {
|
||||
logging.Logger.Error(fmt.Sprintf("获取日志的cron失败,原因:%s", err))
|
||||
}
|
||||
ClearLogsTaskId = AddTask(ScheduledTask{
|
||||
Schedule: setting.Value,
|
||||
Job: ClearLogs,
|
||||
})
|
||||
}
|
||||
|
||||
// 更新清除任务定时任务
|
||||
func (cs *CronService) UpdateLogsCronRun(cron string) {
|
||||
RemoveTask(ClearLogsTaskId)
|
||||
ClearLogsTaskId = AddTask(ScheduledTask{
|
||||
Schedule: cron,
|
||||
Job: ClearLogs,
|
||||
})
|
||||
logging.Logger.Error(fmt.Sprintf("更新日志的cron成功,%s", cron))
|
||||
logging.Logger.Info(fmt.Sprintf("所有的定时任务: %s", TaskList))
|
||||
|
||||
}
|
||||
|
||||
func Setup() {
|
||||
cs := CronService{}
|
||||
cs.InitLogsCronRun()
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package cron_service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/robfig/cron/v3"
|
||||
"message-nest/pkg/logging"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ScheduledTask struct {
|
||||
//ID int
|
||||
Schedule string
|
||||
Job func()
|
||||
}
|
||||
|
||||
var (
|
||||
CronInstance *cron.Cron
|
||||
TaskList map[cron.EntryID]*ScheduledTask
|
||||
mutex sync.Mutex
|
||||
)
|
||||
|
||||
func init() {
|
||||
CronInstance = cron.New()
|
||||
CronInstance.Start()
|
||||
TaskList = make(map[cron.EntryID]*ScheduledTask)
|
||||
}
|
||||
|
||||
// 添加定时任务
|
||||
func AddTask(task ScheduledTask) cron.EntryID {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
taskId, err := CronInstance.AddFunc(task.Schedule, task.Job)
|
||||
if err != nil {
|
||||
// 处理错误
|
||||
logging.Logger.Error(fmt.Sprintf("添加定时任务失败,原因:%s", err))
|
||||
} else {
|
||||
TaskList[taskId] = &task
|
||||
logging.Logger.Error(fmt.Sprintf("添加日志清除任务成功,entryID: %d", taskId))
|
||||
}
|
||||
return taskId
|
||||
}
|
||||
|
||||
// 删除定时任务
|
||||
func RemoveTask(taskID cron.EntryID) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
if _, ok := TaskList[taskID]; ok {
|
||||
CronInstance.Remove(taskID)
|
||||
delete(TaskList, taskID)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package env_service
|
||||
|
||||
import (
|
||||
"message-nest/models"
|
||||
"message-nest/pkg/constant"
|
||||
"message-nest/pkg/logging"
|
||||
)
|
||||
|
||||
type EnvService struct {
|
||||
}
|
||||
|
||||
func (es *EnvService) CommonAdd(section string, key string, value string) {
|
||||
setting, _ := models.GetSettingByKey(section, key)
|
||||
if setting.ID <= 0 {
|
||||
err := models.AddOneSetting(models.Settings{
|
||||
Section: section,
|
||||
Key: key,
|
||||
Value: value,
|
||||
})
|
||||
if err != nil {
|
||||
logging.Logger.Error("初始化%s:%s失败", section, key)
|
||||
} else {
|
||||
logging.Logger.Infof("初始化%s:%s成功", section, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// InitSiteConfig 初始化、重置站点信息设置
|
||||
func (es *EnvService) InitSiteConfig() {
|
||||
section := constant.SiteSettingSectionName
|
||||
for key, value := range constant.SiteSiteDefaultValueMap {
|
||||
es.CommonAdd(section, key, value)
|
||||
}
|
||||
}
|
||||
|
||||
// InitLogConfig 初始化日志清理设置
|
||||
func (es *EnvService) InitLogConfig() {
|
||||
section := constant.LogsCleanSectionName
|
||||
for key, value := range constant.LogsCleanDefaultValueMap {
|
||||
es.CommonAdd(section, key, value)
|
||||
}
|
||||
}
|
||||
|
||||
func Setup() {
|
||||
es := EnvService{}
|
||||
es.InitSiteConfig()
|
||||
es.InitLogConfig()
|
||||
}
|
||||
@@ -2,7 +2,12 @@ package settings_service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/robfig/cron/v3"
|
||||
"message-nest/models"
|
||||
"message-nest/pkg/app"
|
||||
"message-nest/pkg/constant"
|
||||
"message-nest/service/cron_service"
|
||||
)
|
||||
|
||||
type UserSettings struct {
|
||||
@@ -21,3 +26,88 @@ func (us *UserSettings) EditUserPasswd() error {
|
||||
user["password"] = us.NewPassword
|
||||
return models.EditUser(us.UserName, user)
|
||||
}
|
||||
|
||||
// GetUserSetting 获取用户设置
|
||||
func (us *UserSettings) GetUserSetting(section string) (map[string]string, error) {
|
||||
result := make(map[string]string)
|
||||
settings, err := models.GetSettingBySection(section)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
for _, setting := range settings {
|
||||
result[setting.Key] = setting.Value
|
||||
}
|
||||
// 版本信息单独获取
|
||||
if section == constant.AboutSectionName {
|
||||
result = constant.LatestVersion
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// EditSettings 便捷自定义设置
|
||||
func (us *UserSettings) EditSettings(section string, key string, value string, currentUser string) error {
|
||||
setting, _ := models.GetSettingByKey(section, key)
|
||||
if setting.ID <= 0 {
|
||||
err := models.AddOneSetting(models.Settings{
|
||||
IDModel: models.IDModel{
|
||||
CreatedBy: currentUser,
|
||||
ModifiedBy: currentUser,
|
||||
},
|
||||
Section: section,
|
||||
Key: key,
|
||||
Value: value,
|
||||
})
|
||||
return err
|
||||
} else {
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
data := make(map[string]interface{})
|
||||
data["section"] = section
|
||||
data["key"] = key
|
||||
data["value"] = value
|
||||
data["modified_by"] = currentUser
|
||||
err := models.EditSetting(setting.ID, data)
|
||||
if key == constant.LogsCleanCronKeyName {
|
||||
cronService := cron_service.CronService{}
|
||||
cronService.UpdateLogsCronRun(value)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 站点自定义的结构
|
||||
type SiteConfig struct {
|
||||
Title string `json:"title" validate:"omitempty,min=1,max=50" label:"网站标题"`
|
||||
Slogan string `json:"slogan" validate:"omitempty,min=1,max=50" label:"网站slogan"`
|
||||
Logo string `json:"logo" validate:"omitempty,min=1" label:"logo"`
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Cron string `json:"cron" validate:"required,cron" label:"日志定时表达式"`
|
||||
KeepNum string `json:"keep_num" validate:"required,min=1,max=50" label:"日志保留数"`
|
||||
}
|
||||
|
||||
// ValidateDiffSetting 校验不同的设置
|
||||
func (us *UserSettings) ValidateDiffSetting(section string, data map[string]string) string {
|
||||
if section == constant.SiteSettingSectionName {
|
||||
var config SiteConfig
|
||||
config.Title = data["title"]
|
||||
config.Slogan = data["slogan"]
|
||||
config.Logo = data["logo"]
|
||||
_, errStr := app.CommonPlaygroundValid(config)
|
||||
return errStr
|
||||
}
|
||||
if section == constant.LogsCleanSectionName {
|
||||
var config LogConfig
|
||||
config.Cron = data["cron"]
|
||||
config.KeepNum = data["keep_num"]
|
||||
_, err := cron.ParseStandard(config.Cron)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("%s 不是合法的corn表达式", config.Cron)
|
||||
}
|
||||
_, errStr := app.CommonPlaygroundValid(config)
|
||||
return errStr
|
||||
}
|
||||
return fmt.Sprintf("未知的section:%s", section)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user