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

66 lines
2.4 KiB
Go
Raw Normal View History

2023-12-27 22:59:20 +08:00
package application
import (
"context"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/model"
)
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"`
}
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
}
2024-01-23 16:29:41 +08:00
if err := app.scheduler.AddJob(context.Background(), false, entity.DbJobTypeRestore, 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, job *entity.DbRestore) error {
2024-01-23 16:29:41 +08:00
return app.scheduler.AddJob(ctx, true /* 保存到数据库 */, entity.DbJobTypeRestore, job)
2023-12-27 22:59:20 +08:00
}
func (app *DbRestoreApp) Update(ctx context.Context, job *entity.DbRestore) error {
2024-01-23 16:29:41 +08:00
return app.scheduler.UpdateJob(ctx, job)
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: 删除数据库恢复历史文件
2024-01-23 16:29:41 +08:00
return app.scheduler.RemoveJob(ctx, entity.DbJobTypeRestore, jobId)
2023-12-27 22:59:20 +08:00
}
func (app *DbRestoreApp) Enable(ctx context.Context, jobId uint64) error {
2024-01-23 16:29:41 +08:00
return app.scheduler.EnableJob(ctx, entity.DbJobTypeRestore, jobId)
2023-12-27 22:59:20 +08:00
}
func (app *DbRestoreApp) Disable(ctx context.Context, jobId uint64) error {
2024-01-23 16:29:41 +08:00
return app.scheduler.DisableJob(ctx, entity.DbJobTypeRestore, jobId)
2023-12-27 22:59:20 +08:00
}
// GetPageList 分页获取数据库恢复任务
func (app *DbRestoreApp) GetPageList(condition *entity.DbJobQuery, 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
}
// 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...)
}