Skip to content

Commit

Permalink
Merge pull request #1219 from flanksource/db-fixes-
Browse files Browse the repository at this point in the history
fix: remove deleted canaries from cron
  • Loading branch information
moshloop authored Aug 15, 2023
2 parents d2b966e + 8a98e01 commit 173f5b5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/db/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions pkg/jobs/canary/canary_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand All @@ -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()))
}

Expand Down

0 comments on commit 173f5b5

Please sign in to comment.