refactor(gateway): persist conversations in sqlite
This commit is contained in:
1 parent
95c5d59e7c
commit
3afbab8201
3 files changed
+62
-8
No files matched your search
+10
-1
@@ -347,7 +347,16 @@ func run() int {
|
||||
log.Error("close store", "err", err)
|
||||
}
|
||||
}()
|
||||
chatSessionStore := makeTelegramSessionStore(telegramSetup{St: st, Cfg: cfg})
|
||||
chatSessionStore, err := makeTelegramSessionStore(cfg)
|
||||
if err != nil {
|
||||
log.Error("open conversation store", "path", conversationDBPath(cfg.DBPath), "err", err)
|
||||
return 1
|
||||
}
|
||||
defer func() {
|
||||
if err := chatSessionStore.Close(); err != nil {
|
||||
log.Error("close conversation store", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if *requeue > 0 {
|
||||
if err := st.Requeue(context.Background(), *requeue, "manual", ""); err != nil {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
func TestTelegramConversationStoreUsesFreshSQLiteWithoutReadingTaskLog(t *testing.T) {
|
||||
taskLogPath := filepath.Join(t.TempDir(), "tasks.nell")
|
||||
taskLog := []byte("not a SQLite database")
|
||||
if err := os.WriteFile(taskLogPath, taskLog, 0o600); err != nil {
|
||||
t.Fatalf("write task log fixture: %v", err)
|
||||
}
|
||||
|
||||
sessions, err := makeTelegramSessionStore(config.Config{
|
||||
DBPath: taskLogPath,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("makeTelegramSessionStore: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = sessions.Close() })
|
||||
|
||||
if got, want := fmt.Sprintf("%T", sessions), "*gateway.sqliteSessionStore"; got != want {
|
||||
t.Fatalf("conversation store type = %q, want %q", got, want)
|
||||
}
|
||||
if _, err := os.Stat(conversationDBPath(taskLogPath)); err != nil {
|
||||
t.Fatalf("stat conversation database: %v", err)
|
||||
}
|
||||
gotTaskLog, err := os.ReadFile(taskLogPath)
|
||||
if err != nil {
|
||||
t.Fatalf("read task log fixture: %v", err)
|
||||
}
|
||||
if !bytes.Equal(gotTaskLog, taskLog) {
|
||||
t.Fatalf("task log changed during conversation-store cutover: got %q, want %q", gotTaskLog, taskLog)
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/nell"
|
||||
"github.com/samcharles93/archie-core/internal/releaseannounce"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
@@ -94,7 +93,8 @@ func setupTelegramGateway(ctx context.Context, s telegramSetup) (start func(), o
|
||||
|
||||
sessionStore := s.SessionStore
|
||||
if sessionStore == nil {
|
||||
sessionStore = makeTelegramSessionStore(s)
|
||||
s.Log.Error("telegram conversation store is not configured")
|
||||
return nil, false
|
||||
}
|
||||
router := buildTelegramRouter(tg, s, sessionStore)
|
||||
return func() {
|
||||
@@ -155,11 +155,15 @@ func makeTelegramReload(s telegramSetup) func(*telegram.Gateway) error {
|
||||
}
|
||||
}
|
||||
|
||||
func makeTelegramSessionStore(s telegramSetup) gateway.SessionStore {
|
||||
if nellAdapter, ok := s.St.(*nell.Adapter); ok {
|
||||
return gateway.NewSessionStore(nellAdapter.Store(), s.Cfg.BotUser)
|
||||
}
|
||||
return gateway.NewSessionStoreMemory(s.Cfg.BotUser)
|
||||
func makeTelegramSessionStore(cfg config.Config) (gateway.SessionStore, error) {
|
||||
return gateway.OpenSQLiteSessionStore(conversationDBPath(cfg.DBPath))
|
||||
}
|
||||
|
||||
// conversationDBPath keeps conversation state separate while db_path still
|
||||
// names the NellDB task log. The suffix is deterministic so the eventual task
|
||||
// store cutover can open db_path as SQLite without moving conversation data.
|
||||
func conversationDBPath(taskDBPath string) string {
|
||||
return taskDBPath + "-conversations.sqlite"
|
||||
}
|
||||
|
||||
func buildTelegramRouter(tg *telegram.Gateway, s telegramSetup, sessionStore gateway.SessionStore) *gateway.Router {
|
||||
|
||||
Reference in new issue
Block a user