Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 3 | import common |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 4 | import logging |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 5 | import sys |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 6 | from autotest_lib.server import frontend |
| 7 | |
| 8 | _AFE = frontend.AFE(debug=False) |
beeps | 8c30db1 | 2013-03-30 18:20:27 -0700 | [diff] [blame^] | 9 | |
| 10 | |
| 11 | class DatabaseAnomaly(Exception): |
| 12 | """Raised when we observe a database anomaly.""" |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 13 | |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 14 | |
| 15 | def is_job_complete(job_id): |
| 16 | """ |
| 17 | Check if a job is no longer active. |
| 18 | |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 19 | @param job_id: afe job id like 123 from 123-scottza |
beeps | 8c30db1 | 2013-03-30 18:20:27 -0700 | [diff] [blame^] | 20 | @return: An empty list if the job isn't complete. |
| 21 | A list containing the job details, if it is. |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 22 | """ |
beeps | 8c30db1 | 2013-03-30 18:20:27 -0700 | [diff] [blame^] | 23 | return _AFE.run('get_jobs', finished=True, id=job_id) |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def 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 |
beeps | 8c30db1 | 2013-03-30 18:20:27 -0700 | [diff] [blame^] | 38 | |
| 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 Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 43 | |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 44 | |
| 45 | if __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 Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 50 | sys.exit(not is_job_complete(sys.argv[1])) |