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

fix: cron flow for canaries #1109

Merged
merged 6 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 1 addition & 1 deletion checks/runchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func RunChecks(ctx *context.Context) []*pkg.CheckResult {
var results []*pkg.CheckResult

// Check if canary is not marked deleted in DB
if db.Gorm != nil {
if db.Gorm != nil && ctx.Canary.GetPersistedID() != "" {
var deletedAt sql.NullTime
err := db.Gorm.Table("canaries").Select("deleted_at").Where("id = ?", ctx.Canary.GetPersistedID()).Scan(&deletedAt).Error
if err == nil && deletedAt.Valid {
Expand Down
36 changes: 25 additions & 11 deletions pkg/jobs/canary/canary_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,11 @@ func ScanCanaryConfigs() {
}
}

var canaryUpdateTimeCache = make(map[string]time.Time)

// TODO: Refactor to use database object instead of kubernetes
func SyncCanaryJob(canary v1.Canary) error {
if !canary.DeletionTimestamp.IsZero() || canary.Spec.GetSchedule() == "@never" { //nolint:goconst
if !canary.DeletionTimestamp.IsZero() || canary.Spec.GetSchedule() == "@never" {
DeleteCanaryJob(canary)
return nil
}
Expand All @@ -256,9 +259,9 @@ func SyncCanaryJob(canary v1.Canary) error {
}
}

entry := findCronEntry(canary)
if entry != nil {
CanaryScheduler.Remove(entry.ID)
dbCanary, err := db.GetCanary(canary.GetPersistedID())
if err != nil {
return err
}

job := CanaryJob{
Expand All @@ -269,15 +272,26 @@ func SyncCanaryJob(canary v1.Canary) error {
LogFail: canary.IsTrace() || canary.IsDebug() || LogFail,
}

_, err := CanaryScheduler.AddJob(canary.Spec.GetSchedule(), job)
if err != nil {
return fmt.Errorf("failed to schedule canary %s/%s: %v", canary.Namespace, canary.Name, err)
updateTime, exists := canaryUpdateTimeCache[dbCanary.ID.String()]
entry := findCronEntry(canary)
if !exists || dbCanary.UpdatedAt.After(updateTime) || entry == nil {
// Remove entry if it exists
if entry != nil {
CanaryScheduler.Remove(entry.ID)
}

// Schedule canary for the first time
_, err = CanaryScheduler.AddJob(canary.Spec.GetSchedule(), job)
if err != nil {
return fmt.Errorf("failed to schedule canary %s/%s: %v", canary.Namespace, canary.Name, err)
}
logger.Infof("Scheduled %s: %s", canary, canary.Spec.GetSchedule())

canaryUpdateTimeCache[dbCanary.ID.String()] = dbCanary.UpdatedAt
}
logger.Infof("Scheduled %s: %s", canary, canary.Spec.GetSchedule())

entry = findCronEntry(canary)
if entry != nil && time.Until(entry.Next) < 1*time.Hour {
// run all regular canaries on startup
//Run all regularly scheduled canaries on startup (<1h) and not daily/weekly schedules
if entry != nil && time.Until(entry.Next) < 1*time.Hour && !exists {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this condition ever satisfied? If the cron entry is not nil, then the update time will also exist.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have updated the code to modify entry

job = entry.Job.(CanaryJob)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this line?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, from a ux perspective if I add a check that runs every 15 minutes I don't want to wait for 15m to see if it worked

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moshloop I meant just this one line

job = entry.Job.(CanaryJob)

go job.Run()
}
Expand Down
Loading