From 187a6569ff5646709fab3625763e0fd2cf8d1fd4 Mon Sep 17 00:00:00 2001 From: Seednode Date: Tue, 30 Jan 2024 04:27:18 -0600 Subject: [PATCH] Add --index-interval for automatic index rebuilds --- cmd/index.go | 10 +++++++--- cmd/root.go | 4 +++- cmd/web.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/cmd/index.go b/cmd/index.go index 1a7211f..1d524ca 100644 --- a/cmd/index.go +++ b/cmd/index.go @@ -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") diff --git a/cmd/root.go b/cmd/root.go index 4da6971..90ce92a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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") diff --git a/cmd/web.go b/cmd/web.go index 0bdbca3..86fe7e0 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -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 }