diff --git a/pkg/index/index.go b/pkg/index/index.go index 54bc84c19..b651c317b 100644 --- a/pkg/index/index.go +++ b/pkg/index/index.go @@ -49,8 +49,8 @@ func init() { type Index struct { *blobserver.NoImplStorage - - s sorted.KeyValue + reindex bool // whether "reindex" was set in config (likely via perkeepd flag) + s sorted.KeyValue KeyFetcher blob.Fetcher // for verifying claims @@ -389,6 +389,7 @@ func newFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (blobserver.Stor } ix, err = New(kv) } + ix.reindex = reindex if reindex { ix.hasWiped = true } @@ -441,6 +442,8 @@ func ReindexMaxProcs() int { return reindexMaxProcs.v } +func (x *Index) WantsReindex() bool { return x.reindex } + func (x *Index) Reindex() error { x.Lock() if x.blobSource == nil { diff --git a/pkg/serverinit/serverinit.go b/pkg/serverinit/serverinit.go index bbf001c92..f5312778d 100644 --- a/pkg/serverinit/serverinit.go +++ b/pkg/serverinit/serverinit.go @@ -68,6 +68,7 @@ type handlerConfig struct { settingUp, setupDone bool } +// handlerLoader implements blobserver.Loader. type handlerLoader struct { installer HandlerInstaller baseURL string @@ -76,9 +77,10 @@ type handlerLoader struct { curPrefix string closers []io.Closer prefixStack []string - reindex bool } +var _ blobserver.Loader = (*handlerLoader)(nil) + // A HandlerInstaller is anything that can register an HTTP Handler at // a prefix path. Both *http.ServeMux and perkeep.org/pkg/webserver.Server // implement HandlerInstaller. @@ -307,18 +309,14 @@ func (hl *handlerLoader) setupHandler(prefix string) { hl.curPrefix = prefix if strings.HasPrefix(h.htype, "storage-") { - // Assume a storage interface + // Assume a storage interface: stype := strings.TrimPrefix(h.htype, "storage-") - if h.htype == "storage-index" && hl.reindex { - // Let the indexer know that we're in reindex mode - h.conf["reindex"] = true - } pstorage, err := blobserver.CreateStorage(stype, hl, h.conf) if err != nil { exitFailure("error instantiating storage for prefix %q, type %q: %v", h.prefix, stype, err) } - if ix, ok := pstorage.(*index.Index); ok && hl.reindex { + if ix, ok := pstorage.(*index.Index); ok && ix.WantsReindex() { log.Printf("Reindexing %s ...", h.prefix) if err := ix.Reindex(); err != nil { exitFailure("Error reindexing %s: %v", h.prefix, err) @@ -588,6 +586,28 @@ func (c *Config) checkValidAuth() error { return err } +func (c *Config) SetReindex(v bool) { + prefixes, _ := c.jconf["prefixes"].(map[string]interface{}) + for prefix, vei := range prefixes { + if prefix == "_knownkeys" { + continue + } + pmap, ok := vei.(map[string]interface{}) + if !ok { + continue + } + pconf := jsonconfig.Obj(pmap) + typ, _ := pconf["handler"].(string) + if typ != "storage-index" { + continue + } + opts, ok := pconf["handlerArgs"].(map[string]interface{}) + if ok { + opts["reindex"] = v + } + } +} + // InstallHandlers creates and registers all the HTTP Handlers needed // by config into the provided HandlerInstaller and validates that the // configuration is valid. @@ -596,7 +616,7 @@ func (c *Config) checkValidAuth() error { // // The returned shutdown value can be used to cleanly shut down the // handlers. -func (c *Config) InstallHandlers(hi HandlerInstaller, baseURL string, reindex bool) (shutdown io.Closer, err error) { +func (c *Config) InstallHandlers(hi HandlerInstaller, baseURL string) (shutdown io.Closer, err error) { config := c defer func() { if e := recover(); e != nil { @@ -629,7 +649,6 @@ func (c *Config) InstallHandlers(hi HandlerInstaller, baseURL string, reindex bo baseURL: baseURL, config: make(map[string]*handlerConfig), handler: make(map[string]interface{}), - reindex: reindex, } for prefix, vei := range prefixes { diff --git a/server/perkeepd/perkeepd.go b/server/perkeepd/perkeepd.go index efb070b04..33d69982a 100644 --- a/server/perkeepd/perkeepd.go +++ b/server/perkeepd/perkeepd.go @@ -434,8 +434,10 @@ func Main() { exitf("Error registering challenge client with Perkeep muxer: %v", err) } + config.SetReindex(*flagReindex) + // Finally, install the handlers. This also does the final config validation. - shutdownCloser, err := config.InstallHandlers(ws, baseURL, *flagReindex) + shutdownCloser, err := config.InstallHandlers(ws, baseURL) if err != nil { exitf("Error parsing config: %v", err) }