Add --index-interval for automatic index rebuilds

This commit is contained in:
Seednode 2024-01-30 04:27:18 -06:00
parent 6fc21236a7
commit 187a6569ff
3 changed files with 45 additions and 4 deletions

View File

@ -188,13 +188,17 @@ func (index *fileIndex) Import(path string, errorChannel chan<- error) {
}
}
func rebuildIndex(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) {
index.clear()
fileList(args, &filters{}, "", index, formats, encoder, errorChannel)
}
func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
startTime := time.Now()
index.clear()
fileList(args, &filters{}, "", index, formats, encoder, errorChannel)
rebuildIndex(args, index, formats, encoder, errorChannel)
w.Header().Set("Content-Type", "text/plain;charset=UTF-8")

View File

@ -17,7 +17,7 @@ import (
const (
AllowedCharacters string = `^[A-z0-9.\-_]+$`
ReleaseVersion string = "6.3.1"
ReleaseVersion string = "6.4.0"
)
var (
@ -43,6 +43,7 @@ var (
Images bool
Index bool
IndexFile string
IndexInterval string
Info bool
MaxFileCount int
MinFileCount int
@ -136,6 +137,7 @@ func init() {
rootCmd.Flags().BoolVar(&Images, "images", false, "enable support for image files")
rootCmd.Flags().BoolVar(&Index, "index", false, "generate index of supported file paths at startup")
rootCmd.Flags().StringVar(&IndexFile, "index-file", "", "path to optional persistent index file")
rootCmd.Flags().StringVar(&IndexInterval, "index-interval", "", "regenerate index after this amount of time")
rootCmd.Flags().BoolVarP(&Info, "info", "i", false, "expose informational endpoints")
rootCmd.Flags().IntVar(&MaxFileCount, "max-file-count", math.MaxInt32, "skip directories with file counts above this value")
rootCmd.Flags().IntVar(&MinFileCount, "min-file-count", 0, "skip directories with file counts below this value")

View File

@ -584,10 +584,43 @@ func ServePage(args []string) error {
mux.GET(Prefix+"/version", serveVersion(errorChannel))
quit := make(chan struct{})
if Index {
mux.GET(Prefix+AdminPrefix+"/index/rebuild", serveIndexRebuild(args, index, formats, encoder, errorChannel))
importIndex(paths, index, formats, encoder, errorChannel)
if IndexInterval != "" {
interval, err := time.ParseDuration(IndexInterval)
if err != nil {
return err
}
ticker := time.NewTicker(interval)
go func() {
for {
select {
case <-ticker.C:
startTime := time.Now()
rebuildIndex(args, index, formats, encoder, errorChannel)
if Verbose {
fmt.Printf("%s | INDEX: Automatic rebuild took %s\n",
startTime.Format(logDate),
time.Since(startTime).Round(time.Microsecond),
)
}
case <-quit:
ticker.Stop()
return
}
}
}()
}
}
if Info {
@ -615,5 +648,7 @@ func ServePage(args []string) error {
return err
}
close(quit)
return nil
}