Move index rebuild timer function into index.go

This commit is contained in:
Seednode 2024-01-30 06:25:14 -06:00
parent efd28adbc3
commit dc3eda4047
3 changed files with 48 additions and 46 deletions

View File

@ -188,6 +188,38 @@ func (index *fileIndex) Import(path string, errorChannel chan<- error) {
} }
} }
func registerIndexInterval(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, quit <-chan struct{}, errorChannel chan<- error) {
interval, err := time.ParseDuration(IndexInterval)
if err != nil {
errorChannel <- err
return
}
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
}
}
}()
}
func rebuildIndex(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) { func rebuildIndex(args []string, index *fileIndex, formats types.Types, encoder *zstd.Encoder, errorChannel chan<- error) {
index.clear() index.clear()

View File

@ -17,7 +17,7 @@ import (
const ( const (
AllowedCharacters string = `^[A-z0-9.\-_]+$` AllowedCharacters string = `^[A-z0-9.\-_]+$`
ReleaseVersion string = "6.4.2" ReleaseVersion string = "6.4.3"
) )
var ( var (

View File

@ -52,21 +52,6 @@ func newPage(title, body string) string {
return htmlBody.String() return htmlBody.String()
} }
func noFiles(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
w.Header().Set("Content-Type", "text/plain;charset=UTF-8")
w.Write([]byte("No files found in the specified path(s).\n"))
if Verbose {
fmt.Printf("%s | SERVE: Empty path notification to %s\n",
startTime.Format(logDate),
r.RemoteAddr,
)
}
}
func serveStaticFile(paths []string, index *fileIndex, errorChannel chan<- error) httprouter.Handle { func serveStaticFile(paths []string, index *fileIndex, 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) {
prefix := Prefix + sourcePrefix prefix := Prefix + sourcePrefix
@ -221,7 +206,18 @@ func serveRoot(paths []string, index *fileIndex, filename *regexp.Regexp, format
path, err = newFile(list, sortOrder, filename, formats) path, err = newFile(list, sortOrder, filename, formats)
switch { switch {
case path == "": case path == "":
noFiles(w, r) startTime := time.Now()
w.Header().Set("Content-Type", "text/plain;charset=UTF-8")
w.Write([]byte("No files found in the specified path(s).\n"))
if Verbose {
fmt.Printf("%s | SERVE: Empty path notification to %s\n",
startTime.Format(logDate),
r.RemoteAddr,
)
}
return return
case err != nil && err == ErrNoMediaFound: case err != nil && err == ErrNoMediaFound:
@ -458,9 +454,10 @@ func redirectRoot() httprouter.Handle {
} }
func ServePage(args []string) error { func ServePage(args []string) error {
var err error
timeZone := os.Getenv("TZ") timeZone := os.Getenv("TZ")
if timeZone != "" { if timeZone != "" {
var err error
time.Local, err = time.LoadLocation(timeZone) time.Local, err = time.LoadLocation(timeZone)
if err != nil { if err != nil {
return err return err
@ -593,34 +590,7 @@ func ServePage(args []string) error {
importIndex(paths, index, formats, encoder, errorChannel) importIndex(paths, index, formats, encoder, errorChannel)
if IndexInterval != "" { if IndexInterval != "" {
interval, err := time.ParseDuration(IndexInterval) registerIndexInterval(args, index, formats, encoder, quit, errorChannel)
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
}
}
}()
} }
} }