Skip to content

Commit

Permalink
feat: wait for checks before shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Oct 25, 2024
1 parent d85f05b commit 5fb03bf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions canary-checker.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

# topology.runNow=true
log.level.db=warn
# check.concurrency=100

# jobs.ComponentRelationshipSync.runNow=true
7 changes: 7 additions & 0 deletions cmd/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
apicontext "github.com/flanksource/canary-checker/api/context"
"github.com/flanksource/canary-checker/pkg/cache"
"github.com/flanksource/canary-checker/pkg/jobs"
"github.com/flanksource/canary-checker/pkg/jobs/canary"

Check failure on line 11 in cmd/operator.go

View workflow job for this annotation

GitHub Actions / lint

ST1019: package "github.com/flanksource/canary-checker/pkg/jobs/canary" is being imported more than once (stylecheck)
canaryJobs "github.com/flanksource/canary-checker/pkg/jobs/canary"

Check failure on line 12 in cmd/operator.go

View workflow job for this annotation

GitHub Actions / lint

ST1019(related information): other import of "github.com/flanksource/canary-checker/pkg/jobs/canary" (stylecheck)
"github.com/flanksource/canary-checker/pkg/runner"
"github.com/flanksource/canary-checker/pkg/utils"
Expand Down Expand Up @@ -96,6 +97,12 @@ func run() error {
// so we use a goroutine to unblock server start
// to prevent health check from failing
go jobs.Start()

// TODO: stop the cron scheduler so that no more checks are scheduled

shutdown.AddHookWithPriority("check jobs", shutdown.PriorityJobs, func() {
canary.AcquireAllCheckLocks(ctx)
})
}

go serve()
Expand Down
7 changes: 7 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/canary-checker/pkg/echo"
"github.com/flanksource/canary-checker/pkg/jobs"
"github.com/flanksource/canary-checker/pkg/jobs/canary"

Check failure on line 19 in cmd/serve.go

View workflow job for this annotation

GitHub Actions / lint

ST1019: package "github.com/flanksource/canary-checker/pkg/jobs/canary" is being imported more than once (stylecheck)
canaryJobs "github.com/flanksource/canary-checker/pkg/jobs/canary"

Check failure on line 20 in cmd/serve.go

View workflow job for this annotation

GitHub Actions / lint

ST1019(related information): other import of "github.com/flanksource/canary-checker/pkg/jobs/canary" (stylecheck)
echov4 "github.com/labstack/echo/v4"

Expand Down Expand Up @@ -49,6 +50,12 @@ var Serve = &cobra.Command{
canaryJobs.StartScanCanaryConfigs(apicontext.DefaultContext, dataFile, configFiles)
if executor {
jobs.Start()

// TODO: stop the cron scheduler so that no more checks are scheduled

shutdown.AddHookWithPriority("check jobs", shutdown.PriorityJobs, func() {
canary.AcquireAllCheckLocks(apicontext.DefaultContext)
})
}

serve()
Expand Down
26 changes: 26 additions & 0 deletions pkg/jobs/canary/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,29 @@ import (
"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"
"github.com/robfig/cron/v3"
"golang.org/x/sync/semaphore"
)

const propertyCheckConcurrency = "check.concurrency"

var (
// The maximum number of checks that can run concurrently
defaultCheckConcurrency = 50

// Holds in the lock for every running check.
// Can be overwritten by 'check.concurrency' property.
globalCheckSemaphore *semaphore.Weighted
)

// AcquireAllCheckLocks blocks until the global check sempahore is fully acquired.
//
// This helps to ensure that no checks are currently running.
func AcquireAllCheckLocks(ctx context.Context) {
ctx.Logger.V(6).Infof("acquiring all check locks")
globalCheckSemaphore.Acquire(ctx, int64(ctx.Properties().Int(propertyCheckConcurrency, defaultCheckConcurrency)))

Check failure on line 41 in pkg/jobs/canary/sync.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `globalCheckSemaphore.Acquire` is not checked (errcheck)
ctx.Logger.V(6).Infof("acquired all check locks")
}

var canaryJobs sync.Map

const DefaultCanarySchedule = "@every 5m"
Expand Down Expand Up @@ -140,6 +161,7 @@ func newCanaryJob(c CanaryJob) {
IgnoreSuccessHistory: true,
Retention: job.RetentionBalanced,
ResourceID: c.DBCanary.ID.String(),
Semaphores: []*semaphore.Weighted{globalCheckSemaphore},
ResourceType: "canary",
ID: fmt.Sprintf("%s/%s", c.Canary.Namespace, c.Canary.Name),
Fn: c.Run,
Expand All @@ -159,6 +181,10 @@ var SyncCanaryJobs = &job.Job{
Schedule: "@every 5m",
Retention: job.RetentionFew,
Fn: func(ctx job.JobRuntime) error {
if globalCheckSemaphore == nil {
globalCheckSemaphore = semaphore.NewWeighted(int64(ctx.Properties().Int(propertyCheckConcurrency, defaultCheckConcurrency)))
}

canaries, err := db.GetAllCanariesForSync(ctx.Context, runner.WatchNamespace)
if err != nil {
return err
Expand Down

0 comments on commit 5fb03bf

Please sign in to comment.