Skip to content

Commit f995c48

Browse files
committed
fix(upload): 初始化分片上传管理器并优化清理逻辑
1 parent 9c8f70f commit f995c48

File tree

2 files changed

+79
-20
lines changed

2 files changed

+79
-20
lines changed

cmd/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ the address is defined in config file`,
4444
bootstrap.InitOfflineDownloadTools()
4545
bootstrap.LoadStorages()
4646
bootstrap.InitTaskManager()
47+
fs.InitSliceUploadManager()
4748
if !flags.Debug && !flags.Dev {
4849
gin.SetMode(gin.ReleaseMode)
4950
}

internal/fs/sliceup.go

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ func (s *SliceUploadSession) cleanup() {
434434
var globalSliceManager *SliceUploadManager
435435
var globalSliceManagerOnce sync.Once
436436

437+
func InitSliceUploadManager() {
438+
log.Info("Initializing slice upload manager...")
439+
getGlobalSliceManager()
440+
}
441+
437442
// getGlobalSliceManager 获取全局分片上传管理器(延迟初始化)
438443
func getGlobalSliceManager() *SliceUploadManager {
439444
globalSliceManagerOnce.Do(func() {
@@ -466,36 +471,33 @@ func (m *SliceUploadManager) cleanupIncompleteUploads() {
466471
}
467472
}()
468473

469-
// 等待一段时间,确保系统完全启动
470474
time.Sleep(10 * time.Second)
471475

472476
log.Info("Starting cleanup of incomplete slice uploads after restart...")
473477

474-
// 查询所有未完成的上传任务
475478
incompleteUploads, err := db.GetIncompleteSliceUploads()
476479
if err != nil {
477480
log.Errorf("Failed to get incomplete slice uploads: %v", err)
478-
return
479-
}
480-
481-
if len(incompleteUploads) == 0 {
482-
log.Info("No incomplete slice uploads found")
483-
return
484-
}
485-
486-
log.Infof("Found %d incomplete slice uploads, starting cleanup...", len(incompleteUploads))
487-
488-
cleanedCount := 0
489-
for _, upload := range incompleteUploads {
490-
if m.cleanupSingleUpload(upload) {
491-
cleanedCount++
481+
} else {
482+
if len(incompleteUploads) == 0 {
483+
log.Info("No incomplete slice uploads found in database")
484+
} else {
485+
log.Infof("Found %d incomplete slice uploads in database, starting cleanup...", len(incompleteUploads))
486+
cleanedCount := 0
487+
for _, upload := range incompleteUploads {
488+
if m.cleanupSingleUpload(upload) {
489+
cleanedCount++
490+
}
491+
}
492+
log.Infof("Database cleanup completed, cleaned up %d tasks", cleanedCount)
492493
}
493494
}
494495

495-
log.Infof("Slice upload cleanup completed, cleaned up %d tasks", cleanedCount)
496+
m.cleanupOrphanedTempFiles()
497+
498+
log.Info("Slice upload cleanup completed")
496499
}
497500

498-
// cleanupSingleUpload 清理单个上传任务
499501
func (m *SliceUploadManager) cleanupSingleUpload(upload *tables.SliceUpload) bool {
500502
defer func() {
501503
if r := recover(); r != nil {
@@ -505,7 +507,6 @@ func (m *SliceUploadManager) cleanupSingleUpload(upload *tables.SliceUpload) boo
505507

506508
log.Infof("Cleaning up upload task: %s, status: %s", upload.TaskID, upload.Status)
507509

508-
// 清理临时文件
509510
if upload.TmpFile != "" {
510511
if err := os.Remove(upload.TmpFile); err != nil && !os.IsNotExist(err) {
511512
log.Warnf("Failed to remove temp file %s for task %s: %v", upload.TmpFile, upload.TaskID, err)
@@ -514,7 +515,6 @@ func (m *SliceUploadManager) cleanupSingleUpload(upload *tables.SliceUpload) boo
514515
}
515516
}
516517

517-
// 从数据库中删除任务记录
518518
if err := db.DeleteSliceUploadByTaskID(upload.TaskID); err != nil {
519519
log.Errorf("Failed to delete slice upload task %s: %v", upload.TaskID, err)
520520
return false
@@ -523,3 +523,61 @@ func (m *SliceUploadManager) cleanupSingleUpload(upload *tables.SliceUpload) boo
523523
log.Infof("Successfully cleaned up task: %s", upload.TaskID)
524524
return true
525525
}
526+
527+
func (m *SliceUploadManager) cleanupOrphanedTempFiles() {
528+
defer func() {
529+
if r := recover(); r != nil {
530+
log.Errorf("Panic in cleanupOrphanedTempFiles: %v", r)
531+
}
532+
}()
533+
534+
tempDir := conf.GetPersistentTempDir()
535+
if tempDir == "" {
536+
log.Warn("Persistent temp directory not configured, skipping orphaned file cleanup")
537+
return
538+
}
539+
540+
log.Infof("Cleaning up orphaned temp files in: %s", tempDir)
541+
542+
entries, err := os.ReadDir(tempDir)
543+
if err != nil {
544+
log.Errorf("Failed to read temp directory %s: %v", tempDir, err)
545+
return
546+
}
547+
548+
orphanedCount := 0
549+
for _, entry := range entries {
550+
if entry.IsDir() {
551+
continue
552+
}
553+
554+
fileName := entry.Name()
555+
if !strings.HasPrefix(fileName, "slice_upload_") {
556+
continue
557+
}
558+
559+
filePath := filepath.Join(tempDir, fileName)
560+
fileInfo, err := entry.Info()
561+
if err != nil {
562+
log.Warnf("Failed to get file info for %s: %v", filePath, err)
563+
continue
564+
}
565+
566+
if time.Since(fileInfo.ModTime()) < 24*time.Hour {
567+
continue
568+
}
569+
570+
if err := os.Remove(filePath); err != nil {
571+
log.Warnf("Failed to remove orphaned temp file %s: %v", filePath, err)
572+
} else {
573+
log.Debugf("Removed orphaned temp file: %s", filePath)
574+
orphanedCount++
575+
}
576+
}
577+
578+
if orphanedCount > 0 {
579+
log.Infof("Cleaned up %d orphaned temp files", orphanedCount)
580+
} else {
581+
log.Info("No orphaned temp files found")
582+
}
583+
}

0 commit comments

Comments
 (0)