Skip to content

Commit 05882df

Browse files
committed
统一持久化临时目录
1 parent 24411ec commit 05882df

File tree

9 files changed

+122
-166
lines changed

9 files changed

+122
-166
lines changed

internal/bootstrap/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,14 @@ func InitConfig() {
125125
if err != nil {
126126
log.Fatalf("create temp dir error: %+v", err)
127127
}
128+
err = os.MkdirAll(conf.GetPersistentTempDir(), 0o777)
129+
if err != nil {
130+
log.Fatalf("create persistent temp dir error: %+v", err)
131+
}
128132
log.Debugf("config: %+v", conf.Conf)
129133
base.InitClient()
130134
initURL()
135+
CleanTempDir()
131136
}
132137

133138
func confFromEnv() {
@@ -160,6 +165,9 @@ func CleanTempDir() {
160165
log.Errorln("failed list temp file: ", err)
161166
}
162167
for _, file := range files {
168+
if file.Name() == "persistent" {
169+
continue
170+
}
163171
if err := os.RemoveAll(filepath.Join(conf.Conf.TempDir, file.Name())); err != nil {
164172
log.Errorln("failed delete temp file: ", err)
165173
}

internal/bootstrap/task.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ func InitTaskManager() {
3838
op.RegisterSettingChangingCallback(func() {
3939
tool.TransferTaskManager.SetWorkersNumActive(taskFilterNegative(setting.GetInt(conf.TaskOfflineDownloadTransferThreadsNum, conf.Conf.Tasks.Transfer.Workers)))
4040
})
41-
if len(tool.TransferTaskManager.GetAll()) == 0 { //prevent offline downloaded files from being deleted
42-
CleanTempDir()
43-
}
4441
fs.ArchiveDownloadTaskManager = tache.NewManager[*fs.ArchiveDownloadTask](tache.WithWorks(setting.GetInt(conf.TaskDecompressDownloadThreadsNum, conf.Conf.Tasks.Decompress.Workers)), tache.WithPersistFunction(db.GetTaskDataFunc("decompress", conf.Conf.Tasks.Decompress.TaskPersistant), db.UpdateTaskDataFunc("decompress", conf.Conf.Tasks.Decompress.TaskPersistant)), tache.WithMaxRetry(conf.Conf.Tasks.Decompress.MaxRetry))
4542
op.RegisterSettingChangingCallback(func() {
4643
fs.ArchiveDownloadTaskManager.SetWorkersNumActive(taskFilterNegative(setting.GetInt(conf.TaskDecompressDownloadThreadsNum, conf.Conf.Tasks.Decompress.Workers)))

internal/conf/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,7 @@ func DefaultConfig(dataDir string) *Config {
245245
LastLaunchedVersion: "",
246246
}
247247
}
248+
249+
func GetPersistentTempDir() string {
250+
return filepath.Join(Conf.TempDir, "persistent")
251+
}

internal/db/slice_upload.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"path/filepath"
77
"strings"
88
"time"
9-
9+
10+
"github.com/OpenListTeam/OpenList/v4/internal/conf"
1011
"github.com/OpenListTeam/OpenList/v4/internal/model/tables"
11-
"github.com/OpenListTeam/OpenList/v4/pkg/tempdir"
1212
"github.com/pkg/errors"
13-
"gorm.io/gorm"
1413
log "github.com/sirupsen/logrus"
14+
"gorm.io/gorm"
1515
)
1616

1717
func CreateSliceUpload(su *tables.SliceUpload) error {
@@ -52,11 +52,11 @@ func GetIncompleteSliceUploads() ([]*tables.SliceUpload, error) {
5252
tables.SliceUploadStatusProxyComplete,
5353
tables.SliceUploadStatusPendingComplete,
5454
}).Find(&uploads).Error
55-
55+
5656
if err != nil {
5757
return nil, errors.WithStack(err)
5858
}
59-
59+
6060
return uploads, nil
6161
}
6262

@@ -75,10 +75,10 @@ func UpdateSliceStatusAtomic(taskID string, sliceNum int, status []byte) error {
7575
if err := tx.Where("task_id = ?", taskID).First(&su).Error; err != nil {
7676
return err
7777
}
78-
78+
7979
// 更新分片状态
8080
tables.SetSliceUploaded(su.SliceUploadStatus, sliceNum)
81-
81+
8282
// 保存更新
8383
return tx.Save(&su).Error
8484
}))
@@ -88,15 +88,15 @@ func UpdateSliceStatusAtomic(taskID string, sliceNum int, status []byte) error {
8888
func CleanupOrphanedSliceUploads() error {
8989
// 清理超过24小时的未完成任务
9090
cutoff := time.Now().Add(-24 * time.Hour)
91-
91+
9292
var orphanedTasks []tables.SliceUpload
93-
if err := db.Where("status IN (?, ?) AND updated_at < ?",
94-
tables.SliceUploadStatusWaiting,
95-
tables.SliceUploadStatusUploading,
93+
if err := db.Where("status IN (?, ?) AND updated_at < ?",
94+
tables.SliceUploadStatusWaiting,
95+
tables.SliceUploadStatusUploading,
9696
cutoff).Find(&orphanedTasks).Error; err != nil {
9797
return errors.WithStack(err)
9898
}
99-
99+
100100
cleanedCount := 0
101101
for _, task := range orphanedTasks {
102102
// 清理临时文件
@@ -107,106 +107,106 @@ func CleanupOrphanedSliceUploads() error {
107107
log.Debugf("Removed orphaned tmp file: %s", task.TmpFile)
108108
}
109109
}
110-
110+
111111
// 删除数据库记录
112112
if err := db.Delete(&task).Error; err != nil {
113113
log.Errorf("Failed to delete orphaned slice upload task %s: %v", task.TaskID, err)
114114
} else {
115115
cleanedCount++
116116
}
117117
}
118-
118+
119119
if cleanedCount > 0 {
120120
log.Infof("Cleaned up %d orphaned slice upload tasks", cleanedCount)
121121
}
122-
122+
123123
// 额外清理:扫描临时目录中的孤儿文件
124124
return cleanupOrphanedTempFiles()
125125
}
126126

127127
// cleanupOrphanedTempFiles 清理临时目录中的孤儿文件
128128
func cleanupOrphanedTempFiles() error {
129129
// 获取临时目录路径,使用共享的tempdir包
130-
tempDir := tempdir.GetPersistentTempDir()
131-
130+
tempDir := conf.GetPersistentTempDir()
131+
132132
// 检查临时目录是否存在
133133
if _, err := os.Stat(tempDir); os.IsNotExist(err) {
134134
log.Debugf("Temp directory does not exist: %s", tempDir)
135135
return nil
136136
}
137-
137+
138138
// 获取所有活跃的分片上传任务的临时文件列表
139139
var activeTasks []tables.SliceUpload
140-
if err := db.Where("tmp_file IS NOT NULL AND tmp_file != '' AND status IN (?, ?)",
141-
tables.SliceUploadStatusWaiting,
140+
if err := db.Where("tmp_file IS NOT NULL AND tmp_file != '' AND status IN (?, ?)",
141+
tables.SliceUploadStatusWaiting,
142142
tables.SliceUploadStatusUploading).Find(&activeTasks).Error; err != nil {
143143
return errors.WithStack(err)
144144
}
145-
145+
146146
// 构建活跃文件的映射表
147147
activeFiles := make(map[string]bool)
148148
for _, task := range activeTasks {
149149
if task.TmpFile != "" {
150150
activeFiles[task.TmpFile] = true
151151
}
152152
}
153-
153+
154154
cleanedCount := 0
155155
cutoff := time.Now().Add(-24 * time.Hour) // 只清理超过24小时的文件
156-
156+
157157
// 遍历临时目录
158158
err := filepath.WalkDir(tempDir, func(path string, d fs.DirEntry, err error) error {
159159
if err != nil {
160160
log.Warnf("Failed to access path %s: %v", path, err)
161161
return nil // 继续处理其他文件
162162
}
163-
163+
164164
// 跳过目录
165165
if d.IsDir() {
166166
return nil
167167
}
168-
168+
169169
// 只处理分片上传临时文件(以slice_upload_开头)
170170
if !strings.HasPrefix(d.Name(), "slice_upload_") {
171171
return nil
172172
}
173-
173+
174174
// 检查文件是否在活跃任务列表中
175175
if activeFiles[path] {
176176
return nil // 文件仍在使用中,跳过
177177
}
178-
178+
179179
// 检查文件修改时间
180180
info, err := d.Info()
181181
if err != nil {
182182
log.Warnf("Failed to get file info for %s: %v", path, err)
183183
return nil
184184
}
185-
185+
186186
// 只清理超过24小时的文件
187187
if info.ModTime().After(cutoff) {
188188
return nil
189189
}
190-
190+
191191
// 删除孤儿文件
192192
if err := os.Remove(path); err != nil {
193193
log.Warnf("Failed to remove orphaned temp file %s: %v", path, err)
194194
} else {
195195
log.Debugf("Removed orphaned temp file: %s", path)
196196
cleanedCount++
197197
}
198-
198+
199199
return nil
200200
})
201-
201+
202202
if err != nil {
203203
log.Errorf("Failed to walk temp directory %s: %v", tempDir, err)
204204
return errors.WithStack(err)
205205
}
206-
206+
207207
if cleanedCount > 0 {
208208
log.Infof("Cleaned up %d orphaned temp files from %s", cleanedCount, tempDir)
209209
}
210-
210+
211211
return nil
212212
}

0 commit comments

Comments
 (0)