diff --git a/README.md b/README.md index 942db40d5..f406cf6ac 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,11 @@ If you want to change the default port(9999): ---- ### Useful tools for debug +#### Debug server +Use env var ANYDEBUG=address to enable debugging HTTP server. For example: `ANYDEBUG=:6061` will start debug server on port 6061 + +You can find all endpoints in `/debug` page. For example: http://localhost:6061/debug + #### gRPC logging In order to log mw gRPC requests/responses use `ANYTYPE_GRPC_LOG` env var: - `ANYTYPE_LOG_LEVEL="grpc=DEBUG" ANYTYPE_GRPC_LOG=1` - log only method names diff --git a/core/debug/service.go b/core/debug/service.go index e9a3cbd0e..29bb2abf9 100644 --- a/core/debug/service.go +++ b/core/debug/service.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "os" + "path" "path/filepath" "strings" "time" @@ -64,6 +65,13 @@ func (d *debug) Init(a *app.App) (err error) { r.Route("/debug/"+c.Name(), d.DebugRouter) } }) + routes := r.Routes() + r.Get("/debug", func(w http.ResponseWriter, req *http.Request) { + err := renderLinksList(w, "/", routes) + if err != nil { + logger.Error("failed to render links list", err) + } + }) d.server = &http.Server{ Addr: addr, Handler: r, @@ -72,6 +80,28 @@ func (d *debug) Init(a *app.App) (err error) { return nil } +func joinPath(parent string, child string) string { + parent = strings.TrimSuffix(parent, "/*") + return path.Join(parent, child) +} + +func renderLinksList(w io.Writer, path string, routes []chi.Route) error { + for _, r := range routes { + if r.SubRoutes != nil { + err := renderLinksList(w, joinPath(path, r.Pattern), r.SubRoutes.Routes()) + if err != nil { + return err + } + } else { + _, err := fmt.Fprintf(w, `%s
`, joinPath(path, r.Pattern), joinPath(path, r.Pattern)) + if err != nil { + return err + } + } + } + return nil +} + func (d *debug) Run(ctx context.Context) error { if d.server != nil { go func() { diff --git a/core/files/debug.go b/core/files/debug.go index 43f2ef533..47cdb4512 100644 --- a/core/files/debug.go +++ b/core/files/debug.go @@ -2,6 +2,7 @@ package files import ( "context" + "errors" "fmt" "io" "net/http" @@ -12,10 +13,11 @@ import ( "github.com/anyproto/anytype-heart/pkg/lib/localstore" "github.com/anyproto/anytype-heart/util/debug" + "github.com/anyproto/anytype-heart/util/pbtypes" ) func (s *service) DebugRouter(r chi.Router) { - r.Get("/syncstatus", debug.JSONHandler(s.debugFiles)) + r.Get("/status", debug.JSONHandler(s.debugFiles)) r.Get("/queue", debug.JSONHandler(s.fileSync.DebugQueue)) r.Get("/tree/{rootID}", debug.PlaintextHandler(s.printTree)) } @@ -23,6 +25,7 @@ func (s *service) DebugRouter(r chi.Router) { type fileDebugInfo struct { Hash string SyncStatus int + IsIndexed bool } func (s *service) debugFiles(_ *http.Request) ([]*fileDebugInfo, error) { @@ -40,9 +43,20 @@ func (s *service) debugFiles(_ *http.Request) ([]*fileDebugInfo, error) { if err != nil { return nil, fmt.Errorf("get status for %s: %s", hash, err) } + + var isIndexed bool + details, err := s.objectStore.GetDetails(hash) + if err != nil && !errors.Is(err, localstore.ErrNotFound) { + return nil, fmt.Errorf("get status for %s: %s", hash, err) + } + if details != nil && !pbtypes.IsStructEmpty(details.Details) { + isIndexed = true + } + result = append(result, &fileDebugInfo{ Hash: hash, SyncStatus: status, + IsIndexed: isIndexed, }) } return result, nil diff --git a/core/files/files.go b/core/files/files.go index 3c68d4650..fe8abb192 100644 --- a/core/files/files.go +++ b/core/files/files.go @@ -880,6 +880,7 @@ func (s *service) FileByHash(ctx context.Context, hash string) (File, error) { return nil, fmt.Errorf("check if file is imported: %w", err) } if ok { + log.With("fileID", hash).Warn("file is imported, push it to uploading queue") // If file is imported we have to sync it, so we don't set sync status to synced err = s.fileStore.SetIsFileImported(hash, false) if err != nil {