Skip to content

Commit

Permalink
fix(runners): add atimic checks
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Sep 20, 2023
1 parent a6c89d0 commit 7f6173b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
13 changes: 13 additions & 0 deletions config-runner.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
15 changes: 15 additions & 0 deletions services/runners/JobPool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"os/exec"
"strconv"
"sync/atomic"
"time"
)

Expand Down Expand Up @@ -106,6 +107,9 @@ type JobPool struct {
queue []*job

config *RunnerConfig

checking int32
sending int32
}

type RunnerRegistration struct {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -289,6 +297,7 @@ func (p *JobPool) sendProgress() {
}

defer resp.Body.Close()
defer atomic.StoreInt32(&p.sending, 0)
}

func (p *JobPool) tryRegisterRunner() bool {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions services/tasks/TaskRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 7f6173b

Please sign in to comment.