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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
package searchix
import (
"context"
"log"
"log/slog"
"slices"
"sync"
"time"
"searchix/internal/config"
"searchix/internal/importer"
"searchix/internal/index"
"searchix/internal/server"
"github.com/getsentry/sentry-go"
"github.com/pelletier/go-toml/v2"
"github.com/pkg/errors"
)
func nextOccurrenceOfLocalTime(t toml.LocalTime) time.Time {
now := time.Now()
dayTime := t
nextRun := time.Date(
now.Year(),
now.Month(),
now.Day(),
dayTime.Hour,
dayTime.Minute,
dayTime.Second,
0,
time.Local,
)
if nextRun.Before(now) {
return nextRun.AddDate(0, 0, 1)
}
return nextRun
}
func (s *Server) SetupIndex(update bool, replace bool) error {
var i uint
cfgEnabledSources := make([]string, len(s.cfg.Importer.Sources))
for key := range s.cfg.Importer.Sources {
cfgEnabledSources[i] = key
i++
}
slices.Sort(cfgEnabledSources)
read, write, exists, err := index.OpenOrCreate(s.cfg.DataPath, replace)
if err != nil {
return errors.Wrap(err, "Failed to open or create index")
}
s.readIndex = read
s.writeIndex = write
if !exists || replace || update {
slog.Info(
"Starting build job",
"new",
!exists,
"replace",
replace,
"update",
update,
)
err = importer.Start(s.cfg, write, replace || update, nil)
if err != nil {
return errors.Wrap(err, "Failed to build index")
}
if replace || update {
return nil
}
} else {
indexedSources, err := read.GetEnabledSources()
if err != nil {
return errors.Wrap(err, "Failed to get enabled sources from index")
}
slices.Sort(indexedSources)
if !slices.Equal(cfgEnabledSources, indexedSources) {
newSources := slices.DeleteFunc(slices.Clone(cfgEnabledSources), func(s string) bool {
return slices.Contains(indexedSources, s)
})
retiredSources := slices.DeleteFunc(slices.Clone(indexedSources), func(s string) bool {
return slices.Contains(cfgEnabledSources, s)
})
if len(newSources) > 0 {
slog.Info("adding new sources", "sources", newSources)
err := importer.Start(s.cfg, write, false, &newSources)
if err != nil {
return errors.Wrap(err, "Failed to update index with new sources")
}
}
if len(retiredSources) > 0 {
slog.Info("removing retired sources", "sources", retiredSources)
for _, s := range retiredSources {
err := write.DeleteBySource(s)
if err != nil {
return errors.Wrapf(err, "Failed to remove retired source %s", s)
}
}
}
}
}
return nil
}
type Server struct {
sv *server.Server
wg *sync.WaitGroup
cfg *config.Config
sentryHub *sentry.Hub
readIndex *index.ReadIndex
writeIndex *index.WriteIndex
}
func New(cfg *config.Config) (*Server, error) {
slog.SetLogLoggerLevel(cfg.LogLevel)
if cfg.Web.Environment == "production" {
log.SetFlags(0)
} else {
log.SetFlags(log.LstdFlags)
}
err := sentry.Init(sentry.ClientOptions{
EnableTracing: true,
TracesSampleRate: 0.01,
Dsn: cfg.Web.SentryDSN,
Environment: cfg.Web.Environment,
})
if err != nil {
slog.Warn("could not initialise sentry", "error", err)
}
return &Server{
cfg: cfg,
sentryHub: sentry.CurrentHub(),
}, nil
}
func (s *Server) startUpdateTimer(
ctx context.Context,
localHub *sentry.Hub,
) {
const monitorSlug = "import"
localHub.WithScope(func(scope *sentry.Scope) {
var err error
scope.SetContext("monitor", sentry.Context{"slug": monitorSlug})
monitorConfig := &sentry.MonitorConfig{
Schedule: sentry.IntervalSchedule(1, sentry.MonitorScheduleUnitDay),
// minutes
MaxRuntime: 10,
CheckInMargin: 5,
Timezone: time.Local.String(),
}
s.wg.Add(1)
nextRun := nextOccurrenceOfLocalTime(s.cfg.Importer.UpdateAt.LocalTime)
for {
slog.Debug("scheduling next run", "next-run", nextRun)
select {
case <-ctx.Done():
slog.Debug("stopping scheduler")
s.wg.Done()
return
case <-time.After(time.Until(nextRun)):
}
s.wg.Add(1)
slog.Info("updating index")
eventID := localHub.CaptureCheckIn(&sentry.CheckIn{
MonitorSlug: monitorSlug,
Status: sentry.CheckInStatusInProgress,
}, monitorConfig)
err = importer.Start(s.cfg, s.writeIndex, false, nil)
s.wg.Done()
if err != nil {
slog.Warn("error updating index", "error", err)
localHub.CaptureException(err)
localHub.CaptureCheckIn(&sentry.CheckIn{
ID: *eventID,
MonitorSlug: monitorSlug,
Status: sentry.CheckInStatusError,
}, monitorConfig)
} else {
slog.Info("update complete")
localHub.CaptureCheckIn(&sentry.CheckIn{
ID: *eventID,
MonitorSlug: monitorSlug,
Status: sentry.CheckInStatusOK,
}, monitorConfig)
}
nextRun = nextRun.AddDate(0, 0, 1)
}
})
}
func (s *Server) Start(ctx context.Context, liveReload bool) error {
var err error
s.sv, err = server.New(s.cfg, s.readIndex, liveReload)
if err != nil {
return errors.Wrap(err, "error setting up server")
}
s.wg = &sync.WaitGroup{}
go s.startUpdateTimer(ctx, sentry.CurrentHub().Clone())
s.wg.Add(1)
err = s.sv.Start()
if err != nil {
s.wg.Done()
return errors.Wrap(err, "error starting server")
}
return nil
}
func (s *Server) Stop() {
<-s.sv.Stop()
defer s.wg.Done()
s.sentryHub.Flush(2 * time.Second)
}
|