feat(backend): add config option max_task_duration_sec

This commit is contained in:
Denis Gukov 2023-12-25 04:17:12 +05:00
parent 7e7a543e03
commit 34485b7b8e
2 changed files with 14 additions and 0 deletions

View File

@ -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"
)
@ -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 ||
@ -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

View File

@ -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"`