From 8a98e017f9e4a4e610371851bf68ef11daed0ad6 Mon Sep 17 00:00:00 2001 From: Yash Mehrotra Date: Sun, 13 Aug 2023 09:28:43 +0530 Subject: [PATCH] fix: remove deleted canaries from cron --- pkg/db/topology.go | 11 +++++++---- pkg/jobs/canary/canary_jobs.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pkg/db/topology.go b/pkg/db/topology.go index 4c215cded..e201b152b 100644 --- a/pkg/db/topology.go +++ b/pkg/db/topology.go @@ -27,7 +27,7 @@ func PersistTopology(t *v1.Topology) (bool, error) { return changed, err } tx := Gorm.Table("topologies").Clauses(clause.OnConflict{ - Columns: []clause.Column{{Name: "name"}, {Name: "namespace"}}, + Columns: []clause.Column{{Name: "agent_id"}, {Name: "name"}, {Name: "namespace"}}, UpdateAll: true, }).Create(model) if tx.Error != nil { @@ -212,11 +212,14 @@ func GetChildRelationshipsForParentComponent(componentID uuid.UUID) ([]pkg.Compo } func PersistComponentRelationships(relationships []*pkg.ComponentRelationship) error { - tx := Gorm.Clauses(clause.OnConflict{ + if len(relationships) == 0 { + return nil + } + + return Gorm.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "component_id"}, {Name: "relationship_id"}, {Name: "selector_id"}}, UpdateAll: true, - }).Create(relationships) - return tx.Error + }).Create(relationships).Error } func GetCheckRelationshipsForComponent(componentID uuid.UUID) ([]pkg.CheckComponentRelationship, error) { diff --git a/pkg/jobs/canary/canary_jobs.go b/pkg/jobs/canary/canary_jobs.go index 5dc1b4e4b..f089f433a 100644 --- a/pkg/jobs/canary/canary_jobs.go +++ b/pkg/jobs/canary/canary_jobs.go @@ -256,6 +256,14 @@ func findCronEntry(id string) *cron.Entry { return nil } +func getAllCanaryIDsInCron() []string { + var ids []string + for _, entry := range CanaryScheduler.Entries() { + ids = append(ids, entry.Job.(CanaryJob).DBCanary.ID.String()) + } + return ids +} + func ScanCanaryConfigs() { logger.Infof("Syncing canary specs: %v", CanaryConfigFiles) for _, configfile := range CanaryConfigFiles { @@ -342,10 +350,13 @@ func SyncCanaryJobs() { return } + existingIDsInCron := getAllCanaryIDsInCron() + var idsInNewFetch []string for _, c := range canaries { jobHistory := models.NewJobHistory("CanarySync", "canary", c.ID.String()).Start() _ = db.PersistJobHistory(jobHistory) + idsInNewFetch = append(idsInNewFetch, c.ID.String()) if err := SyncCanaryJob(c); err != nil { logger.Errorf("Error syncing canary[%s]: %v", c.ID, err.Error()) _ = db.PersistJobHistory(jobHistory.AddError(err.Error()).End()) @@ -355,6 +366,11 @@ func SyncCanaryJobs() { _ = db.PersistJobHistory(jobHistory.End()) } + idsToRemoveFromCron := utils.SetDifference(existingIDsInCron, idsInNewFetch) + for _, id := range idsToRemoveFromCron { + DeleteCanaryJob(id) + } + logger.Infof("Synced canary jobs %d", len(CanaryScheduler.Entries())) }