@@ -52,6 +52,7 @@ def __init__(self, app, nworkers, **kwargs):
5252 k8s_galaxy_instance_id = dict (map = str ),
5353 k8s_timeout_seconds_job_deletion = dict (map = int , valid = lambda x : int > 0 , default = 30 ),
5454 k8s_job_api_version = dict (map = str , default = "batch/v1" ),
55+ k8s_job_ttl_secs_after_finished = dict (map = int , valid = lambda x : x is None or int (x ) >= 0 , default = 300 ),
5556 k8s_supplemental_group_id = dict (map = str ),
5657 k8s_pull_policy = dict (map = str , default = "Default" ),
5758 k8s_fs_group_id = dict (map = int ),
@@ -221,6 +222,9 @@ def __produce_unique_k8s_job_name(self, galaxy_internal_job_id):
221222 def __get_k8s_job_spec (self , ajs ):
222223 """Creates the k8s Job spec. For a Job spec, the only requirement is to have a .spec.template."""
223224 k8s_job_spec = {"template" : self .__get_k8s_job_spec_template (ajs )}
225+ job_ttl = self .runner_params ["k8s_job_ttl_secs_after_finished" ]
226+ if self .runner_params ["k8s_cleanup_job" ] != "never" and job_ttl is not None :
227+ k8s_job_spec ["ttlSecondsAfterFinished" ] = job_ttl
224228 return k8s_job_spec
225229
226230 def __get_k8s_job_spec_template (self , ajs ):
@@ -478,13 +482,18 @@ def __cleanup_k8s_job(self, job):
478482 job .scale (replicas = 0 )
479483 if (k8s_cleanup_job == "always" or
480484 (k8s_cleanup_job == "onsuccess" and not job_failed )):
481- delete_options = {
482- "apiVersion" : "v1" ,
483- "kind" : "DeleteOptions" ,
484- "propagationPolicy" : "Background"
485- }
486- r = job .api .delete (json = delete_options , ** job .api_kwargs ())
487- job .api .raise_for_status (r )
485+ job_ttl = self .runner_params ["k8s_job_ttl_secs_after_finished" ]
486+ if job_ttl is None :
487+ delete_options = {
488+ "apiVersion" : "v1" ,
489+ "kind" : "DeleteOptions" ,
490+ "propagationPolicy" : "Background"
491+ }
492+ r = job .api .delete (json = delete_options , ** job .api_kwargs ())
493+ job .api .raise_for_status (r )
494+ else :
495+ # Let the k8s ttl take care of deletion
496+ pass
488497
489498 def __job_failed_due_to_low_memory (self , job_state ):
490499 """
0 commit comments