From 34485b7b8e825a1a1f0e671e3d2a876c7d1c1796 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Mon, 25 Dec 2023 04:17:12 +0500 Subject: [PATCH] feat(backend): add config option max_task_duration_sec --- services/tasks/RemoteJob.go | 12 ++++++++++++ util/config.go | 2 ++ 2 files changed, 14 insertions(+) diff --git a/services/tasks/RemoteJob.go b/services/tasks/RemoteJob.go index f38f4d28..90a9dc2f 100644 --- a/services/tasks/RemoteJob.go +++ b/services/tasks/RemoteJob.go @@ -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 diff --git a/util/config.go b/util/config.go index 1b8962df..32c70c34 100644 --- a/util/config.go +++ b/util/config.go @@ -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"`