Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add remove old stations #1038

Merged
merged 8 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,35 @@ func RemoveSchemaFromAllUsingStations(schemaName string, tenantName string) erro
return nil
}

func GetIsNotActiveStations() ([]models.Station, error) {
shohamroditimemphis marked this conversation as resolved.
Show resolved Hide resolved
ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second)
defer cancelfunc()
conn, err := MetadataDbClient.Client.Acquire(ctx)
if err != nil {
return []models.Station{}, err
}
defer conn.Release()
query := `SELECT * FROM stations WHERE is_deleted = true`
stmt, err := conn.Conn().Prepare(ctx, "get_not_active_stations", query)
if err != nil {
return []models.Station{}, err
}
rows, err := conn.Conn().Query(ctx, stmt.Name)
if err != nil {
return []models.Station{}, err
}
defer rows.Close()
stations, err := pgx.CollectRows(rows, pgx.RowToStructByPos[models.Station])
if err != nil {
return []models.Station{}, err
}
if len(stations) == 0 {
return []models.Station{}, err
}
return stations, nil

}

// Producer Functions
func GetProducersByConnectionIDWithStationDetails(connectionId string) ([]models.LightProducer, error) {
ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second)
Expand Down
21 changes: 21 additions & 0 deletions server/memphis_handlers_stations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2165,3 +2165,24 @@ func getUserAndTenantIdFromString(username string) (string, int, error) {
return username, -1, nil

}

func (s *Server) RemoveOldStations() error {
stations, err := db.GetIsNotActiveStations()
if err != nil {
return err
}

for _, station := range stations {
err = removeStationResources(s, station, true)
if err != nil {
return err
}

err = db.DeleteStation(station.Name, station.TenantName)
shohamroditimemphis marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
}

return nil
}
10 changes: 8 additions & 2 deletions server/memphis_zombie_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,6 @@ func killFunc(s *Server) {
}
}
}

s.removeStaleStations()
}

func (s *Server) KillZombieResources() {
Expand All @@ -180,6 +178,14 @@ func (s *Server) KillZombieResources() {
firstIteration := true
for range time.Tick(time.Minute * 1) {
s.Debugf("Killing Zombie resources iteration")
if firstIteration {
s.removeStaleStations()
err := s.RemoveOldStations()
if err != nil {
serv.Errorf("KillZombieResources: RemoveOldStations: %v", err.Error())
return
shohamroditimemphis marked this conversation as resolved.
Show resolved Hide resolved
}
}
killFunc(s)

if firstIteration || count == 1*60 { // once in 1 hour
Expand Down