Files
mayfly-go/server/internal/db/domain/entity/db_task_base.go
kanzihuang ae3d2659aa 重构数据库备份与恢复模块 (#80)
* fix: 保存 LastResult 时截断字符串过长部分,以避免数据库报错

* refactor: 新增 entity.DbTaskBase 和 persistence.dbTaskBase, 用于实现数据库备份和恢复任务处理相关部分

* fix: aeskey变更后,解密密码出现数组越界访问错误

* fix: 时间属性为零值时,保存到 mysql 数据库报错

* refactor db.infrastructure.service.scheduler

* feat: 实现立即备份功能

* refactor db.infrastructure.service.db_instance

* refactor: 从数据库中获取数据库备份目录、mysql文件路径等配置信息

* fix: 数据库备份和恢复问题

* fix: 修改 .gitignore 文件,忽略数据库备份目录和数据库程序目录
2024-01-05 08:55:34 +08:00

110 lines
2.1 KiB
Go

package entity
import (
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils/timex"
"time"
)
type TaskStatus int
const (
TaskDelay TaskStatus = iota
TaskReady
TaskReserved
TaskSuccess
TaskFailed
)
const LastResultSize = 256
type DbTask interface {
model.ModelI
GetId() uint64
GetDeadline() time.Time
IsFinished() bool
Schedule() bool
Update(task DbTask)
GetTaskBase() *DbTaskBase
MessageWithStatus(status TaskStatus) string
IsEnabled() bool
}
func NewDbBTaskBase(enabled bool, repeated bool, startTime time.Time, interval time.Duration) *DbTaskBase {
return &DbTaskBase{
Enabled: enabled,
Repeated: repeated,
StartTime: startTime,
Interval: interval,
}
}
type DbTaskBase struct {
model.Model
Enabled bool // 是否启用
StartTime time.Time // 开始时间
Interval time.Duration // 间隔时间
Repeated bool // 是否重复执行
LastStatus TaskStatus // 最近一次执行状态
LastResult string // 最近一次执行结果
LastTime timex.NullTime // 最近一次执行时间
Deadline time.Time `gorm:"-" json:"-"` // 计划执行时间
}
func (d *DbTaskBase) GetId() uint64 {
if d == nil {
return 0
}
return d.Id
}
func (d *DbTaskBase) GetDeadline() time.Time {
return d.Deadline
}
func (d *DbTaskBase) Schedule() bool {
if d.IsFinished() || !d.Enabled {
return false
}
switch d.LastStatus {
case TaskSuccess:
if d.Interval == 0 {
return false
}
lastTime := d.LastTime.Time
if lastTime.Sub(d.StartTime) < 0 {
lastTime = d.StartTime.Add(-d.Interval)
}
d.Deadline = lastTime.Add(d.Interval - lastTime.Sub(d.StartTime)%d.Interval)
case TaskFailed:
d.Deadline = time.Now().Add(time.Minute)
default:
d.Deadline = d.StartTime
}
return true
}
func (d *DbTaskBase) IsFinished() bool {
return !d.Repeated && d.LastStatus == TaskSuccess
}
func (d *DbTaskBase) Update(task DbTask) {
t := task.GetTaskBase()
d.StartTime = t.StartTime
d.Interval = t.Interval
}
func (d *DbTaskBase) GetTaskBase() *DbTaskBase {
return d
}
func (*DbTaskBase) MessageWithStatus(_ TaskStatus) string {
return ""
}
func (d *DbTaskBase) IsEnabled() bool {
return d.Enabled
}