Skip to content

Commit

Permalink
Optimize extraneous calls to chat from storage's cleanFilesOfDeletedC…
Browse files Browse the repository at this point in the history
…hatTask
  • Loading branch information
nkonev committed Nov 1, 2023
1 parent c051353 commit d9895d7
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions storage/tasks/clean_files_of_deleted_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tasks

import (
"context"
"fmt"
"github.com/ehsaniara/gointerlock"
redisV8 "github.com/go-redis/redis/v8"
"github.com/minio/minio-go/v7"
Expand Down Expand Up @@ -38,44 +39,65 @@ type CleanFilesOfDeletedChatService struct {

func (srv *CleanFilesOfDeletedChatService) doJob() {
ct := context.Background()
filenameChatPrefix := "chat/"
srv.processChats(filenameChatPrefix, ct)
srv.processChats(ct)
}

func (srv *CleanFilesOfDeletedChatService) processChats(filenameChatPrefix string, c context.Context) {
func (srv *CleanFilesOfDeletedChatService) processChats(c context.Context) {
filenameChatPrefix := "chat/"

logger.Logger.Infof("Starting cleaning files of deleted chats job")
var objects <-chan minio.ObjectInfo = srv.minioClient.ListObjects(c, srv.minioBucketsConfig.Files, minio.ListObjectsOptions{

// get only top-level chats (no recursive)
var objectsChats <-chan minio.ObjectInfo = srv.minioClient.ListObjects(c, srv.minioBucketsConfig.Files, minio.ListObjectsOptions{
Prefix: filenameChatPrefix,
Recursive: true,
Recursive: false,
})

for objInfo := range objects {
for chatObjInfo := range objectsChats {
// here in minio 'chat/108/'
logger.Logger.Debugf("Start processing minio key '%v'", objInfo.Key)
chatId, err := utils.ParseChatId(objInfo.Key)
logger.Logger.Debugf("Start processing minio key '%v'", chatObjInfo.Key)
chatId, err := utils.ParseChatId(chatObjInfo.Key)
if err != nil {
logger.Logger.Errorf("Unable to extract chat id from %v", objInfo.Key)
logger.Logger.Errorf("Unable to extract chat id from %v", chatObjInfo.Key)
continue
}
logger.Logger.Debugf("Successfully get chatId '%v'", chatId)
logger.Logger.Debugf("Successfully got chatId '%v'", chatId)


exists, err := srv.chatClient.CheckIsChatExists(chatId, c)
// check chat's existence
chatExists, err := srv.chatClient.CheckIsChatExists(chatId, c)
if err != nil {
logger.Logger.Errorf("Unable to chech existence of chat id from %v", objInfo.Key)
logger.Logger.Errorf("Unable to chech existence of chat id %v", chatId)
continue
}
if !exists {
logger.Logger.Infof("Deleting file(directory) object %v", objInfo.Key)
err := srv.minioClient.RemoveObject(c, srv.minioBucketsConfig.Files, objInfo.Key, minio.RemoveObjectOptions{})
if err != nil {
logger.Logger.Errorf("Object file %v has been cleared from minio with error: %v", objInfo.Key, err)


// performing cleanup in minio - getting subfolders (recursively)
filenameChatFilesPrefix := fmt.Sprintf("chat/%v/", chatId)
var objectsOfChat <-chan minio.ObjectInfo = srv.minioClient.ListObjects(c, srv.minioBucketsConfig.Files, minio.ListObjectsOptions{
Prefix: filenameChatFilesPrefix,
Recursive: true,
})
for objInfo := range objectsOfChat {
// here in minio 'chat/108/'
logger.Logger.Debugf("Start processing minio key '%v'", objInfo.Key)

if !chatExists {
logger.Logger.Infof("Deleting file(directory) object %v", objInfo.Key)
err := srv.minioClient.RemoveObject(c, srv.minioBucketsConfig.Files, objInfo.Key, minio.RemoveObjectOptions{})
if err != nil {
logger.Logger.Errorf("Object file %v has been cleared from minio with error: %v", objInfo.Key, err)
} else {
logger.Logger.Debugf("Object file %v has been cleared from minio successfully", objInfo.Key)
}
} else {
logger.Logger.Debugf("Object file %v has been cleared from minio successfully", objInfo.Key)
logger.Logger.Debugf("Chat %v is present, skipping", chatId)
}
} else {
logger.Logger.Debugf("Chat %v is present, skipping", chatId)
}

}


logger.Logger.Infof("End of processChats job")
}

Expand Down

0 comments on commit d9895d7

Please sign in to comment.