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: only poll Semaphore API if there is space for new jobs #10

Merged
merged 1 commit into from
Jan 24, 2024
Merged
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
7 changes: 6 additions & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ func (c *Controller) tick(ctx context.Context) bool {
return true
}

klog.Infof("Polling job queue for %v", agentTypes)
if !c.jobScheduler.HasSpace() {
klog.Info("Not polling Semaphore API - no space")
return true
}

klog.InfoS("Polling Semaphore API", "types", agentTypes)
jobs, err := c.semaphoreClient.JobsFor(agentTypes)
if err != nil {
klog.Error(err, "error polling job queue")
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/job_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (s *JobScheduler) RegisterInformer(informerFactory informers.SharedInformer
return err
}

func (s *JobScheduler) HasSpace() bool {
return len(s.current) < s.config.MaxParallelJobs
}

func (s *JobScheduler) Create(ctx context.Context, req semaphore.JobRequest, agentType *agenttypes.AgentType) error {
s.mu.Lock()
defer s.mu.Unlock()
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/job_scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Test__JobScheduler(t *testing.T) {
defer clear(scheduler.current)

// create jobs up to max
require.True(t, scheduler.HasSpace())
for i := 0; i < maxParallelJobs; i++ {
jobID := randJobID()
req := semaphore.JobRequest{JobID: jobID, MachineType: agentType.AgentTypeName}
Expand All @@ -86,6 +87,9 @@ func Test__JobScheduler(t *testing.T) {
require.True(t, scheduler.JobExists(jobID))
}

// no more space available
require.False(t, scheduler.HasSpace())

// creating a job returns an error now
jobID := randJobID()
req := semaphore.JobRequest{JobID: jobID, MachineType: agentType.AgentTypeName}
Expand Down