diff --git a/plugins/scheduler-k3s/template.go b/plugins/scheduler-k3s/template.go index dcc6060d8..4f57700ba 100644 --- a/plugins/scheduler-k3s/template.go +++ b/plugins/scheduler-k3s/template.go @@ -349,24 +349,25 @@ type ClusterIssuer struct { } type Job struct { - AppName string - Command []string - DeploymentID int64 - Entrypoint string - Env map[string]string - ID string - Image string - ImagePullSecrets string - ImageSourceType string - Interactive bool - Labels map[string]string - Namespace string - ProcessType string - Schedule string - SecurityContext SecurityContext - Suffix string - RemoveContainer bool - WorkingDir string + AppName string + Command []string + DeploymentID int64 + Entrypoint string + Env map[string]string + ID string + Image string + ImagePullSecrets string + ImageSourceType string + Interactive bool + Labels map[string]string + Namespace string + ProcessType string + Schedule string + SecurityContext SecurityContext + Suffix string + RemoveContainer bool + WorkingDir string + ActiveDeadlineSeconds int64 } // SecurityContext contains the security context for a process @@ -479,7 +480,8 @@ func templateKubernetesJob(input Job) (batchv1.Job, error) { Annotations: annotations, }, Spec: batchv1.JobSpec{ - BackoffLimit: ptr.To(int32(0)), + ActiveDeadlineSeconds: ptr.To(input.ActiveDeadlineSeconds), + BackoffLimit: ptr.To(int32(0)), Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: labels, diff --git a/plugins/scheduler-k3s/triggers.go b/plugins/scheduler-k3s/triggers.go index de2e9c2c9..1c12808ba 100644 --- a/plugins/scheduler-k3s/triggers.go +++ b/plugins/scheduler-k3s/triggers.go @@ -1329,23 +1329,32 @@ func TriggerSchedulerRun(scheduler string, appName string, envCount int, args [] return fmt.Errorf("Error getting security context: %w", err) } + activeDeadlineSeconds := int64(86400) // 24 hours + if os.Getenv("DOKKU_RUN_TTL_SECONDS") != "" { + activeDeadlineSeconds, err = strconv.ParseInt(os.Getenv("DOKKU_RUN_TTL_SECONDS"), 10, 64) + if err != nil { + return fmt.Errorf("Error parsing DOKKU_RUN_TTL_SECONDS value as int64: %w", err) + } + } + workingDir := common.GetWorkingDir(appName, image) job, err := templateKubernetesJob(Job{ - AppName: appName, - Command: []string{commandShell}, - DeploymentID: deploymentID, - Entrypoint: entrypoint, - Env: extraEnv, - Image: image, - ImagePullSecrets: imagePullSecrets, - ImageSourceType: imageSourceType, - Interactive: attachToPod || common.ToBool(os.Getenv("DOKKU_FORCE_TTY")), - Labels: labels, - Namespace: namespace, - ProcessType: processType, - RemoveContainer: rmContainer, - SecurityContext: securityContext, - WorkingDir: workingDir, + AppName: appName, + Command: []string{commandShell}, + DeploymentID: deploymentID, + Entrypoint: entrypoint, + Env: extraEnv, + Image: image, + ImagePullSecrets: imagePullSecrets, + ImageSourceType: imageSourceType, + Interactive: attachToPod || common.ToBool(os.Getenv("DOKKU_FORCE_TTY")), + Labels: labels, + Namespace: namespace, + ProcessType: processType, + RemoveContainer: rmContainer, + SecurityContext: securityContext, + WorkingDir: workingDir, + ActiveDeadlineSeconds: activeDeadlineSeconds, }) if err != nil { return fmt.Errorf("Error templating job: %w", err)