blob: 252ea6b4ecf6d28d7e1a0e820c33b8e7ce2eea43 [file] [log] [blame]
Scott Zawalskicb2e2b72012-04-17 12:10:05 -04001#!/usr/bin/python
2
Scott Zawalskicb2e2b72012-04-17 12:10:05 -04003import common
Simran Basi91746ef2012-08-07 13:55:25 -07004import logging
Simran Basi91746ef2012-08-07 13:55:25 -07005import sys
Scott Zawalskicb2e2b72012-04-17 12:10:05 -04006from autotest_lib.server import frontend
7
8_AFE = frontend.AFE(debug=False)
beeps8c30db12013-03-30 18:20:27 -07009
10
11class DatabaseAnomaly(Exception):
12 """Raised when we observe a database anomaly."""
Simran Basi91746ef2012-08-07 13:55:25 -070013
Scott Zawalskicb2e2b72012-04-17 12:10:05 -040014
15def is_job_complete(job_id):
16 """
17 Check if a job is no longer active.
18
Simran Basi91746ef2012-08-07 13:55:25 -070019 @param job_id: afe job id like 123 from 123-scottza
beeps8c30db12013-03-30 18:20:27 -070020 @return: An empty list if the job isn't complete.
21 A list containing the job details, if it is.
Scott Zawalskicb2e2b72012-04-17 12:10:05 -040022 """
beeps8c30db12013-03-30 18:20:27 -070023 return _AFE.run('get_jobs', finished=True, id=job_id)
Simran Basi91746ef2012-08-07 13:55:25 -070024
25
26def is_special_task_complete(job_id):
27 """
28 Check if a special task (Cleanup, Verify, Repair) job is no longer active.
29
30 @param job_id: job id in string format like '123' from '123-cleanup'
31
32 @return True if a job is complete, and False if it is not.
33 """
34 # Make sure the job_id is a number.
35 if not job_id.isdigit():
36 logging.error('Job_id: %s is not a number returning False.', job_id)
37 return False
beeps8c30db12013-03-30 18:20:27 -070038
39 task = _AFE.run('get_special_tasks', id=job_id)
40 if not task:
41 raise DatabaseAnomaly('Special Task %s not found in database.' % job_id)
42 return task[0].get('is_complete')
Simran Basi91746ef2012-08-07 13:55:25 -070043
Scott Zawalskicb2e2b72012-04-17 12:10:05 -040044
45if __name__ == '__main__':
46 if len(sys.argv) != 2:
47 print ('Set return status to 0 if job is complete or 1 if it is not.\n'
48 'Usage: is_job_complete.py <job_id>')
49 else:
Simran Basi91746ef2012-08-07 13:55:25 -070050 sys.exit(not is_job_complete(sys.argv[1]))