/
worker.go
91 lines (79 loc) · 1.75 KB
/
worker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package database
import (
"context"
"github.com/ducesoft/ulsp/log"
"sync"
)
type Worker struct {
dbRepo DBRepository
dbCache *DBCache
done chan struct{}
update chan struct{}
lock sync.Mutex
}
func NewWorker() *Worker {
return &Worker{
done: make(chan struct{}, 1),
update: make(chan struct{}, 1),
}
}
func (w *Worker) Cache() *DBCache {
return w.dbCache
}
func (w *Worker) setCache(c *DBCache) {
w.lock.Lock()
defer w.lock.Unlock()
w.dbCache = c
}
func (w *Worker) setColumnCache(col map[string][]*ColumnDesc) {
w.lock.Lock()
defer w.lock.Unlock()
if w.dbCache != nil {
w.dbCache.ColumnsWithParent = col
}
}
func (w *Worker) Start() {
go func() {
ctx := context.Background()
log.Info(ctx, "LSP server worker has been started. ")
for {
select {
case <-w.done:
log.Info(ctx, "LSP server worker has been stopped. ")
return
case <-w.update:
generator := NewDBCacheUpdater(w.dbRepo)
col, err := generator.GenerateDBCacheSecondary(context.Background())
if err != nil {
log.Error(ctx, err.Error())
}
w.setColumnCache(col)
log.Info(ctx, "LSP server worker update db cache secondary complete")
}
}
}()
}
func (w *Worker) Stop() {
close(w.done)
}
func (w *Worker) ReCache(ctx context.Context, repo DBRepository) error {
w.dbRepo = repo
if err := w.updateAllCache(ctx); err != nil {
return err
}
w.updateAdditionalCache()
return nil
}
func (w *Worker) updateAllCache(ctx context.Context) error {
generator := NewDBCacheUpdater(w.dbRepo)
cache, err := generator.GenerateDBCachePrimary(ctx)
if err != nil {
return err
}
w.setCache(cache)
log.Info(ctx, "db worker: Update db cache primary complete")
return nil
}
func (w *Worker) updateAdditionalCache() {
w.update <- struct{}{}
}