Skip to content

Commit 1da97b5

Browse files
authored
feat(repositories): add cache size in mon/status (#6288)
1 parent cd27066 commit 1da97b5

File tree

6 files changed

+92
-10
lines changed

6 files changed

+92
-10
lines changed

cli/cdsctl/admin.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func adminCommands() []*cobra.Command {
1616
adminDatabase(),
1717
adminServices(),
1818
adminCdn(),
19+
adminRepositories(),
1920
adminHooks(),
2021
adminOrganization(),
2122
adminIntegrationModels(),

cli/cdsctl/admin_repositories.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/ovh/cds/cli"
7+
"github.com/ovh/cds/sdk"
8+
)
9+
10+
var adminRepositoriesCmd = cli.Command{
11+
Name: "repositories",
12+
Short: "Manage CDS repositories uService",
13+
}
14+
15+
func adminRepositories() *cobra.Command {
16+
return cli.NewCommand(adminRepositoriesCmd, nil, []*cobra.Command{
17+
cli.NewListCommand(adminRepositorisStatusCmd, adminRepositorisStatusRun, nil),
18+
})
19+
}
20+
21+
func adminRepositorisStatusRun(_ cli.Values) (cli.ListResult, error) {
22+
services, err := client.ServicesByType(sdk.TypeRepositories)
23+
if err != nil {
24+
return nil, err
25+
}
26+
status := sdk.MonitoringStatus{}
27+
for _, srv := range services {
28+
status.Lines = append(status.Lines, srv.MonitoringStatus.Lines...)
29+
}
30+
return cli.AsListResult(status.Lines), nil
31+
}
32+
33+
var adminRepositorisStatusCmd = cli.Command{
34+
Name: "status",
35+
Short: "display the status of repositories",
36+
Example: "cdsctl admin repositories status",
37+
}

engine/repositories/fs.go

+29
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"time"
89

910
"github.com/rockbears/log"
1011

@@ -42,3 +43,31 @@ func (s *Service) cleanFS(ctx context.Context, r *sdk.OperationRepo) error {
4243
log.Info(ctx, "cleaning operation basedir: %v", r.Basedir)
4344
return sdk.WithStack(os.RemoveAll(r.Basedir))
4445
}
46+
47+
func (s *Service) computeCacheSize(ctx context.Context) error {
48+
tick := time.NewTicker(5 * time.Minute)
49+
50+
defer tick.Stop()
51+
for {
52+
select {
53+
case <-tick.C:
54+
var size int64
55+
err := filepath.Walk(s.Cfg.Basedir, func(_ string, info os.FileInfo, err error) error {
56+
if err != nil {
57+
return err
58+
}
59+
if !info.IsDir() {
60+
size += info.Size()
61+
}
62+
return err
63+
})
64+
if err != nil {
65+
log.ErrorWithStackTrace(ctx, sdk.WrapError(err, "unable to compute size"))
66+
continue
67+
}
68+
s.cacheSize = size
69+
case <-ctx.Done():
70+
return ctx.Err()
71+
}
72+
}
73+
}

engine/repositories/repositories.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,25 @@ func (s *Service) Serve(c context.Context) error {
106106
}
107107

108108
log.Info(ctx, "Initializing processor...")
109-
go func() {
109+
s.GoRoutines.RunWithRestart(ctx, "processor", func(ctx context.Context) {
110110
if err := s.processor(ctx); err != nil {
111-
log.Info(ctx, "Shutdown processor")
111+
log.ErrorWithStackTrace(ctx, err)
112112
}
113-
}()
113+
})
114114

115115
log.Info(ctx, "Initializing vacuumCleaner...")
116-
go func() {
116+
s.GoRoutines.RunWithRestart(ctx, "vacuumCleaner", func(ctx context.Context) {
117117
if err := s.vacuumCleaner(ctx); err != nil {
118-
log.Info(ctx, "Shutdown vacuumCleaner")
118+
log.ErrorWithStackTrace(ctx, err)
119119
}
120-
}()
120+
})
121+
122+
log.Info(ctx, "Initializing cache size...")
123+
s.GoRoutines.RunWithRestart(ctx, "computeCacheSize", func(ctx context.Context) {
124+
if err := s.computeCacheSize(ctx); err != nil {
125+
log.ErrorWithStackTrace(ctx, err)
126+
}
127+
})
121128

122129
//Gracefully shutdown the http server
123130
go func() {

engine/repositories/repositories_handlers.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"archive/tar"
55
"bytes"
66
"context"
7+
"fmt"
78
"io"
89
"net/http"
910
"strings"
@@ -133,6 +134,12 @@ func (s *Service) getOperationsHandler() service.Handler {
133134
// Status returns sdk.MonitoringStatus, implements interface service.Service
134135
func (s *Service) Status(ctx context.Context) *sdk.MonitoringStatus {
135136
m := s.NewMonitoringStatus()
137+
138+
m.Lines = append(m.Lines, sdk.MonitoringStatusLine{
139+
Component: "Cache size",
140+
Value: fmt.Sprintf("%d octets", s.cacheSize),
141+
Status: sdk.MonitoringStatusOK,
142+
})
136143
return m
137144
}
138145

engine/repositories/types.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
// Service is the repostories service
1414
type Service struct {
1515
service.Common
16-
Cfg Configuration
17-
Router *api.Router
18-
Cache cache.Store
19-
dao dao
16+
Cfg Configuration
17+
Router *api.Router
18+
Cache cache.Store
19+
dao dao
20+
cacheSize int64
2021
}
2122

2223
// Configuration is the vcs configuration structure

0 commit comments

Comments
 (0)