Browse Source
feat: Add chat storage auto-flush mechanism
feat: Add chat storage auto-flush mechanism
- Introduce new configuration option `--chat-flush-interval` to control chat storage cleanup - Implement periodic chat storage flushing with configurable interval (default 7 days) - Migrate chat storage from text to CSV format for better data management - Add thread-safe file handling for chat storage operations - Update root command to support new chat flush interval flagpull/271/head
5 changed files with 110 additions and 65 deletions
-
4src/cmd/root.go
-
3src/config/settings.go
-
46src/internal/rest/helpers/flushChatCsv.go
-
100src/pkg/utils/chat_storage.go
-
6src/views/components/SendMessage.js
@ -0,0 +1,46 @@ |
|||||
|
package helpers |
||||
|
|
||||
|
import ( |
||||
|
"os" |
||||
|
"sync" |
||||
|
"time" |
||||
|
|
||||
|
"github.com/aldinokemal/go-whatsapp-web-multidevice/config" |
||||
|
"github.com/sirupsen/logrus" |
||||
|
) |
||||
|
|
||||
|
var flushMutex sync.Mutex |
||||
|
|
||||
|
func FlushChatCsv() error { |
||||
|
flushMutex.Lock() |
||||
|
defer flushMutex.Unlock() |
||||
|
|
||||
|
// Create an empty file (truncating any existing content)
|
||||
|
file, err := os.OpenFile(config.PathChatStorage, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
defer file.Close() |
||||
|
|
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
// StartAutoFlushChatStorage starts a goroutine that periodically flushes the chat storage
|
||||
|
func StartAutoFlushChatStorage() { |
||||
|
interval := time.Duration(config.AppChatFlushIntervalDays) * 24 * time.Hour |
||||
|
|
||||
|
go func() { |
||||
|
ticker := time.NewTicker(interval) |
||||
|
defer ticker.Stop() |
||||
|
|
||||
|
for range ticker.C { |
||||
|
if err := FlushChatCsv(); err != nil { |
||||
|
logrus.Errorf("Error flushing chat storage: %v", err) |
||||
|
} else { |
||||
|
logrus.Info("Successfully flushed chat storage") |
||||
|
} |
||||
|
} |
||||
|
}() |
||||
|
|
||||
|
logrus.Infof("Auto flush for chat storage started (your account chat still safe). Will flush every %d days", config.AppChatFlushIntervalDays) |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue