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 |
| 5 | import _mysql_exceptions |
| 6 | import sys |
| 7 | from autotest_lib.database import database_connection |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 8 | from autotest_lib.server import frontend |
| 9 | |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 10 | DB_CONFIG_SECTION = 'AUTOTEST_WEB' |
| 11 | SPECIAL_TASKS_SQL_CMD = 'SELECT is_complete FROM afe_special_tasks WHERE id=%s' |
| 12 | |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 13 | _AFE = frontend.AFE(debug=False) |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 14 | _DATABASE = database_connection.DatabaseConnection(DB_CONFIG_SECTION) |
| 15 | _DATABASE.connect() |
| 16 | |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 17 | |
| 18 | def is_job_complete(job_id): |
| 19 | """ |
| 20 | Check if a job is no longer active. |
| 21 | |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 22 | @param job_id: afe job id like 123 from 123-scottza |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 23 | @return True if job is complete and False if it is not |
| 24 | """ |
| 25 | if not _AFE.run('get_jobs', finished=True, id=job_id): |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 26 | return False |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 27 | |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 28 | return True |
| 29 | |
| 30 | |
| 31 | def is_special_task_complete(job_id): |
| 32 | """ |
| 33 | Check if a special task (Cleanup, Verify, Repair) job is no longer active. |
| 34 | |
| 35 | @param job_id: job id in string format like '123' from '123-cleanup' |
| 36 | |
| 37 | @return True if a job is complete, and False if it is not. |
| 38 | """ |
| 39 | # Make sure the job_id is a number. |
| 40 | if not job_id.isdigit(): |
| 41 | logging.error('Job_id: %s is not a number returning False.', job_id) |
| 42 | return False |
| 43 | try: |
| 44 | query_results = _DATABASE.execute(SPECIAL_TASKS_SQL_CMD, job_id, |
| 45 | try_reconnecting=True) |
| 46 | except _mysql_exceptions.OperationalError: |
| 47 | logging.error('Database query failed for job_id: %s.', job_id) |
| 48 | return False |
| 49 | # Check if no table rows are returned. |
| 50 | if not query_results: |
| 51 | logging.error('Job_id: %s is not a valid special task id.', job_id) |
| 52 | return False |
| 53 | # Return the only column in the first (only) row. |
| 54 | # 1 means done, 0 means running. |
| 55 | return query_results[0][0] |
| 56 | |
Scott Zawalski | cb2e2b7 | 2012-04-17 12:10:05 -0400 | [diff] [blame] | 57 | |
| 58 | if __name__ == '__main__': |
| 59 | if len(sys.argv) != 2: |
| 60 | print ('Set return status to 0 if job is complete or 1 if it is not.\n' |
| 61 | 'Usage: is_job_complete.py <job_id>') |
| 62 | else: |
Simran Basi | 91746ef | 2012-08-07 13:55:25 -0700 | [diff] [blame] | 63 | sys.exit(not is_job_complete(sys.argv[1])) |