- 简化响应体关闭逻辑,移除不必要的nil检查 - 调整后台状态推送频率,从500ms改为250ms - 修复前端事件监听器注册顺序 - 移除未使用的进度变量 - 优化goroutine中的任务执行逻辑 - 改进文件路径显示,统一使用文件名而非完整路径 - 添加waitgroup等待确保资源正确释放
This commit is contained in:
@@ -32,21 +32,22 @@ func (a *App) startup(ctx context.Context) {
|
|||||||
go func() {
|
go func() {
|
||||||
for log := range a.logChan {
|
for log := range a.logChan {
|
||||||
runtime.EventsEmit(a.ctx, "log", log)
|
runtime.EventsEmit(a.ctx, "log", log)
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// 后台 goroutine 持续推送运行状态
|
// 后台 goroutine 持续推送运行状态
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(250 * time.Millisecond)
|
||||||
runtime.EventsEmit(a.ctx, "is-run", a.isRun)
|
runtime.EventsEmit(a.ctx, "is-run", a.isRun)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//在程序启动时运行上传程序
|
//在程序启动时运行上传程序
|
||||||
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
|
|
||||||
if config.APPConfig.IsRunOnStart {
|
if config.APPConfig.IsRunOnStart {
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
|
||||||
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir)
|
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir)
|
||||||
a.isRun = true
|
a.isRun = true
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-11
@@ -14,7 +14,6 @@ const concurrentFiles = ref(1)
|
|||||||
const uploadThreads = ref(1)
|
const uploadThreads = ref(1)
|
||||||
const autoStart = ref(false)
|
const autoStart = ref(false)
|
||||||
|
|
||||||
const progress = ref(0)
|
|
||||||
const isRunning = ref(false)
|
const isRunning = ref(false)
|
||||||
const logOutput = ref<string[]>([])
|
const logOutput = ref<string[]>([])
|
||||||
const logContentRef = ref<HTMLElement>()
|
const logContentRef = ref<HTMLElement>()
|
||||||
@@ -45,7 +44,7 @@ const sortedProgressList = computed(() => {
|
|||||||
const addLog = (msg: string) => {
|
const addLog = (msg: string) => {
|
||||||
logOutput.value.push(`[${new Date().toLocaleString()}]` + msg)
|
logOutput.value.push(`[${new Date().toLocaleString()}]` + msg)
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
if (logContentRef.value && logRoll.value){
|
if (logContentRef.value && logRoll.value) {
|
||||||
logContentRef.value.scrollTop = logContentRef.value.scrollHeight
|
logContentRef.value.scrollTop = logContentRef.value.scrollHeight
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -71,19 +70,15 @@ const startRun = () => {
|
|||||||
ElMessage.warning('请选择检测目录')
|
ElMessage.warning('请选择检测目录')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
isRunning.value = true
|
|
||||||
progress.value = 0
|
|
||||||
StartUpload()
|
StartUpload()
|
||||||
}
|
}
|
||||||
|
|
||||||
const stopRun = () => {
|
const stopRun = () => {
|
||||||
if (isRunning.value) {
|
|
||||||
isRunning.value = false
|
|
||||||
addLog(`正在停止运行`)
|
addLog(`正在停止运行`)
|
||||||
StopUpload().then(() => {
|
StopUpload().then(() => {
|
||||||
ElMessage.info('已停止运行')
|
ElMessage.info('已停止运行')
|
||||||
})
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const clearLog = () => {
|
const clearLog = () => {
|
||||||
@@ -133,14 +128,14 @@ watch(autoStart, () => {
|
|||||||
WriteConfig("is-run-on-start", autoStart.value)
|
WriteConfig("is-run-on-start", autoStart.value)
|
||||||
})
|
})
|
||||||
|
|
||||||
EventsOn("log", (msg) => {
|
EventsOn("is-run", (run) => {
|
||||||
addLog(msg)
|
isRunning.value = run
|
||||||
})
|
})
|
||||||
EventsOn("progress", (progress) => {
|
EventsOn("progress", (progress) => {
|
||||||
progressList.value = progress
|
progressList.value = progress
|
||||||
})
|
})
|
||||||
EventsOn("is-run", (run) => {
|
EventsOn("log", (msg) => {
|
||||||
isRunning.value = run
|
addLog(msg)
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -38,9 +38,8 @@ func UploadDataToServer(ctx context.Context, data string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if resp != nil {
|
io.Copy(io.Discard, resp.Body)
|
||||||
_, _ = io.Copy(io.Discard, resp.Body)
|
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func StartLooking(ctx context.Context, logChan *chan string, lookingPath string)
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(250 * time.Millisecond)
|
||||||
|
|
||||||
var pg []Progress
|
var pg []Progress
|
||||||
progress.Range(func(key, value any) bool {
|
progress.Range(func(key, value any) bool {
|
||||||
@@ -137,6 +137,10 @@ func uploadData(ctx context.Context, logChan *chan string, lookingPath string) {
|
|||||||
g.SetLimit(config.APPConfig.HandleFileCount) // 设置同时处理文件数
|
g.SetLimit(config.APPConfig.HandleFileCount) // 设置同时处理文件数
|
||||||
// 执行所有任务
|
// 执行所有任务
|
||||||
for _, task := range tasks {
|
for _, task := range tasks {
|
||||||
|
select {
|
||||||
|
case <-egctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
select {
|
select {
|
||||||
case <-egctx.Done():
|
case <-egctx.Done():
|
||||||
@@ -160,6 +164,7 @@ func uploadData(ctx context.Context, logChan *chan string, lookingPath string) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@@ -242,6 +247,7 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
|
|||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
close(lines)
|
close(lines)
|
||||||
|
wg.Wait()
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
progress.Store(filepath.Base(filePath),
|
progress.Store(filepath.Base(filePath),
|
||||||
@@ -261,7 +267,7 @@ func processFile(ctx context.Context, logChan *chan string, filePath string, fil
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
AddLog(logChan, fmt.Sprintf("文件【%s】处理完成,共处理 %d 行数据", filePath, countLine))
|
AddLog(logChan, fmt.Sprintf("文件【%s】处理完成,共处理 %d 行数据", filepath.Base(filePath), countLine))
|
||||||
}
|
}
|
||||||
|
|
||||||
func processLines(ctx context.Context, logChan *chan string, lines *chan string, workerID int, filePath string, countLine *int32) {
|
func processLines(ctx context.Context, logChan *chan string, lines *chan string, workerID int, filePath string, countLine *int32) {
|
||||||
@@ -276,7 +282,7 @@ func processLines(ctx context.Context, logChan *chan string, lines *chan string,
|
|||||||
}
|
}
|
||||||
// 上传数据
|
// 上传数据
|
||||||
if err := api.UploadDataToServer(ctx, line); err != nil {
|
if err := api.UploadDataToServer(ctx, line); err != nil {
|
||||||
AddLog(logChan, fmt.Sprintf("Worker %d (文件 %s): 上传失败: %v", workerID, filePath, err))
|
AddLog(logChan, fmt.Sprintf("Worker %d (文件 %s): 上传失败: %v", workerID, filepath.Base(filePath), err))
|
||||||
}
|
}
|
||||||
atomic.AddInt32(countLine, 1)
|
atomic.AddInt32(countLine, 1)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user