feat: [CDE-202]: add(git instance store): add methods to get dead gitinstances and bulk update state (#2774)

* update where clause
* fix sql query
* update comment
* fix lint
* add function to find dead gitinstances
pull/3571/head
Deepak Bhatt 2024-10-07 11:27:09 +00:00 committed by Harness
parent f1efecefee
commit f62a6e5e68
5 changed files with 61 additions and 17 deletions

View File

@ -676,9 +676,15 @@ type (
// Update tries to update a gitspace instance in the datastore with optimistic locking.
Update(ctx context.Context, gitspaceInstance *types.GitspaceInstance) error
// BulkUpdateState updates state of given gitspace instance IDs to given state
BulkUpdateState(ctx context.Context, state enum.GitspaceInstanceStateType, gitspaceInstanceIDs []int64) error
// List lists the gitspace instance present in a parent space ID in the datastore.
List(ctx context.Context, filter *types.GitspaceFilter) ([]*types.GitspaceInstance, error)
// ListDead lists dead gitspace instances whose heartbeat stopped coming after the given time.
ListDead(ctx context.Context, filter *types.GitspaceFilter) (gitInstanceIDs []int64, err error)
// FetchInactiveGitspaceConfigs lists the inactive gitspace instance present in the datastore
FetchInactiveGitspaceConfigs(ctx context.Context, filter *types.GitspaceFilter) ([]int64, error)

View File

@ -256,6 +256,30 @@ func (g gitspaceInstanceStore) Update(
return nil
}
func (g gitspaceInstanceStore) BulkUpdateState(
ctx context.Context,
state enum.GitspaceInstanceStateType,
gitspaceInstanceIDs []int64,
) error {
stmt := database.Builder.
Update(gitspaceInstanceTable).
Set("gits_state", state).
Where(squirrel.Eq{"gits_id": gitspaceInstanceIDs})
sqlStr, args, err := stmt.ToSql()
if err != nil {
return errors.Wrap(err, "Failed to convert squirrel builder to sql")
}
db := dbtx.GetAccessor(ctx, g.db)
if _, err = db.ExecContext(ctx, sqlStr, args...); err != nil {
return database.ProcessSQLErrorf(ctx, err,
"Failed to update gitspace instances for %v", gitspaceInstanceIDs)
}
return nil
}
func (g gitspaceInstanceStore) FindLatestByGitspaceConfigID(
ctx context.Context,
gitspaceConfigID int64,
@ -304,6 +328,29 @@ func (g gitspaceInstanceStore) List(
return g.mapToGitspaceInstances(ctx, dst)
}
func (g gitspaceInstanceStore) ListDead(
ctx context.Context,
filter *types.GitspaceFilter,
) (gitInstanceIDs []int64, err error) {
stmt := database.Builder.
Select("gits_id").
From(gitspaceInstanceTable).
Where(squirrel.Lt{"gits_last_heartbeat": filter.LastHeartBeatBefore}).
Where(squirrel.Eq{"gits_state": filter.State}).
OrderBy("gits_created ASC")
sqlStr, args, err := stmt.ToSql()
if err != nil {
return nil, errors.Wrap(err, "Failed to convert squirrel builder to sql")
}
db := dbtx.GetAccessor(ctx, g.db)
if err = db.SelectContext(ctx, &gitInstanceIDs, sqlStr, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed executing gitspace instance list query")
}
return gitInstanceIDs, nil
}
func (g gitspaceInstanceStore) FetchInactiveGitspaceConfigs(
ctx context.Context,
filter *types.GitspaceFilter,

2
go.mod
View File

@ -125,7 +125,6 @@ require (
github.com/gorilla/websocket v1.4.2 // indirect
github.com/h2non/filetype v1.1.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
@ -154,7 +153,6 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect

8
go.sum
View File

@ -120,7 +120,6 @@ github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -396,8 +395,6 @@ github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6
github.com/iancoleman/orderedmap v0.2.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
@ -683,7 +680,6 @@ github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThC
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk=
github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0=
@ -718,8 +714,6 @@ github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNo
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
@ -788,8 +782,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
github.com/zricethezav/gitleaks/v8 v8.18.5-0.20240614204812-26f34692fac6 h1:UL8vBvxILAVsruyxIGMskACYzOk57nR8aq6dpZLR3KQ=
github.com/zricethezav/gitleaks/v8 v8.18.5-0.20240614204812-26f34692fac6/go.mod h1:3EFYK+ZNDHPNQinyZTVGHG7/sFsApEZ9DrCGA1AP63M=
github.com/zricethezav/gitleaks/v8 v8.18.5-0.20240912004812-e93a7c0d2604 h1:lR3oEmvayjHikZppbVZY5Zsrw7FA1QvZuP6O7uyFK4k=
github.com/zricethezav/gitleaks/v8 v8.18.5-0.20240912004812-e93a7c0d2604/go.mod h1:3EFYK+ZNDHPNQinyZTVGHG7/sFsApEZ9DrCGA1AP63M=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=

View File

@ -79,11 +79,12 @@ type GitspaceInstance struct {
}
type GitspaceFilter struct {
QueryFilter ListQueryFilter
UserID string
LastUsedBefore int64
State []enum.GitspaceInstanceStateType
SpaceIDs []int64
IncludeDeleted bool
Limit int
QueryFilter ListQueryFilter
UserID string
LastUsedBefore int64
LastHeartBeatBefore int64
State []enum.GitspaceInstanceStateType
SpaceIDs []int64
IncludeDeleted bool
Limit int
}