@@ -1778,35 +1778,55 @@ cdef class job:
1778
1778
1779
1779
return retList
1780
1780
1781
- def find_id (self , jobid ):
1782
- """ Retrieve job ID data.
1781
+ cdef _load_single_job(self , jobid):
1782
+ """
1783
+ Uses slurm_load_job to setup the self._job_ptr for a single job given by the jobid.
1784
+ After calling this, the job pointer can be used in other methods
1785
+ to operate on the informations of the job.
1783
1786
1784
- This method accepts both string and integer formats of the jobid. It
1785
- calls slurm_xlate_job_id() to convert the jobid appropriately.
1786
- This works for single jobs and job arrays.
1787
+ This method accepts both string and integer formate of the jobid. It
1788
+ calls slurm_xlate_job_id to convert the jobid appropriately.
1787
1789
1788
- :param str jobid: Job id key string to search
1789
- :returns: List of dictionary of values for given job id
1790
- :rtype: `list`
1790
+ Raises an value error if the jobid does not correspond to a existing job.
1791
+
1792
+ :param str jobid: The jobid
1793
+ :returns: void
1794
+ :rtype: None.
1791
1795
"""
1792
1796
cdef:
1793
1797
int apiError
1794
1798
int rc
1795
1799
1800
+ # jobid can be given as int or string
1796
1801
if isinstance (jobid, int ) or isinstance (jobid, long ):
1797
1802
jobid = str (jobid).encode(" UTF-8" )
1798
1803
else :
1799
1804
jobid = jobid.encode(" UTF-8" )
1800
-
1805
+ # convert jobid appropriately for slurm
1801
1806
jobid_xlate = slurm.slurm_xlate_job_id(jobid)
1807
+
1808
+ # load the job which sets the self._job_ptr pointer
1802
1809
rc = slurm.slurm_load_job(& self ._job_ptr, jobid_xlate, self ._ShowFlags)
1803
1810
1804
- if rc == slurm.SLURM_SUCCESS:
1805
- return list (self .get_job_ptr().values())
1806
- else :
1811
+ if rc != slurm.SLURM_SUCCESS:
1807
1812
apiError = slurm.slurm_get_errno()
1808
1813
raise ValueError (slurm.stringOrNone(slurm.slurm_strerror(apiError), ' ' ), apiError)
1809
1814
1815
+ def find_id (self , jobid ):
1816
+ """ Retrieve job ID data.
1817
+
1818
+ This method accepts both string and integer formats of the jobid.
1819
+ This works for single jobs and job arrays. It uses the internal
1820
+ helper _load_single_job to do slurm_load_job. If the job corresponding
1821
+ to the jobid does not exist, a ValueError will be raised.
1822
+
1823
+ :param str jobid: Job id key string to search
1824
+ :returns: List of dictionary of values for given job id
1825
+ :rtype: `list`
1826
+ """
1827
+ self ._load_single_job(jobid)
1828
+ return list (self .get_job_ptr().values())
1829
+
1810
1830
def find_user (self , user ):
1811
1831
""" Retrieve a user's job data.
1812
1832
@@ -2883,6 +2903,38 @@ cdef class job:
2883
2903
# return "Submitted batch job %s" % job_id
2884
2904
return job_id
2885
2905
2906
+ def wait_finished (self , jobid ):
2907
+ """
2908
+ Block until the job given by the jobid finishes.
2909
+ This works for single jobs, as well as job arrays.
2910
+ :param jobid: The job id of the slurm job.
2911
+ To reference a job with job array set, use the first/"master" jobid
2912
+ (the same as given by squeue)
2913
+ :returns: The exit code of the slurm job.
2914
+ :rtype: `int`
2915
+ """
2916
+ exit_status = - 9999
2917
+ complete = False
2918
+ while not complete:
2919
+ complete = True
2920
+ p_time.sleep(5 )
2921
+ self ._load_single_job(jobid)
2922
+ for i in range (0 , self ._job_ptr.record_count):
2923
+ self ._record = & self ._job_ptr.job_array[i]
2924
+ if IS_JOB_COMPLETED(self ._job_ptr.job_array[i]):
2925
+ exit_status_arrayjob = None
2926
+ if WIFEXITED(self ._record.exit_code):
2927
+ exit_status_arrayjob = WEXITSTATUS(self ._record.exit_code)
2928
+ else :
2929
+ exit_status_arrayjob = 1
2930
+ # set exit code to the highest of all jobs in job array
2931
+ exit_status = max ([exit_status, exit_status_arrayjob])
2932
+ else :
2933
+ # go on with the next interation, unil all jobs in array are completed
2934
+ complete = False
2935
+ slurm.slurm_free_job_info_msg(self ._job_ptr)
2936
+ return exit_status
2937
+
2886
2938
2887
2939
def slurm_pid2jobid (uint32_t JobPID = 0 ):
2888
2940
""" Get the slurm job id from a process id.
0 commit comments