Change launch logic to receive config in scheduler
This commit is contained in:
@@ -37,11 +37,11 @@ type jobScheduler struct {
|
||||
registerCh chan RunnerRegistration
|
||||
connectedRunners []Runner
|
||||
configuredRunners map[string]config.Runner
|
||||
jobs map[string]config.Job
|
||||
}
|
||||
|
||||
type Run struct {
|
||||
Name string
|
||||
JobName string
|
||||
JobConfig config.Job
|
||||
Ref string
|
||||
}
|
||||
|
||||
@@ -54,12 +54,11 @@ 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)
|
||||
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.Name,
|
||||
URL: job.URL,
|
||||
Id: run.JobName,
|
||||
URL: run.JobConfig.URL,
|
||||
}
|
||||
launched := false
|
||||
for i, runner := range j.connectedRunners {
|
||||
@@ -76,9 +75,10 @@ func runJobScheduler(j jobScheduler) {
|
||||
default:
|
||||
err := wsjson.Write(context.Background(), runner.conn, rJ)
|
||||
if err != nil {
|
||||
log.Debugf("Could not launch run: %v", err)
|
||||
log.Errorf("Could not launch run: %v", err)
|
||||
break
|
||||
} else {
|
||||
log.Infof("Launched run for job %v on runner %v", run.Name, runner.id)
|
||||
log.Infof("Launched run for job %v on runner %v", run.JobName, runner.id)
|
||||
launched = true
|
||||
j.connectedRunners[i].running = true
|
||||
break
|
||||
@@ -93,10 +93,7 @@ func runJobScheduler(j jobScheduler) {
|
||||
if len(j.connectedRunners) == 0 {
|
||||
errorMsg = "no connected runners"
|
||||
}
|
||||
log.Errorf("Could not launch run for job \"%v\": %v", run.Name, errorMsg)
|
||||
}
|
||||
} 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)
|
||||
|
||||
+16
-8
@@ -15,7 +15,7 @@ import (
|
||||
var log = logging.MustGetLogger("cursorius-server")
|
||||
|
||||
func setupHTTPServer(runCh chan jobscheduler.Run, registerCh chan jobscheduler.RunnerRegistration,
|
||||
webhookConfig map[string]config.Webhook) {
|
||||
jobs map[string]config.Job) {
|
||||
http.HandleFunc("/webhook/", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case "POST":
|
||||
@@ -29,11 +29,15 @@ func setupHTTPServer(runCh chan jobscheduler.Run, registerCh chan jobscheduler.R
|
||||
// TODO: verify that this handles all valid URL formats
|
||||
webhookJobName := splitUrl[2]
|
||||
|
||||
for jobName, jobConfig := range webhookConfig {
|
||||
for jobName, jobConfig := range jobs {
|
||||
if webhookJobName == jobName {
|
||||
switch jobConfig.Sender {
|
||||
if jobConfig.Webhook == nil {
|
||||
log.Errorf("Matching job does not have webhook configuration, ignoring....")
|
||||
return
|
||||
}
|
||||
switch jobConfig.Webhook.Sender {
|
||||
case config.Gitea:
|
||||
hook, err := gitea.New(gitea.Options.Secret(jobConfig.Secret))
|
||||
hook, err := gitea.New(gitea.Options.Secret(jobConfig.Webhook.Secret))
|
||||
if err != nil {
|
||||
log.Errorf("Could not create Gitea webhook handler: %v", err)
|
||||
return
|
||||
@@ -48,10 +52,14 @@ func setupHTTPServer(runCh chan jobscheduler.Run, registerCh chan jobscheduler.R
|
||||
return
|
||||
}
|
||||
log.Infof("Got webhook with payload %v", payload)
|
||||
runCh <- jobscheduler.Run{Name: "test"}
|
||||
runCh <- jobscheduler.Run{
|
||||
JobName: jobName,
|
||||
JobConfig: jobConfig,
|
||||
Ref: "master",
|
||||
}
|
||||
return
|
||||
default:
|
||||
log.Errorf("Job configured with unkown webhook sender \"%v\", igonring...", jobConfig.Sender)
|
||||
log.Errorf("Job configured with unknown webhook sender \"%v\", igonring...", jobConfig.Webhook.Sender)
|
||||
return
|
||||
|
||||
}
|
||||
@@ -75,9 +83,9 @@ func setupHTTPServer(runCh chan jobscheduler.Run, registerCh chan jobscheduler.R
|
||||
})
|
||||
}
|
||||
|
||||
func Listen(address string, port int, runCh chan jobscheduler.Run, registerCh chan jobscheduler.RunnerRegistration, webhookConfig map[string]config.Webhook) {
|
||||
func Listen(address string, port int, runCh chan jobscheduler.Run, registerCh chan jobscheduler.RunnerRegistration, jobs map[string]config.Job) {
|
||||
|
||||
setupHTTPServer(runCh, registerCh, webhookConfig)
|
||||
setupHTTPServer(runCh, registerCh, jobs)
|
||||
|
||||
connect_string := fmt.Sprintf("%v:%v", address, port)
|
||||
log.Noticef("Launching HTTP server on %v\n", connect_string)
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"git.ohea.xyz/cursorius/server/jobscheduler"
|
||||
"git.ohea.xyz/cursorius/server/listen"
|
||||
"git.ohea.xyz/cursorius/server/poll"
|
||||
"git.ohea.xyz/cursorius/server/util"
|
||||
"github.com/op/go-logging"
|
||||
)
|
||||
|
||||
@@ -39,7 +38,6 @@ func main() {
|
||||
|
||||
poll.StartPolling(configData.Config.Jobs, runCh)
|
||||
|
||||
webhookConfig := util.CreateWebhookConfig(configData.Config.Jobs)
|
||||
listen.Listen(configData.Config.Address, configData.Config.Port, runCh, registerCh, webhookConfig)
|
||||
listen.Listen(configData.Config.Address, configData.Config.Port, runCh, registerCh, configData.Config.Jobs)
|
||||
|
||||
}
|
||||
|
||||
+2
-1
@@ -90,7 +90,8 @@ func pollJob(repoName string, jobConfig config.Job, runCh chan jobscheduler.Run)
|
||||
for _, ref := range refsToRunFor {
|
||||
log.Debugf("Dispatching job for ref %v in repo %v", ref, repoName)
|
||||
runCh <- jobscheduler.Run{
|
||||
Name: repoName,
|
||||
JobName: repoName,
|
||||
JobConfig: jobConfig,
|
||||
Ref: ref,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package util
|
||||
|
||||
import "git.ohea.xyz/cursorius/server/config"
|
||||
|
||||
// Generated map for webhook handler config
|
||||
func CreateWebhookConfig(conf map[string]config.Job) map[string]config.Webhook {
|
||||
webhookConfig := make(map[string]config.Webhook, 0)
|
||||
for k, v := range conf {
|
||||
if v.Webhook != nil {
|
||||
webhookConfig[k] = *v.Webhook
|
||||
}
|
||||
}
|
||||
return webhookConfig
|
||||
}
|
||||
Reference in New Issue
Block a user