支持多开实例隔离并默认打开小店

This commit is contained in:
yml2213
2026-08-09 17:25:18 +08:00
parent 6c881e9182
commit a9ad969b64
16 changed files with 385 additions and 27 deletions
+32 -5
View File
@@ -30,10 +30,12 @@ type App struct {
scheduler *seckill.Scheduler
logStore *logger.MemoryLogStore
traceMu sync.RWMutex
trace *trace.Recorder
paths types.AppPaths
font *truetype.Font
traceMu sync.RWMutex
trace *trace.Recorder
paths types.AppPaths
font *truetype.Font
instanceLease *config.InstanceLease
instanceName string
cfgMu sync.RWMutex
cfg types.Config
@@ -122,10 +124,23 @@ func New() *App {
func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
a.paths = config.DefaultAppPaths()
lease, instanceErr := config.AcquireInstancePaths()
if instanceErr != nil {
a.paths = config.DefaultAppPaths()
a.instanceName = "默认实例"
} else {
a.paths = lease.Paths()
a.instanceLease = lease
a.instanceName = lease.Name()
}
configMigrated, configMigrationErr := config.MigrateLegacyFile(a.paths.ConfigFile, a.paths.LegacyConfigFile)
a.logStore = logger.NewMemoryLogStore(800)
a.closeLog = logger.InitLogging(a.logStore, a.paths.LogDir)
if instanceErr != nil {
logger.LogEvent("系统", "warn", "实例隔离初始化失败,已回退默认目录: %v", instanceErr)
} else {
logger.LogEvent("系统", "success", "已启动%s,数据目录: %s", a.instanceName, a.paths.DataDir)
}
if configMigrationErr != nil {
logger.LogEvent("配置", "warn", "旧配置迁移失败: %s: %v", a.paths.LegacyConfigFile, configMigrationErr)
} else if configMigrated {
@@ -182,6 +197,10 @@ func (a *App) Shutdown(ctx context.Context) {
if a.closeLog != nil {
a.closeLog()
}
if a.instanceLease != nil {
a.instanceLease.Release()
a.instanceLease = nil
}
}
// GetVersion 返回应用版本号。
@@ -191,3 +210,11 @@ func (a *App) GetVersion() string {
}
return constants.Version
}
// GetInstanceName 返回当前自动分配的本地实例名称。
func (a *App) GetInstanceName() string {
if a.instanceName == "" {
return "默认实例"
}
return a.instanceName
}