refactor(core): align with cordis spatiotemporal composability architecture

- Purify core micro-kernel by removing context hardcoded helpers and reverse dependencies
- Eliminate init() side effects in infra plugins with reversible lifecycle disposal
- Completely isolate plugins by removing cross-plugin imports and using core/contracts
- Introduce TaskService and RiskControlService contracts for unified cross-plugin APIs
- Regenerate Swagger documentation and update developer guide matrix
- Achieve 0 violations in check_cordis_architecture.sh and 100% test pass
This commit is contained in:
ryan
2026-08-28 15:05:31 +08:00
parent fc7fae7b0e
commit 299ac30ee4
150 changed files with 4328 additions and 2923 deletions
@@ -12,8 +12,6 @@ import (
"strings"
"time"
database "Wavelet/plugins/infra/database"
"gorm.io/gorm"
)
@@ -110,12 +108,15 @@ func LoadConfig(ctx context.Context) (Config, error) {
func loadConfigByKey(ctx context.Context, key string, fallback Config) (Config, error) {
var val string
err := database.DB(ctx).Table("w_system_configs").Where("key = ?", key).Pluck("value", &val).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fallback, nil
db := getDB(ctx)
if db != nil {
err := db.Table("w_system_configs").Where("key = ?", key).Pluck("value", &val).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return fallback, nil
}
return Config{}, err
}
return Config{}, err
}
if strings.TrimSpace(val) == "" {
return fallback, nil
@@ -182,7 +183,7 @@ func SaveActiveConfig(ctx context.Context, cfg Config) error {
}
func saveSystemConfig(ctx context.Context, key string, value any, description string) error {
err := database.DB(ctx).Transaction(func(tx *gorm.DB) error {
err := getDB(ctx).Transaction(func(tx *gorm.DB) error {
return upsertSystemConfig(ctx, tx, key, value, description)
})
if err == nil && key == "storage_config" {
@@ -0,0 +1,62 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package objectstore
import (
"context"
"sync"
"gorm.io/gorm"
"Wavelet/core"
"Wavelet/core/contracts"
)
var (
dbMu sync.RWMutex
dbSvc contracts.DBService
cacheMu sync.RWMutex
cacheSvc contracts.CacheService
)
// SetDBService sets the DBService instance for objectstore.
func SetDBService(s contracts.DBService) {
dbMu.Lock()
defer dbMu.Unlock()
dbSvc = s
}
// SetCacheService sets the CacheService instance for objectstore.
func SetCacheService(s contracts.CacheService) {
cacheMu.Lock()
defer cacheMu.Unlock()
cacheSvc = s
}
func getDB(ctx context.Context) *gorm.DB {
if c, ok := ctx.(*core.Context); ok && c != nil {
if s, err := core.Inject[contracts.DBService](c); err == nil && s != nil {
return s.DB(ctx)
}
}
dbMu.RLock()
s := dbSvc
dbMu.RUnlock()
if s != nil {
return s.DB(ctx)
}
return nil
}
func getCache(ctx context.Context) contracts.CacheService {
if c, ok := ctx.(*core.Context); ok && c != nil {
if s, err := core.Inject[contracts.CacheService](c); err == nil && s != nil {
return s
}
}
cacheMu.RLock()
s := cacheSvc
cacheMu.RUnlock()
return s
}
@@ -13,10 +13,6 @@ import (
"sync"
"time"
cache "Wavelet/plugins/infra/cache"
database "Wavelet/plugins/infra/database"
"Wavelet/pkg/util"
"gorm.io/gorm"
)
@@ -28,6 +24,7 @@ const (
// Object describes a readable stored object.
type Object struct {
Key string
CachePath string
Body io.ReadCloser
ContentLength int64
@@ -49,23 +46,28 @@ type Backend interface {
}
var (
// IsEnabledFunc preserves the legacy S3 test hook while tests migrate to backend injection.
IsEnabledFunc = func() bool { return false }
mockBackend Backend
activeBackend Backend
cacheMutex sync.RWMutex
activeDriver Driver
activeBackend Backend
activeConfigJSON string
lastChecked time.Time
cacheMutex sync.RWMutex
pubSubOnce sync.Once
mockBackend Backend
// IsEnabledFunc controls whether mock/in-memory backend is activated in tests.
IsEnabledFunc = func() bool { return false }
)
// ConfigInvalidationChannel is the Redis pub/sub channel used to evict storage caches cluster-wide.
const ConfigInvalidationChannel = "storage:config_invalidation"
var pubSubOnce sync.Once
// SetMockBackend forces an in-memory/mock backend for testing.
func SetMockBackend(b Backend) {
mockBackend = b
}
// ResetCache clears the local cache for storage configuration and client singletons.
// ResetCache clears cached driver and backend instances.
func ResetCache() {
cacheMutex.Lock()
defer cacheMutex.Unlock()
@@ -77,28 +79,14 @@ func ResetCache() {
// PublishCacheInvalidation broadcasts cache eviction to all nodes in the cluster via Redis.
func PublishCacheInvalidation(ctx context.Context) {
if cache.Redis != nil {
_ = cache.Redis.Publish(ctx, ConfigInvalidationChannel, "reset").Err()
if cache := getCache(ctx); cache != nil {
_ = cache.Invalidate(ctx, ConfigInvalidationChannel)
}
ResetCache()
}
// startPubSubListener starts the background subscriber for cache invalidations.
func startPubSubListener() {
rdb := cache.Redis
if rdb == nil {
return
}
util.Go(func() {
pubsub := rdb.Subscribe(context.Background(), ConfigInvalidationChannel)
defer func() {
_ = pubsub.Close()
}()
ch := pubsub.Channel()
for range ch {
ResetCache()
}
})
}
// Active returns the configured active driver and backend, using an in-memory cache with 5s TTL.
@@ -127,9 +115,12 @@ func Active(ctx context.Context) (Driver, Backend, error) {
}
var val string
err := database.DB(ctx).Table("w_system_configs").Where("key = ?", "storage_config").Pluck("value", &val).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return "", nil, err
db := getDB(ctx)
if db != nil {
err := db.Table("w_system_configs").Where("key = ?", "storage_config").Pluck("value", &val).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return "", nil, err
}
}
sc := struct{ Value string }{Value: val}
@@ -11,9 +11,10 @@ import (
"testing"
"time"
cache "Wavelet/plugins/infra/cache"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
cache "Wavelet/plugins/infra/cache"
)
func TestStorageCache(t *testing.T) {
@@ -11,8 +11,9 @@ import (
"path"
"strings"
"Wavelet/pkg/httppool"
"github.com/studio-b12/gowebdav"
"Wavelet/pkg/httppool"
)
type contextTransport struct {