Skip to content

Commit

Permalink
feat(backend): add config option max_task_duration_sec
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Dec 24, 2023
1 parent 7e7a543 commit 34485b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions services/tasks/RemoteJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ansible-semaphore/semaphore/db"
"github.com/ansible-semaphore/semaphore/db_lib"
"github.com/ansible-semaphore/semaphore/lib"
"github.com/ansible-semaphore/semaphore/util"
"net/http"
"time"
)
Expand Down Expand Up @@ -120,7 +121,16 @@ func (t *RemoteJob) Run(username string, incomingVersion *string) (err error) {

tsk.RunnerID = runner.ID

startTime := time.Now()

taskTimedOut := false

for {
if util.Config.MaxTaskDurationSec > 0 && int(time.Now().Sub(startTime).Seconds()) > util.Config.MaxTaskDurationSec {
taskTimedOut = true
break
}

time.Sleep(1_000_000_000)
tsk = t.taskPool.GetTask(t.Task.ID)
if tsk.Task.Status == lib.TaskSuccessStatus ||
Expand All @@ -138,6 +148,8 @@ func (t *RemoteJob) Run(username string, incomingVersion *string) (err error) {

if tsk.Task.Status == lib.TaskFailStatus {
err = fmt.Errorf("task failed")
} else if taskTimedOut {
err = fmt.Errorf("task timed out")
}

return
Expand Down
2 changes: 2 additions & 0 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ type ConfigType struct {
// oidc settings
OidcProviders map[string]OidcProvider `json:"oidc_providers"`

MaxTaskDurationSec int `json:"max_task_duration_sec" env:"MAX_TASK_DURATION_SEC"`

// task concurrency
MaxParallelTasks int `json:"max_parallel_tasks" default:"10" rule:"^[0-9]{1,10}$" env:"SEMAPHORE_MAX_PARALLEL_TASKS"`

Expand Down

0 comments on commit 34485b7

Please sign in to comment.