-
Notifications
You must be signed in to change notification settings - Fork 24
/
hints.go
46 lines (40 loc) · 1.02 KB
/
hints.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
package main
import (
"context"
"encoding/json"
"os"
"time"
"github.com/nbd-wtf/go-nostr/sdk/hints/memory"
)
// save these things to a file so we can reload them later
func outboxHintsFileLoaderSaver(ctx context.Context) {
if file, err := os.Open(s.HintsMemoryDumpPath); err == nil {
hdb := memory.NewHintDB()
if err := json.NewDecoder(file).Decode(&hdb); err == nil {
sys.Hints = hdb
}
file.Close()
}
const tmp = "/tmp/njump-outbox-hints-tmp.json"
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Minute * 5):
}
hdb := sys.Hints.(*memory.HintDB)
file, err := os.Create(tmp)
if err != nil {
log.Error().Err(err).Str("path", tmp).Msg("failed to create outbox hints file")
time.Sleep(time.Hour)
continue
}
file.Close()
json.NewEncoder(file).Encode(hdb)
if err := os.Rename(tmp, s.HintsMemoryDumpPath); err != nil {
log.Error().Err(err).Str("from", tmp).Str("to", s.HintsMemoryDumpPath).Msg("failed to move outbox hints file")
time.Sleep(time.Hour)
continue
}
}
}