Skip to content

Commit

Permalink
feat(repositories): add cache size in mon/status (#6288)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux committed Sep 26, 2022
1 parent cd27066 commit 1da97b5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 10 deletions.
1 change: 1 addition & 0 deletions cli/cdsctl/admin.go
Expand Up @@ -16,6 +16,7 @@ func adminCommands() []*cobra.Command {
adminDatabase(),
adminServices(),
adminCdn(),
adminRepositories(),
adminHooks(),
adminOrganization(),
adminIntegrationModels(),
Expand Down
37 changes: 37 additions & 0 deletions cli/cdsctl/admin_repositories.go
@@ -0,0 +1,37 @@
package main

import (
"github.com/spf13/cobra"

"github.com/ovh/cds/cli"
"github.com/ovh/cds/sdk"
)

var adminRepositoriesCmd = cli.Command{
Name: "repositories",
Short: "Manage CDS repositories uService",
}

func adminRepositories() *cobra.Command {
return cli.NewCommand(adminRepositoriesCmd, nil, []*cobra.Command{
cli.NewListCommand(adminRepositorisStatusCmd, adminRepositorisStatusRun, nil),
})
}

func adminRepositorisStatusRun(_ cli.Values) (cli.ListResult, error) {
services, err := client.ServicesByType(sdk.TypeRepositories)
if err != nil {
return nil, err
}
status := sdk.MonitoringStatus{}
for _, srv := range services {
status.Lines = append(status.Lines, srv.MonitoringStatus.Lines...)
}
return cli.AsListResult(status.Lines), nil
}

var adminRepositorisStatusCmd = cli.Command{
Name: "status",
Short: "display the status of repositories",
Example: "cdsctl admin repositories status",
}
29 changes: 29 additions & 0 deletions engine/repositories/fs.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/rockbears/log"

Expand Down Expand Up @@ -42,3 +43,31 @@ func (s *Service) cleanFS(ctx context.Context, r *sdk.OperationRepo) error {
log.Info(ctx, "cleaning operation basedir: %v", r.Basedir)
return sdk.WithStack(os.RemoveAll(r.Basedir))
}

func (s *Service) computeCacheSize(ctx context.Context) error {
tick := time.NewTicker(5 * time.Minute)

defer tick.Stop()
for {
select {
case <-tick.C:
var size int64
err := filepath.Walk(s.Cfg.Basedir, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
if err != nil {
log.ErrorWithStackTrace(ctx, sdk.WrapError(err, "unable to compute size"))
continue
}
s.cacheSize = size
case <-ctx.Done():
return ctx.Err()
}
}
}
19 changes: 13 additions & 6 deletions engine/repositories/repositories.go
Expand Up @@ -106,18 +106,25 @@ func (s *Service) Serve(c context.Context) error {
}

log.Info(ctx, "Initializing processor...")
go func() {
s.GoRoutines.RunWithRestart(ctx, "processor", func(ctx context.Context) {
if err := s.processor(ctx); err != nil {
log.Info(ctx, "Shutdown processor")
log.ErrorWithStackTrace(ctx, err)
}
}()
})

log.Info(ctx, "Initializing vacuumCleaner...")
go func() {
s.GoRoutines.RunWithRestart(ctx, "vacuumCleaner", func(ctx context.Context) {
if err := s.vacuumCleaner(ctx); err != nil {
log.Info(ctx, "Shutdown vacuumCleaner")
log.ErrorWithStackTrace(ctx, err)
}
}()
})

log.Info(ctx, "Initializing cache size...")
s.GoRoutines.RunWithRestart(ctx, "computeCacheSize", func(ctx context.Context) {
if err := s.computeCacheSize(ctx); err != nil {
log.ErrorWithStackTrace(ctx, err)
}
})

//Gracefully shutdown the http server
go func() {
Expand Down
7 changes: 7 additions & 0 deletions engine/repositories/repositories_handlers.go
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -133,6 +134,12 @@ func (s *Service) getOperationsHandler() service.Handler {
// Status returns sdk.MonitoringStatus, implements interface service.Service
func (s *Service) Status(ctx context.Context) *sdk.MonitoringStatus {
m := s.NewMonitoringStatus()

m.Lines = append(m.Lines, sdk.MonitoringStatusLine{
Component: "Cache size",
Value: fmt.Sprintf("%d octets", s.cacheSize),
Status: sdk.MonitoringStatusOK,
})
return m
}

Expand Down
9 changes: 5 additions & 4 deletions engine/repositories/types.go
Expand Up @@ -13,10 +13,11 @@ import (
// Service is the repostories service
type Service struct {
service.Common
Cfg Configuration
Router *api.Router
Cache cache.Store
dao dao
Cfg Configuration
Router *api.Router
Cache cache.Store
dao dao
cacheSize int64
}

// Configuration is the vcs configuration structure
Expand Down

0 comments on commit 1da97b5

Please sign in to comment.