refactor(app): 重构应用状态管理和配置常量定义
- 将全局变量 isRun 移动到 App 结构体内部作为实例字段 - 在 config.go 中定义配置键名为常量,提高代码可维护性 - 使用结构体实例字段替代全局变量管理上传状态 - 修改 StartLooking 函数中的上下文取消处理逻辑 - 移除上传程序退出日志的重复记录
This commit is contained in:
@@ -16,10 +16,9 @@ type App struct {
|
||||
logChan chan string
|
||||
uploaderCTX context.Context
|
||||
uploaderCancel context.CancelFunc
|
||||
isRun bool
|
||||
}
|
||||
|
||||
var isRun = false
|
||||
|
||||
func NewApp() *App {
|
||||
return &App{}
|
||||
}
|
||||
@@ -40,8 +39,8 @@ func (a *App) startup(ctx context.Context) {
|
||||
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
|
||||
if config.APPConfig.IsRunOnStart {
|
||||
time.Sleep(time.Second)
|
||||
isRun = true
|
||||
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir)
|
||||
a.isRun = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,15 +61,18 @@ func (a *App) WriteConfig(key string, value any) {
|
||||
}
|
||||
|
||||
func (a *App) StartUpload() {
|
||||
if isRun {
|
||||
if a.isRun {
|
||||
return
|
||||
}
|
||||
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
|
||||
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir)
|
||||
a.isRun = true
|
||||
}
|
||||
|
||||
func (a *App) StopUpload() {
|
||||
if isRun {
|
||||
if a.isRun {
|
||||
a.uploaderCancel()
|
||||
}
|
||||
a.isRun = false
|
||||
uploader.AddLog(&a.logChan, "上传程序已退出")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user