blob: f19a27b6998a767da482dc6e9661f6f38dd2b24c [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
5import _mysql_exceptions
6import sys
7from autotest_lib.database import database_connection
Scott Zawalskicb2e2b72012-04-17 12:10:05 -04008from autotest_lib.server import frontend
9
Simran Basi91746ef2012-08-07 13:55:25 -070010DB_CONFIG_SECTION = 'AUTOTEST_WEB'
11SPECIAL_TASKS_SQL_CMD = 'SELECT is_complete FROM afe_special_tasks WHERE id=%s'
12
Scott Zawalskicb2e2b72012-04-17 12:10:05 -040013_AFE = frontend.AFE(debug=False)
Simran Basi91746ef2012-08-07 13:55:25 -070014_DATABASE = database_connection.DatabaseConnection(DB_CONFIG_SECTION)
15_DATABASE.connect()
16
Scott Zawalskicb2e2b72012-04-17 12:10:05 -040017
18def is_job_complete(job_id):
19 """
20 Check if a job is no longer active.
21
Simran Basi91746ef2012-08-07 13:55:25 -070022 @param job_id: afe job id like 123 from 123-scottza
Scott Zawalskicb2e2b72012-04-17 12:10:05 -040023 @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 Basi91746ef2012-08-07 13:55:25 -070026 return False
Scott Zawalskicb2e2b72012-04-17 12:10:05 -040027
Simran Basi91746ef2012-08-07 13:55:25 -070028 return True
29
30
31def 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 Zawalskicb2e2b72012-04-17 12:10:05 -040057
58if __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 Basi91746ef2012-08-07 13:55:25 -070063 sys.exit(not is_job_complete(sys.argv[1]))