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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
package main
import (
"flag"
"fmt"
"log"
"log/slog"
"os"
"os/signal"
"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"
)
var buildVersion string
var (
configFile = flag.String("config", "config.toml", "config `file` to use")
printDefaultConfig = flag.Bool(
"print-default-config",
false,
"print default configuration and exit",
)
liveReload = flag.Bool("live", false, "whether to enable live reloading (development)")
replace = flag.Bool(
"replace",
false,
"whether to replace existing database, if it exists",
)
version = flag.Bool("version", false, "print version information")
)
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 main() {
flag.Parse()
if *version {
fmt.Fprintf(os.Stderr, "searchix %s", buildVersion)
if buildVersion != config.CommitSHA && buildVersion != config.ShortSHA {
fmt.Fprintf(os.Stderr, " %s", config.CommitSHA)
}
_, err := fmt.Fprint(os.Stderr, "\n")
if err != nil {
panic("can't write to standard error?!")
}
os.Exit(0)
}
if *printDefaultConfig {
_, err := fmt.Print(config.GetDefaultConfig())
if err != nil {
panic("can't write to standard output?!")
}
os.Exit(0)
}
cfg, err := config.GetConfig(*configFile)
if err != nil {
// only use log functions after the config file has been read successfully
fmt.Fprintf(os.Stderr, "error parsing configuration file: %v", err)
os.Exit(1)
}
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: 1.0,
Dsn: cfg.Web.SentryDSN,
Environment: cfg.Web.Environment,
})
if err != nil {
slog.Warn("could not initialise sentry", "error", err)
}
var i uint
cfgEnabledSources := make([]string, len(cfg.Importer.Sources))
for key := range cfg.Importer.Sources {
cfgEnabledSources[i] = key
i++
}
slices.Sort(cfgEnabledSources)
read, write, exists, err := index.OpenOrCreate(cfg.DataPath, *replace)
if err != nil {
log.Fatalf("Failed to open or create index: %v", err)
}
if !exists || *replace {
slog.Info("Index doesn't exist. Starting build job...")
err = importer.Start(cfg, write, *replace, nil)
if err != nil {
log.Fatalf("Failed to build index: %v", err)
}
if *replace {
return
}
} else {
indexedSources, err := read.GetEnabledSources()
if err != nil {
log.Fatalln("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(cfg, write, false, &newSources)
if err != nil {
log.Fatalf("failed to update index with new sources: %v", err)
}
}
if len(retiredSources) > 0 {
slog.Info("removing retired sources", "sources", retiredSources)
for _, s := range retiredSources {
err := write.DeleteBySource(s)
if err != nil {
log.Fatalf("failed to remove retired source %s from index: %v", s, err)
}
}
}
}
}
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt)
sv, err := server.New(cfg, read, *liveReload)
if err != nil {
log.Fatalf("error setting up server: %v", err)
}
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
sig := <-c
log.Printf("signal captured: %v", sig)
<-sv.Stop()
slog.Debug("server stopped")
}()
go func(localHub *sentry.Hub) {
const monitorSlug = "import"
localHub.WithScope(func(scope *sentry.Scope) {
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(),
}
nextRun := nextOccurrenceOfLocalTime(cfg.Importer.UpdateAt.LocalTime)
for {
slog.Debug("scheduling next run", "next-run", nextRun)
<-time.After(time.Until(nextRun))
wg.Add(1)
slog.Info("updating index")
eventID := localHub.CaptureCheckIn(&sentry.CheckIn{
MonitorSlug: monitorSlug,
Status: sentry.CheckInStatusInProgress,
}, monitorConfig)
err = importer.Start(cfg, write, false, nil)
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)
}
})
}(sentry.CurrentHub().Clone())
sErr := make(chan error)
wg.Add(1)
go func() {
defer wg.Done()
sErr <- sv.Start()
}()
if cfg.Web.Environment == "development" {
log.Printf("server listening on %s", cfg.Web.BaseURL.String())
}
err = <-sErr
if err != nil {
// Error starting or closing listener:
log.Fatalf("error: %v", err)
}
sentry.Flush(2 * time.Second)
wg.Wait()
}
|