Skip to content

Commit

Permalink
fix: sync checks for canaries added from ui
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Jul 26, 2023
1 parent ac227af commit 103a59f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
39 changes: 24 additions & 15 deletions pkg/db/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,9 @@ func CreateCheck(canary pkg.Canary, check *pkg.Check) error {
return Gorm.Create(&check).Error
}

func PersistCanary(canary v1.Canary, source string) (*pkg.Canary, map[string]string, bool, error) {
func PersistCanaryModel(model pkg.Canary) (*pkg.Canary, map[string]string, bool, error) {
var err error
changed := false
model, err := pkg.CanaryFromV1(canary)
if err != nil {
return nil, nil, changed, err
}
if canary.GetPersistedID() != "" {
model.ID, _ = uuid.Parse(canary.GetPersistedID())
}
model.Source = source
tx := Gorm.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "agent_id"}, {Name: "name"}, {Name: "namespace"}, {Name: "source"}},
UpdateAll: true,
Expand Down Expand Up @@ -333,15 +326,15 @@ func PersistCanary(canary v1.Canary, source string) (*pkg.Canary, map[string]str
return nil, nil, changed, err
}

var spec v1.CanarySpec
if err = json.Unmarshal(model.Spec, &spec); err != nil {
return nil, nil, changed, err
}

var checks = make(map[string]string)
var newCheckIDs []string
for _, config := range canary.Spec.GetAllChecks() {
for _, config := range spec.GetAllChecks() {
check := pkg.FromExternalCheck(model, config)
// not creating the new check if already exists in the status
// status is not patched correctly with the status id
if checkID := canary.GetCheckID(check.Name); checkID != "" {
check.ID, _ = uuid.Parse(checkID)
}
check.Spec, _ = json.Marshal(config)
id, err := PersistCheck(check, model.ID)
if err != nil {
Expand All @@ -361,9 +354,25 @@ func PersistCanary(canary v1.Canary, source string) (*pkg.Canary, map[string]str
}
metrics.UnregisterGauge(checkIDsToRemove)
}

model.Checks = checks
return &model, checks, changed, nil
}

func PersistCanary(canary v1.Canary, source string) (*pkg.Canary, map[string]string, bool, error) {
changed := false
model, err := pkg.CanaryFromV1(canary)
if err != nil {
return nil, nil, changed, err
}
if canary.GetPersistedID() != "" {
model.ID, _ = uuid.Parse(canary.GetPersistedID())
}
model.Source = source

return PersistCanaryModel(model)
}

func RefreshCheckStatusSummary() {
if err := duty.RefreshCheckStatusSummary(Pool); err != nil {
logger.Errorf("error refreshing check_status_summary materialized view: %v", err)
Expand Down
13 changes: 13 additions & 0 deletions pkg/jobs/canary/canary_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ func SyncCanaryJobs() {
jobHistory := models.NewJobHistory("CanarySync", "canary", c.ID.String()).Start()
_ = db.PersistJobHistory(jobHistory)

// A canary can exist whilst not having any checks in database
// We persist the canary again for those cases to populate the checks
if c.Checks == nil {
model, _, _, err := db.PersistCanaryModel(c)
if err != nil {
logger.Errorf("Error persisting canary[%s]: %v", c.ID, err.Error())
_ = db.PersistJobHistory(jobHistory.AddError(err.Error()).End())
continue

Check failure on line 355 in pkg/jobs/canary/canary_jobs.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}
c = *model
}

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 Down

0 comments on commit 103a59f

Please sign in to comment.