优化结构
This commit is contained in:
@@ -11,8 +11,19 @@ var ErrTaskRuntimeReload = errors.New("task runtime reload failed")
|
||||
|
||||
type TaskRuntimeReloadError struct{ Err error }
|
||||
|
||||
func (e *TaskRuntimeReloadError) Error() string { return e.Err.Error() }
|
||||
func (e *TaskRuntimeReloadError) Error() string {
|
||||
if e == nil || e.Err == nil {
|
||||
return ErrTaskRuntimeReload.Error()
|
||||
}
|
||||
return e.Err.Error()
|
||||
}
|
||||
func (e *TaskRuntimeReloadError) Is(target error) bool { return target == ErrTaskRuntimeReload }
|
||||
func (e *TaskRuntimeReloadError) Unwrap() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.Err
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Driver, Host, Port, User, Password, Name, Path, Config, Template, AdminPassword string
|
||||
@@ -46,30 +57,51 @@ func NewSystemConfigUsecase(repo InitializationRepo, tasks TaskReloader) *System
|
||||
}
|
||||
|
||||
func (uc *SystemConfigUsecase) IsInitialized(ctx context.Context) (bool, error) {
|
||||
if uc == nil || uc.repo == nil {
|
||||
return false, errors.New("系统初始化服务未就绪")
|
||||
}
|
||||
return uc.repo.IsInitialized(ctx)
|
||||
}
|
||||
func (uc *SystemConfigUsecase) Initialize(ctx context.Context, config *DatabaseConfig) error {
|
||||
if uc == nil || uc.repo == nil {
|
||||
return errors.New("系统初始化服务未就绪")
|
||||
}
|
||||
if config == nil {
|
||||
return errors.New("数据库初始化参数无效")
|
||||
}
|
||||
if err := uc.repo.Initialize(ctx, config); err != nil {
|
||||
return err
|
||||
}
|
||||
if uc.tasks == nil {
|
||||
return &TaskRuntimeReloadError{Err: errors.New("任务调度服务未就绪")}
|
||||
}
|
||||
if err := uc.tasks.Reload(ctx); err != nil {
|
||||
return &TaskRuntimeReloadError{Err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (uc *SystemConfigUsecase) PersistConfig(ctx context.Context) error {
|
||||
if uc == nil || uc.repo == nil {
|
||||
return errors.New("系统配置服务未就绪")
|
||||
}
|
||||
return uc.repo.PersistConfig(ctx)
|
||||
}
|
||||
func (uc *SystemConfigUsecase) PersistRuntimeConfig(ctx context.Context, value *config.Config) error {
|
||||
if uc == nil || uc.repo == nil {
|
||||
return errors.New("系统配置服务未就绪")
|
||||
}
|
||||
return uc.repo.PersistRuntimeConfig(ctx, value)
|
||||
}
|
||||
func (uc *SystemConfigUsecase) ReloadConfig(ctx context.Context) error {
|
||||
if uc == nil || uc.repo == nil {
|
||||
return errors.New("系统配置服务未就绪")
|
||||
}
|
||||
if err := uc.repo.ReloadConfig(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if uc.tasks == nil {
|
||||
return &TaskRuntimeReloadError{Err: errors.New("任务调度服务未就绪")}
|
||||
}
|
||||
if err := uc.tasks.Reload(ctx); err != nil {
|
||||
return &TaskRuntimeReloadError{Err: err}
|
||||
}
|
||||
@@ -77,11 +109,22 @@ func (uc *SystemConfigUsecase) ReloadConfig(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (uc *SystemConfigUsecase) ConfigurationJSON() (json.RawMessage, error) {
|
||||
if uc == nil || uc.repo == nil {
|
||||
return nil, errors.New("系统配置服务未就绪")
|
||||
}
|
||||
return uc.repo.ConfigurationJSON()
|
||||
}
|
||||
|
||||
func (uc *SystemConfigUsecase) SaveConfigurationJSON(ctx context.Context, value json.RawMessage) error {
|
||||
if uc == nil || uc.repo == nil {
|
||||
return errors.New("系统配置服务未就绪")
|
||||
}
|
||||
return uc.repo.SaveConfigurationJSON(ctx, value)
|
||||
}
|
||||
|
||||
func (uc *SystemConfigUsecase) DiskMountPoints() []string { return uc.repo.DiskMountPoints() }
|
||||
func (uc *SystemConfigUsecase) DiskMountPoints() []string {
|
||||
if uc == nil || uc.repo == nil {
|
||||
return nil
|
||||
}
|
||||
return uc.repo.DiskMountPoints()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user