Skip to content

Commit 45cbc76

Browse files
committed
periodically update folder status in pgsql
1 parent 53dd5a7 commit 45cbc76

File tree

3 files changed

+107
-9
lines changed

3 files changed

+107
-9
lines changed

pkg/background_task/background_task.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,12 @@ func InitBackgroundTaskManager(ctx context.Context) {
7979
interval: 0,
8080
})
8181

82+
manager.RegisterTask(Task{
83+
name: "CheckAndUpdateStatus",
84+
taskFunc: postgres.CheckAndUpdateStatus,
85+
taskType: PeriodicTask,
86+
interval: 1 * time.Minute,
87+
})
88+
8289
manager.Start(ctx)
8390
}

pkg/drives/drive.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,11 @@ func (rs *DriveResourceService) GeneratePathList(db *gorm.DB, processor PathProc
226226
func (rs *DriveResourceService) parsePathToURI(path string) (string, string) {
227227
pathSplit := strings.Split(strings.TrimPrefix(path, "/"), "/")
228228
if len(pathSplit) < 2 {
229-
return "Unknown", path
229+
return "unknown", path
230230
}
231231
if strings.HasPrefix(pathSplit[1], "pvc-userspace-") {
232232
if len(pathSplit) == 2 {
233-
return "Unknown", path
233+
return "unknown", path
234234
}
235235
if pathSplit[2] == "Data" {
236236
return "data", filepath.Join(pathSplit[1:]...)
@@ -240,9 +240,9 @@ func (rs *DriveResourceService) parsePathToURI(path string) (string, string) {
240240
}
241241
if pathSplit[1] == "External" {
242242
externalPath := ParseExternalPath(filepath.Join(pathSplit[2:]...))
243-
return "External", externalPath
243+
return "external", externalPath
244244
}
245-
return "Error", path
245+
return "error", path
246246
}
247247

248248
func generateListingData(listing *files.Listing, stopChan <-chan struct{}, dataChan chan<- string, d *common.Data, mountedData []files.DiskInfo) {
@@ -502,14 +502,14 @@ func ParseExternalPath(path string) string {
502502
if strings.HasPrefix(path, datum.Path) {
503503
idSerial := datum.IDSerial
504504
if idSerial == "" {
505-
idSerial = "root"
505+
idSerial = datum.Type + "_device"
506506
}
507507
partationUUID := datum.PartitionUUID
508508
if partationUUID == "" {
509-
partationUUID = "root"
509+
partationUUID = datum.Type + "_partition"
510510
}
511511
return filepath.Join(datum.Type, idSerial, partationUUID, path)
512512
}
513513
}
514-
return "unknown_type/unknown_device/unknow_partation/" + path
514+
return ""
515515
}

pkg/postgres/path_list.go

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"gorm.io/gorm"
99
"gorm.io/gorm/clause"
1010
"k8s.io/klog/v2"
11+
"os"
12+
"path/filepath"
13+
"strings"
1114
"sync"
1215
"time"
1316
)
@@ -22,6 +25,8 @@ type PathList struct {
2225
UpdateTime time.Time `gorm:"not null;type:timestamptz;autoUpdateTime"`
2326
}
2427

28+
var pathListInited bool = false
29+
2530
func createPathListTable() {
2631
// Migrate the schema, create the table if it does not exist
2732
err := DBServer.AutoMigrate(&PathList{})
@@ -60,6 +65,8 @@ func InitDrivePathList() {
6065
}
6166
}
6267

68+
pathListInited = true
69+
6370
if err := logPathList(); err != nil {
6471
fmt.Println("Error logging path list:", err)
6572
}
@@ -82,14 +89,31 @@ func GenerateOtherPathList(ctx context.Context) {
8289
//klog.Info("~~~Temp log: cookie hasn't come")
8390
}
8491
mu.Unlock()
92+
93+
select {
94+
case <-ctx.Done():
95+
return
96+
default:
97+
}
8598
}
8699
}()
87100

88101
for {
89102
mu.Lock()
90103
for len(common.BflCookieCache) == 0 {
91104
//klog.Info("~~~Temp log: waiting for cookie")
92-
cond.Wait()
105+
done := ctx.Done()
106+
if done != nil {
107+
select {
108+
case <-done:
109+
mu.Unlock()
110+
return
111+
default:
112+
cond.Wait()
113+
}
114+
} else {
115+
cond.Wait()
116+
}
93117
}
94118

95119
var srcTypeList = []string{
@@ -136,7 +160,7 @@ func logPathList() error {
136160
}
137161

138162
func ProcessDirectory(db *gorm.DB, drive, path string, modTime time.Time) error {
139-
if drive == "Unknown" || drive == "Error" || path == "" {
163+
if drive == "unknown" || drive == "error" || path == "" {
140164
// won't deal with these on purpose
141165
return nil
142166
}
@@ -187,3 +211,70 @@ func batchUpdate(paths []PathList) error {
187211
}),
188212
}).Create(&paths).Error
189213
}
214+
215+
func CheckAndUpdateStatus(ctx context.Context) {
216+
if !pathListInited {
217+
return
218+
}
219+
220+
var pathEntries []PathList
221+
222+
if err := DBServer.WithContext(ctx).Where("drive IN (?, ?, ?, ?)", "drive", "data", "cache", "external").Find(&pathEntries).Error; err != nil {
223+
klog.Errorf("failed to query drive path list: %v", err)
224+
return
225+
}
226+
227+
for _, entry := range pathEntries {
228+
var fullPath string
229+
var exists bool
230+
var err error
231+
232+
switch entry.Drive {
233+
case "drive":
234+
fullPath = "/data/" + entry.Path
235+
case "data":
236+
fullPath = "/data/" + entry.Path
237+
case "cache":
238+
fullPath = "/appcache/" + entry.Path
239+
case "external":
240+
pathSplit := strings.Split(entry.Path, "/")
241+
fullPath = "/data/External/" + filepath.Join(pathSplit[3:]...)
242+
default:
243+
continue
244+
}
245+
246+
exists, err = pathExists(fullPath)
247+
if err != nil {
248+
klog.Errorf("failed to check if path exists: %v", err)
249+
return
250+
}
251+
252+
var newStatus int
253+
if exists {
254+
newStatus = 0
255+
} else {
256+
newStatus = 1
257+
}
258+
259+
if entry.Status == newStatus {
260+
continue
261+
}
262+
263+
if err := DBServer.WithContext(ctx).Model(&PathList{}).Where("drive = ? AND path = ?", entry.Drive, entry.Path).Update("status", newStatus).Error; err != nil {
264+
klog.Errorf("failed to update drive path status: %v", err)
265+
return
266+
}
267+
}
268+
269+
return
270+
}
271+
272+
func pathExists(path string) (bool, error) {
273+
_, err := os.Stat(path)
274+
if os.IsNotExist(err) {
275+
return false, nil
276+
} else if err != nil {
277+
return false, err
278+
}
279+
return true, nil
280+
}

0 commit comments

Comments
 (0)