Skip to content

Commit 122a5be

Browse files
committed
fix: update lastUpdate correctly
Signed-off-by: mudler <mudler@localai.io>
1 parent 1298530 commit 122a5be

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

rag/persistency.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
// CollectionState represents the persistent state of a collection
2121
type CollectionState struct {
22-
ExternalSources []ExternalSource `json:"external_sources"`
22+
ExternalSources []*ExternalSource `json:"external_sources"`
2323
Index map[string][]engine.Result `json:"index"`
2424
}
2525

@@ -29,7 +29,7 @@ type PersistentKB struct {
2929
path string
3030
assetDir string
3131
maxChunkSize int
32-
sources []ExternalSource
32+
sources []*ExternalSource
3333

3434
index map[string][]engine.Result
3535
}
@@ -48,7 +48,7 @@ func loadDB(path string) (*CollectionState, error) {
4848
if err := json.Unmarshal(data, &legacyFiles); err != nil {
4949
return nil, err
5050
}
51-
state.ExternalSources = []ExternalSource{}
51+
state.ExternalSources = []*ExternalSource{}
5252
state.Index = map[string][]engine.Result{}
5353
}
5454

@@ -68,7 +68,7 @@ func NewPersistentCollectionKB(stateFile, assetDir string, store Engine, maxChun
6868
Engine: store,
6969
assetDir: assetDir,
7070
maxChunkSize: maxChunkSize,
71-
sources: []ExternalSource{},
71+
sources: []*ExternalSource{},
7272
index: map[string][]engine.Result{},
7373
}
7474
persistentKB.Lock()
@@ -104,7 +104,7 @@ func (db *PersistentKB) Reset() error {
104104
for f := range db.index {
105105
os.Remove(filepath.Join(db.assetDir, f))
106106
}
107-
db.sources = []ExternalSource{}
107+
db.sources = []*ExternalSource{}
108108
db.index = map[string][]engine.Result{}
109109
db.save()
110110
db.Unlock()
@@ -368,14 +368,14 @@ func chunkFile(fpath string, maxchunksize int) ([]string, error) {
368368
}
369369

370370
// GetExternalSources returns the list of external sources for this collection
371-
func (db *PersistentKB) GetExternalSources() []ExternalSource {
371+
func (db *PersistentKB) GetExternalSources() []*ExternalSource {
372372
db.Lock()
373373
defer db.Unlock()
374374
return db.sources
375375
}
376376

377377
// AddExternalSource adds an external source to the collection
378-
func (db *PersistentKB) AddExternalSource(source ExternalSource) error {
378+
func (db *PersistentKB) AddExternalSource(source *ExternalSource) error {
379379
db.Lock()
380380
defer db.Unlock()
381381

rag/source_manager.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ type ExternalSource struct {
2222

2323
// SourceManager manages external sources for collections
2424
type SourceManager struct {
25-
sources map[string][]ExternalSource // collection name -> sources
26-
collections map[string]*PersistentKB // collection name -> collection
25+
sources map[string][]*ExternalSource // collection name -> sources
26+
collections map[string]*PersistentKB // collection name -> collection
2727
mu sync.RWMutex
2828
ctx context.Context
2929
cancel context.CancelFunc
@@ -33,7 +33,7 @@ type SourceManager struct {
3333
func NewSourceManager() *SourceManager {
3434
ctx, cancel := context.WithCancel(context.Background())
3535
return &SourceManager{
36-
sources: make(map[string][]ExternalSource),
36+
sources: make(map[string][]*ExternalSource),
3737
collections: make(map[string]*PersistentKB),
3838
ctx: ctx,
3939
cancel: cancel,
@@ -72,14 +72,14 @@ func (sm *SourceManager) AddSource(collectionName, url string, updateInterval ti
7272
}
7373

7474
// Add the source to the collection's persistent storage
75-
if err := collection.AddExternalSource(source); err != nil {
75+
if err := collection.AddExternalSource(&source); err != nil {
7676
return err
7777
}
7878

79-
sm.sources[collectionName] = append(sm.sources[collectionName], source)
79+
sm.sources[collectionName] = append(sm.sources[collectionName], &source)
8080

8181
// Trigger an immediate update
82-
go sm.updateSource(collectionName, source, collection)
82+
go sm.updateSource(collectionName, &source, collection)
8383

8484
return nil
8585
}
@@ -116,7 +116,10 @@ func (sm *SourceManager) RemoveSource(collectionName, url string) error {
116116
}
117117

118118
// updateSource updates a single source
119-
func (sm *SourceManager) updateSource(collectionName string, source ExternalSource, collection *PersistentKB) {
119+
func (sm *SourceManager) updateSource(collectionName string, source *ExternalSource, collection *PersistentKB) {
120+
121+
// update LastUpdate
122+
source.LastUpdate = time.Now()
120123

121124
xlog.Info("Updating source", "url", source.URL)
122125
content, err := sources.SourceRouter(source.URL)

test/e2e/persistency_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ var _ = Describe("Persistency", func() {
123123
LastUpdate: time.Now(),
124124
}
125125

126-
err := kb.AddExternalSource(source)
126+
err := kb.AddExternalSource(&source)
127127
Expect(err).To(BeNil())
128128

129129
sources := kb.GetExternalSources()
@@ -144,10 +144,10 @@ var _ = Describe("Persistency", func() {
144144
LastUpdate: time.Now(),
145145
}
146146

147-
err := kb.AddExternalSource(source)
147+
err := kb.AddExternalSource(&source)
148148
Expect(err).To(BeNil())
149149

150-
err = kb.AddExternalSource(source)
150+
err = kb.AddExternalSource(&source)
151151
Expect(err).ToNot(BeNil())
152152
})
153153
})

test/e2e/source_manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ var _ = Describe("SourceManager", func() {
7676
UpdateInterval: DefaultUpdateInterval,
7777
LastUpdate: time.Now(),
7878
}
79-
err := kb.AddExternalSource(source)
79+
err := kb.AddExternalSource(&source)
8080
Expect(err).To(BeNil())
8181

8282
// Register the collection
@@ -142,7 +142,7 @@ var _ = Describe("SourceManager", func() {
142142
sourceManager.Start()
143143

144144
// Wait for at least one update cycle and verify the source was updated
145-
Eventually(func() []rag.ExternalSource {
145+
Eventually(func() []*rag.ExternalSource {
146146
return kb.GetExternalSources()
147147
}, TestTimeout, TestPollingInterval).Should(HaveLen(1))
148148

0 commit comments

Comments
 (0)