支持多开实例隔离并默认打开小店
This commit is contained in:
+32
-5
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user