优化结构
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()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"kra/internal/config"
|
||||
)
|
||||
|
||||
type initializationRepoStub struct {
|
||||
initializeErr error
|
||||
reloadErr error
|
||||
}
|
||||
|
||||
func (*initializationRepoStub) IsInitialized(context.Context) (bool, error) { return false, nil }
|
||||
func (r *initializationRepoStub) Initialize(context.Context, *DatabaseConfig) error {
|
||||
return r.initializeErr
|
||||
}
|
||||
func (*initializationRepoStub) PersistConfig(context.Context) error { return nil }
|
||||
func (*initializationRepoStub) PersistRuntimeConfig(context.Context, *config.Config) error {
|
||||
return nil
|
||||
}
|
||||
func (r *initializationRepoStub) ReloadConfig(context.Context) error { return r.reloadErr }
|
||||
func (*initializationRepoStub) ConfigurationJSON() (json.RawMessage, error) {
|
||||
return json.RawMessage(`{}`), nil
|
||||
}
|
||||
func (*initializationRepoStub) SaveConfigurationJSON(context.Context, json.RawMessage) error {
|
||||
return nil
|
||||
}
|
||||
func (*initializationRepoStub) DiskMountPoints() []string { return nil }
|
||||
|
||||
type taskReloaderStub struct{ err error }
|
||||
|
||||
func (r taskReloaderStub) Reload(context.Context) error { return r.err }
|
||||
|
||||
func TestInitializeClassifiesPostCommitTaskReloadFailure(t *testing.T) {
|
||||
reloadErr := errors.New("scheduler unavailable")
|
||||
uc := NewSystemConfigUsecase(&initializationRepoStub{}, taskReloaderStub{err: reloadErr})
|
||||
err := uc.Initialize(context.Background(), &DatabaseConfig{})
|
||||
if !errors.Is(err, ErrTaskRuntimeReload) || !errors.Is(err, reloadErr) {
|
||||
t.Fatalf("Initialize() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReloadConfigClassifiesMissingTaskRuntime(t *testing.T) {
|
||||
uc := NewSystemConfigUsecase(&initializationRepoStub{}, nil)
|
||||
err := uc.ReloadConfig(context.Background())
|
||||
if !errors.Is(err, ErrTaskRuntimeReload) {
|
||||
t.Fatalf("ReloadConfig() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemConfigUsecaseRejectsMissingRepository(t *testing.T) {
|
||||
uc := NewSystemConfigUsecase(nil, nil)
|
||||
if _, err := uc.IsInitialized(context.Background()); err == nil {
|
||||
t.Fatal("IsInitialized accepted a missing repository")
|
||||
}
|
||||
if _, err := uc.ConfigurationJSON(); err == nil {
|
||||
t.Fatal("ConfigurationJSON accepted a missing repository")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user