@@ -434,6 +434,11 @@ func (s *SliceUploadSession) cleanup() {
434
434
var globalSliceManager * SliceUploadManager
435
435
var globalSliceManagerOnce sync.Once
436
436
437
+ func InitSliceUploadManager () {
438
+ log .Info ("Initializing slice upload manager..." )
439
+ getGlobalSliceManager ()
440
+ }
441
+
437
442
// getGlobalSliceManager 获取全局分片上传管理器(延迟初始化)
438
443
func getGlobalSliceManager () * SliceUploadManager {
439
444
globalSliceManagerOnce .Do (func () {
@@ -466,36 +471,33 @@ func (m *SliceUploadManager) cleanupIncompleteUploads() {
466
471
}
467
472
}()
468
473
469
- // 等待一段时间,确保系统完全启动
470
474
time .Sleep (10 * time .Second )
471
475
472
476
log .Info ("Starting cleanup of incomplete slice uploads after restart..." )
473
477
474
- // 查询所有未完成的上传任务
475
478
incompleteUploads , err := db .GetIncompleteSliceUploads ()
476
479
if err != nil {
477
480
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 )
492
493
}
493
494
}
494
495
495
- log .Infof ("Slice upload cleanup completed, cleaned up %d tasks" , cleanedCount )
496
+ m .cleanupOrphanedTempFiles ()
497
+
498
+ log .Info ("Slice upload cleanup completed" )
496
499
}
497
500
498
- // cleanupSingleUpload 清理单个上传任务
499
501
func (m * SliceUploadManager ) cleanupSingleUpload (upload * tables.SliceUpload ) bool {
500
502
defer func () {
501
503
if r := recover (); r != nil {
@@ -505,7 +507,6 @@ func (m *SliceUploadManager) cleanupSingleUpload(upload *tables.SliceUpload) boo
505
507
506
508
log .Infof ("Cleaning up upload task: %s, status: %s" , upload .TaskID , upload .Status )
507
509
508
- // 清理临时文件
509
510
if upload .TmpFile != "" {
510
511
if err := os .Remove (upload .TmpFile ); err != nil && ! os .IsNotExist (err ) {
511
512
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
514
515
}
515
516
}
516
517
517
- // 从数据库中删除任务记录
518
518
if err := db .DeleteSliceUploadByTaskID (upload .TaskID ); err != nil {
519
519
log .Errorf ("Failed to delete slice upload task %s: %v" , upload .TaskID , err )
520
520
return false
@@ -523,3 +523,61 @@ func (m *SliceUploadManager) cleanupSingleUpload(upload *tables.SliceUpload) boo
523
523
log .Infof ("Successfully cleaned up task: %s" , upload .TaskID )
524
524
return true
525
525
}
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