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 { 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) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
startTime := time.Now() startTime := time.Now()
index.clear() rebuildIndex(args, index, formats, encoder, errorChannel)
fileList(args, &filters{}, "", index, formats, encoder, errorChannel)
w.Header().Set("Content-Type", "text/plain;charset=UTF-8") w.Header().Set("Content-Type", "text/plain;charset=UTF-8")

View File

@ -17,7 +17,7 @@ import (
const ( const (
AllowedCharacters string = `^[A-z0-9.\-_]+$` AllowedCharacters string = `^[A-z0-9.\-_]+$`
ReleaseVersion string = "6.3.1" ReleaseVersion string = "6.4.0"
) )
var ( var (
@ -43,6 +43,7 @@ var (
Images bool Images bool
Index bool Index bool
IndexFile string IndexFile string
IndexInterval string
Info bool Info bool
MaxFileCount int MaxFileCount int
MinFileCount int MinFileCount int
@ -136,6 +137,7 @@ func init() {
rootCmd.Flags().BoolVar(&Images, "images", false, "enable support for image files") 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().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(&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().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(&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") 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)) mux.GET(Prefix+"/version", serveVersion(errorChannel))
quit := make(chan struct{})
if Index { if Index {
mux.GET(Prefix+AdminPrefix+"/index/rebuild", serveIndexRebuild(args, index, formats, encoder, errorChannel)) mux.GET(Prefix+AdminPrefix+"/index/rebuild", serveIndexRebuild(args, index, formats, encoder, errorChannel))
importIndex(paths, 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 { if Info {
@ -615,5 +648,7 @@ func ServePage(args []string) error {
return err return err
} }
close(quit)
return nil return nil
} }