Files
mayfly-go/server/internal/db/application/db_restore.go

139 lines
3.9 KiB
Go
Raw Normal View History

2023-12-27 22:59:20 +08:00
package application
import (
"context"
"errors"
2023-12-27 22:59:20 +08:00
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/logx"
2023-12-27 22:59:20 +08:00
"mayfly-go/pkg/model"
"sync"
2023-12-27 22:59:20 +08:00
)
type DbRestoreApp struct {
2024-01-23 16:29:41 +08:00
scheduler *dbScheduler `inject:"DbScheduler"`
restoreRepo repository.DbRestore `inject:"DbRestoreRepo"`
restoreHistoryRepo repository.DbRestoreHistory `inject:"DbRestoreHistoryRepo"`
mutex sync.Mutex
}
func (app *DbRestoreApp) Init() error {
var jobs []*entity.DbRestore
2024-01-23 16:29:41 +08:00
if err := app.restoreRepo.ListToDo(&jobs); err != nil {
return err
}
if err := app.scheduler.AddJob(context.Background(), jobs); err != nil {
return err
}
return nil
}
func (app *DbRestoreApp) Close() {
2024-01-23 16:29:41 +08:00
app.scheduler.Close()
2023-12-27 22:59:20 +08:00
}
func (app *DbRestoreApp) Create(ctx context.Context, jobs any) error {
app.mutex.Lock()
defer app.mutex.Unlock()
if err := app.restoreRepo.AddJob(ctx, jobs); err != nil {
return err
}
_ = app.scheduler.AddJob(ctx, jobs)
return nil
2023-12-27 22:59:20 +08:00
}
func (app *DbRestoreApp) Update(ctx context.Context, job *entity.DbRestore) error {
app.mutex.Lock()
defer app.mutex.Unlock()
if err := app.restoreRepo.UpdateById(ctx, job); err != nil {
return err
}
_ = app.scheduler.UpdateJob(ctx, job)
return nil
2023-12-27 22:59:20 +08:00
}
func (app *DbRestoreApp) Delete(ctx context.Context, jobId uint64) error {
2023-12-27 22:59:20 +08:00
// todo: 删除数据库恢复历史文件
app.mutex.Lock()
defer app.mutex.Unlock()
if err := app.scheduler.RemoveJob(ctx, entity.DbJobTypeRestore, jobId); err != nil {
return err
}
history := &entity.DbRestoreHistory{
DbRestoreId: jobId,
}
if err := app.restoreHistoryRepo.DeleteByCond(ctx, history); err != nil {
return err
}
if err := app.restoreRepo.DeleteById(ctx, jobId); err != nil {
return err
}
return nil
2023-12-27 22:59:20 +08:00
}
func (app *DbRestoreApp) Enable(ctx context.Context, jobId uint64) error {
app.mutex.Lock()
defer app.mutex.Unlock()
repo := app.restoreRepo
job := &entity.DbRestore{}
if err := repo.GetById(job, jobId); err != nil {
return err
}
if job.IsEnabled() {
return nil
}
if job.IsExpired() {
return errors.New("任务已过期")
}
_ = app.scheduler.EnableJob(ctx, job)
if err := repo.UpdateEnabled(ctx, jobId, true); err != nil {
logx.Errorf("数据库恢复任务已启用( jobId: %d ),任务状态保存失败: %v", jobId, err)
return err
}
return nil
2023-12-27 22:59:20 +08:00
}
func (app *DbRestoreApp) Disable(ctx context.Context, jobId uint64) error {
app.mutex.Lock()
defer app.mutex.Unlock()
repo := app.restoreRepo
job := &entity.DbRestore{}
if err := repo.GetById(job, jobId); err != nil {
return err
}
if !job.IsEnabled() {
return nil
}
_ = app.scheduler.DisableJob(ctx, entity.DbJobTypeRestore, jobId)
if err := repo.UpdateEnabled(ctx, jobId, false); err != nil {
logx.Errorf("数据库恢复任务已禁用( jobId: %d ),任务状态保存失败: %v", jobId, err)
return err
}
return nil
2023-12-27 22:59:20 +08:00
}
// GetPageList 分页获取数据库恢复任务
func (app *DbRestoreApp) GetPageList(condition *entity.DbRestoreQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
2024-01-23 16:29:41 +08:00
return app.restoreRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
2023-12-27 22:59:20 +08:00
}
// GetRestoresEnabled 获取数据库恢复任务
func (app *DbRestoreApp) GetRestoresEnabled(toEntity any, backupHistoryId ...uint64) error {
return app.restoreRepo.GetEnabledRestores(toEntity, backupHistoryId...)
}
2023-12-27 22:59:20 +08:00
// GetDbNamesWithoutRestore 获取未配置定时恢复的数据库名称
func (app *DbRestoreApp) GetDbNamesWithoutRestore(instanceId uint64, dbNames []string) ([]string, error) {
2024-01-23 16:29:41 +08:00
return app.restoreRepo.GetDbNamesWithoutRestore(instanceId, dbNames)
}
// GetHistoryPageList 分页获取数据库备份历史
func (app *DbRestoreApp) GetHistoryPageList(condition *entity.DbRestoreHistoryQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
2024-01-23 16:29:41 +08:00
return app.restoreHistoryRepo.GetDbRestoreHistories(condition, pageParam, toEntity, orderBy...)
}