Change launch logic to receive config in scheduler

This commit is contained in:
2022-12-24 00:10:06 -07:00
parent b29d3a8f80
commit c731e560af
5 changed files with 59 additions and 70 deletions
+39 -43
View File
@@ -37,12 +37,12 @@ type jobScheduler struct {
registerCh chan RunnerRegistration
connectedRunners []Runner
configuredRunners map[string]config.Runner
jobs map[string]config.Job
}
type Run struct {
Name string
Ref string
JobName string
JobConfig config.Job
Ref string
}
type runnerJob struct {
@@ -54,49 +54,46 @@ func runJobScheduler(j jobScheduler) {
for {
select {
case run := <-j.runCh:
log.Infof("Got run: %v", run)
if job, exists := j.jobs[run.Name]; exists {
log.Debugf("Finding runner for job \"%v\"", run.Name)
rJ := runnerJob{
Id: run.Name,
URL: job.URL,
}
launched := false
for i, runner := range j.connectedRunners {
// don't send job to runner that is already occupied
if !runner.running {
// don't send job to runner with closed receiveChan (is defunct)
// there should never be messages to read on an inactive runner,
// so we aren't losing any data here
select {
case <-runner.receiveChan:
// if the receive channel is closed, swap delete the runner as it's defunct
j.connectedRunners[i] = j.connectedRunners[len(j.connectedRunners)-1]
j.connectedRunners = j.connectedRunners[:len(j.connectedRunners)-1]
default:
err := wsjson.Write(context.Background(), runner.conn, rJ)
if err != nil {
log.Debugf("Could not launch run: %v", err)
} else {
log.Infof("Launched run for job %v on runner %v", run.Name, runner.id)
launched = true
j.connectedRunners[i].running = true
break
}
log.Infof("Launching run for job \"%v\" on ref \"%v\"", run.JobName, run.Ref)
log.Debugf("Finding runner for job \"%v\"", run.JobName)
rJ := runnerJob{
Id: run.JobName,
URL: run.JobConfig.URL,
}
launched := false
for i, runner := range j.connectedRunners {
// don't send job to runner that is already occupied
if !runner.running {
// don't send job to runner with closed receiveChan (is defunct)
// there should never be messages to read on an inactive runner,
// so we aren't losing any data here
select {
case <-runner.receiveChan:
// if the receive channel is closed, swap delete the runner as it's defunct
j.connectedRunners[i] = j.connectedRunners[len(j.connectedRunners)-1]
j.connectedRunners = j.connectedRunners[:len(j.connectedRunners)-1]
default:
err := wsjson.Write(context.Background(), runner.conn, rJ)
if err != nil {
log.Errorf("Could not launch run: %v", err)
break
} else {
log.Infof("Launched run for job %v on runner %v", run.JobName, runner.id)
launched = true
j.connectedRunners[i].running = true
break
}
} else {
log.Debugf("Skipping runner %v, as runner is activly running another job", runner.id)
}
} else {
log.Debugf("Skipping runner %v, as runner is activly running another job", runner.id)
}
if !launched {
errorMsg := "could not find valid runner"
if len(j.connectedRunners) == 0 {
errorMsg = "no connected runners"
}
log.Errorf("Could not launch run for job \"%v\": %v", run.Name, errorMsg)
}
if !launched {
errorMsg := "could not find valid runner"
if len(j.connectedRunners) == 0 {
errorMsg = "no connected runners"
}
} else {
log.Errorf("No configured job with name %v", run.Name)
log.Errorf("Could not launch run for job \"%v\": %v", run.JobName, errorMsg)
}
case registration := <-j.registerCh:
log.Debugf("New runner appeared with id: %v and secret: %v", registration.Id, registration.Secret)
@@ -149,7 +146,6 @@ func StartJobScheduler(jobs map[string]config.Job, configuredRunners map[string]
registerCh: make(chan RunnerRegistration),
connectedRunners: make([]Runner, 0),
configuredRunners: configuredRunners,
jobs: jobs,
}
go runJobScheduler(scheduler)