From 7f6173bf14ad264bcc98e23315c2e2bb75fb8059 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Wed, 20 Sep 2023 02:17:41 +0200 Subject: [PATCH] fix(runners): add atimic checks --- config-runner.json | 13 +++++++++++++ services/runners/JobPool.go | 15 +++++++++++++++ services/tasks/TaskRunner.go | 2 -- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 config-runner.json diff --git a/config-runner.json b/config-runner.json new file mode 100644 index 000000000..6c35f924f --- /dev/null +++ b/config-runner.json @@ -0,0 +1,13 @@ +{ + "bolt": { + "host": "/Users/fiftin/go/src/github.com/ansible-semaphore/semaphore/database.boltdb" + }, + + "dialect": "bolt", + "tmp_path": "/var/folders/x1/d7p_yr4j7g57_r2r8s0ll_1r0000gn/T/semaphore", + + "runner": { + "config_file": "/tmp/semaphore-runner.json", + "api_url": "http://localhost:3000/api" + } +} \ No newline at end of file diff --git a/services/runners/JobPool.go b/services/runners/JobPool.go index 639093853..4f054477a 100644 --- a/services/runners/JobPool.go +++ b/services/runners/JobPool.go @@ -20,6 +20,7 @@ import ( "os" "os/exec" "strconv" + "sync/atomic" "time" ) @@ -106,6 +107,9 @@ type JobPool struct { queue []*job config *RunnerConfig + + checking int32 + sending int32 } type RunnerRegistration struct { @@ -256,6 +260,10 @@ func (p *JobPool) Run() { func (p *JobPool) sendProgress() { + if !atomic.CompareAndSwapInt32(&p.sending, 0, 1) { + return + } + client := &http.Client{} url := util.Config.Runner.ApiURL + "/runners/" + strconv.Itoa(p.config.RunnerID) @@ -289,6 +297,7 @@ func (p *JobPool) sendProgress() { } defer resp.Body.Close() + defer atomic.StoreInt32(&p.sending, 0) } func (p *JobPool) tryRegisterRunner() bool { @@ -381,6 +390,12 @@ func (p *JobPool) tryRegisterRunner() bool { // checkNewJobs tries to find runner to queued jobs func (p *JobPool) checkNewJobs() { + if !atomic.CompareAndSwapInt32(&p.checking, 0, 1) { + return + } + + defer atomic.StoreInt32(&p.checking, 0) + client := &http.Client{} url := util.Config.Runner.ApiURL + "/runners/" + strconv.Itoa(p.config.RunnerID) diff --git a/services/tasks/TaskRunner.go b/services/tasks/TaskRunner.go index 8fbf26866..67c344293 100644 --- a/services/tasks/TaskRunner.go +++ b/services/tasks/TaskRunner.go @@ -64,7 +64,6 @@ func (t *TaskRunner) SetStatus(status db.TaskStatus) { switch t.Task.Status { // check old status case db.TaskRunningStatus: if status == db.TaskWaitingStatus { - //panic("running TaskRunner cannot be " + status) return } break @@ -77,7 +76,6 @@ func (t *TaskRunner) SetStatus(status db.TaskStatus) { case db.TaskSuccessStatus: case db.TaskFailStatus: case db.TaskStoppedStatus: - //panic("stopped TaskRunner cannot be " + status) return }