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

feat: wait for checks before shutdown #2293

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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
4 changes: 4 additions & 0 deletions cmd/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func run() error {
// so we use a goroutine to unblock server start
// to prevent health check from failing
go jobs.Start()

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

go serve()
Expand Down
4 changes: 4 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ var Serve = &cobra.Command{
canaryJobs.StartScanCanaryConfigs(apicontext.DefaultContext, dataFile, configFiles)
if executor {
jobs.Start()

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

serve()
Expand Down
28 changes: 28 additions & 0 deletions pkg/jobs/canary/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,31 @@ 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")
if err := globalCheckSemaphore.Acquire(ctx, int64(ctx.Properties().Int(propertyCheckConcurrency, defaultCheckConcurrency))); err != nil {
ctx.Logger.Errorf("failed to acquire check semaphores: %v", err)
}
ctx.Logger.V(6).Infof("acquired all check locks")
}

var canaryJobs sync.Map

const DefaultCanarySchedule = "@every 5m"
Expand Down Expand Up @@ -140,6 +163,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 +183,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
Loading