Files
Paul FaureandClaude Opus 5.5 b5f843e99b feat(search): add a --timeout flag to the index command (#13053)
* feat(search): add a --timeout flag to the index command

ocis search index called the search service with a hard-coded 10 minute
request timeout. The service walks the whole space inside that request, so
when the timeout expired the walk was cancelled (error walking the tree:
context canceled) and everything after that point stayed unindexed. A space
whose walk takes longer than ten minutes could never be fully indexed from
the command line.

The timeout is now a flag: --timeout takes a duration, default 10m so the
behaviour is unchanged out of the box; 0 waits indefinitely (go-micro treats
a zero RequestTimeout as an already-expired deadline, so a far-off one is
substituted).

Fixes #13052

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Paul Faure <paul@faure.ca>

* docs(search): say the index command needs the running search service

The index command is a gRPC client of the search service and never opens
the Bleve index, so it works with the default exclusive lock
(SEARCH_ENGINE_BLEVE_SCALE=false) but needs the service running.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WpoeSoKYEdgQcWjEzTVKpT
Signed-off-by: Paul Faure <paul@faure.ca>

---------

Signed-off-by: Paul Faure <paul@faure.ca>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-09 09:54:38 +02:00

90 lines
2.8 KiB
Go

package command
import (
"context"
"errors"
"fmt"
"time"
"github.com/urfave/cli/v2"
"go-micro.dev/v4/client"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/ocis-pkg/service/grpc"
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
searchsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0"
"github.com/owncloud/ocis/v2/services/search/pkg/config"
"github.com/owncloud/ocis/v2/services/search/pkg/config/parser"
)
// _noTimeout stands in for "no limit" when --timeout is 0: go-micro treats a
// zero RequestTimeout as an immediately expired deadline, so a far-off one is
// used instead.
const _noTimeout = 100 * 365 * 24 * time.Hour
// Index is the entrypoint for the index command.
func Index(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "index",
Usage: "index the files for one one more users",
Category: "index management",
Aliases: []string{"i"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "space",
Aliases: []string{"s"},
Usage: "the id of the space to travers and index the files of. This or --all-spaces is required.",
},
&cli.BoolFlag{
Name: "all-spaces",
Usage: "index all spaces instead. This or --space is required.",
},
&cli.DurationFlag{
Name: "timeout",
Usage: "how long to wait for the indexing to finish before giving up, e.g. 2h. The walk is cancelled when the timeout expires. 0 waits indefinitely.",
Value: 10 * time.Minute,
},
},
Before: func(_ *cli.Context) error {
return configlog.ReturnFatal(parser.ParseConfig(cfg))
},
Action: func(ctx *cli.Context) error {
if ctx.String("space") == "" && !ctx.Bool("all-spaces") {
return errors.New("either --space or --all-spaces is required")
}
traceProvider, err := tracing.GetServiceTraceProvider(cfg.Tracing, cfg.Service.Name)
if err != nil {
return err
}
grpcClient, err := grpc.NewClient(
append(grpc.GetClientOptions(cfg.GRPCClientTLS),
grpc.WithKeepaliveParams(cfg.GRPCClientOptions),
grpc.WithTraceProvider(traceProvider),
)...,
)
if err != nil {
return err
}
// The service walks the whole space inside this request, so the
// request timeout bounds the walk: when it expires the walk is
// cancelled and everything after that point stays unindexed.
timeout := ctx.Duration("timeout")
if timeout <= 0 {
timeout = _noTimeout
}
c := searchsvc.NewSearchProviderService("com.owncloud.api.search", grpcClient)
_, err = c.IndexSpace(context.Background(), &searchsvc.IndexSpaceRequest{
SpaceId: ctx.String("space"),
}, func(opts *client.CallOptions) { opts.RequestTimeout = timeout })
if err != nil {
fmt.Println("failed to index space: " + err.Error())
return err
}
return nil
},
}
}