mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | """ |
| 4 | Autotest scheduler |
| 5 | """ |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 6 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 7 | |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 8 | import datetime, errno, optparse, os, pwd, Queue, re, shutil, signal |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 9 | import smtplib, socket, stat, subprocess, sys, tempfile, time, traceback |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 10 | import itertools, logging, weakref |
mbligh | 70feeee | 2008-06-11 16:20:49 +0000 | [diff] [blame] | 11 | import common |
mbligh | 8bcd23a | 2009-02-03 19:14:06 +0000 | [diff] [blame] | 12 | import MySQLdb |
showard | 043c62a | 2009-06-10 19:48:57 +0000 | [diff] [blame] | 13 | from autotest_lib.scheduler import scheduler_logging_config |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 14 | from autotest_lib.frontend import setup_django_environment |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 15 | from autotest_lib.client.common_lib import global_config, logging_manager |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 16 | from autotest_lib.client.common_lib import host_protections, utils |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 17 | from autotest_lib.database import database_connection |
showard | 844960a | 2009-05-29 18:41:18 +0000 | [diff] [blame] | 18 | from autotest_lib.frontend.afe import models, rpc_utils, readonly_connection |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 19 | from autotest_lib.scheduler import drone_manager, drones, email_manager |
showard | 043c62a | 2009-06-10 19:48:57 +0000 | [diff] [blame] | 20 | from autotest_lib.scheduler import monitor_db_cleanup |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 21 | from autotest_lib.scheduler import status_server, scheduler_config |
mbligh | 70feeee | 2008-06-11 16:20:49 +0000 | [diff] [blame] | 22 | |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 23 | BABYSITTER_PID_FILE_PREFIX = 'monitor_db_babysitter' |
| 24 | PID_FILE_PREFIX = 'monitor_db' |
mbligh | b090f14 | 2008-02-27 21:33:46 +0000 | [diff] [blame] | 25 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 26 | RESULTS_DIR = '.' |
| 27 | AUTOSERV_NICE_LEVEL = 10 |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 28 | DB_CONFIG_SECTION = 'AUTOTEST_WEB' |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 29 | AUTOTEST_PATH = os.path.join(os.path.dirname(__file__), '..') |
| 30 | |
| 31 | if os.environ.has_key('AUTOTEST_DIR'): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 32 | AUTOTEST_PATH = os.environ['AUTOTEST_DIR'] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 33 | AUTOTEST_SERVER_DIR = os.path.join(AUTOTEST_PATH, 'server') |
| 34 | AUTOTEST_TKO_DIR = os.path.join(AUTOTEST_PATH, 'tko') |
| 35 | |
| 36 | if AUTOTEST_SERVER_DIR not in sys.path: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 37 | sys.path.insert(0, AUTOTEST_SERVER_DIR) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 38 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 39 | _AUTOSERV_PID_FILE = '.autoserv_execute' |
| 40 | _CRASHINFO_PID_FILE = '.collect_crashinfo_execute' |
| 41 | _PARSER_PID_FILE = '.parser_execute' |
| 42 | |
| 43 | _ALL_PIDFILE_NAMES = (_AUTOSERV_PID_FILE, _CRASHINFO_PID_FILE, |
| 44 | _PARSER_PID_FILE) |
| 45 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 46 | # error message to leave in results dir when an autoserv process disappears |
| 47 | # mysteriously |
| 48 | _LOST_PROCESS_ERROR = """\ |
| 49 | Autoserv failed abnormally during execution for this job, probably due to a |
| 50 | system error on the Autotest server. Full results may not be available. Sorry. |
| 51 | """ |
| 52 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 53 | _db = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 54 | _shutdown = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 55 | _autoserv_path = os.path.join(drones.AUTOTEST_INSTALL_DIR, 'server', 'autoserv') |
| 56 | _parser_path = os.path.join(drones.AUTOTEST_INSTALL_DIR, 'tko', 'parse') |
mbligh | 4314a71 | 2008-02-29 22:44:30 +0000 | [diff] [blame] | 57 | _testing_mode = False |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 58 | _base_url = None |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 59 | _notify_email_statuses = [] |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 60 | _drone_manager = drone_manager.DroneManager() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 61 | |
| 62 | |
showard | ec6a3b9 | 2009-09-25 20:29:13 +0000 | [diff] [blame] | 63 | def _get_pidfile_timeout_secs(): |
| 64 | """@returns How long to wait for autoserv to write pidfile.""" |
| 65 | pidfile_timeout_mins = global_config.global_config.get_config_value( |
| 66 | scheduler_config.CONFIG_SECTION, 'pidfile_timeout_mins', type=int) |
| 67 | return pidfile_timeout_mins * 60 |
| 68 | |
| 69 | |
mbligh | 83c1e9e | 2009-05-01 23:10:41 +0000 | [diff] [blame] | 70 | def _site_init_monitor_db_dummy(): |
| 71 | return {} |
| 72 | |
| 73 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 74 | def main(): |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 75 | try: |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 76 | try: |
| 77 | main_without_exception_handling() |
| 78 | except SystemExit: |
| 79 | raise |
| 80 | except: |
| 81 | logging.exception('Exception escaping in monitor_db') |
| 82 | raise |
| 83 | finally: |
| 84 | utils.delete_pid_file_if_exists(PID_FILE_PREFIX) |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def main_without_exception_handling(): |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 88 | setup_logging() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 89 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 90 | usage = 'usage: %prog [options] results_dir' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 91 | parser = optparse.OptionParser(usage) |
| 92 | parser.add_option('--recover-hosts', help='Try to recover dead hosts', |
| 93 | action='store_true') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 94 | parser.add_option('--test', help='Indicate that scheduler is under ' + |
| 95 | 'test and should use dummy autoserv and no parsing', |
| 96 | action='store_true') |
| 97 | (options, args) = parser.parse_args() |
| 98 | if len(args) != 1: |
| 99 | parser.print_usage() |
| 100 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 101 | |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 102 | scheduler_enabled = global_config.global_config.get_config_value( |
| 103 | scheduler_config.CONFIG_SECTION, 'enable_scheduler', type=bool) |
| 104 | |
| 105 | if not scheduler_enabled: |
| 106 | msg = ("Scheduler not enabled, set enable_scheduler to true in the " |
| 107 | "global_config's SCHEDULER section to enabled it. Exiting.") |
mbligh | 6fbdb80 | 2009-08-03 16:42:55 +0000 | [diff] [blame] | 108 | logging.error(msg) |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 109 | sys.exit(1) |
| 110 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 111 | global RESULTS_DIR |
| 112 | RESULTS_DIR = args[0] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 113 | |
mbligh | 83c1e9e | 2009-05-01 23:10:41 +0000 | [diff] [blame] | 114 | site_init = utils.import_site_function(__file__, |
| 115 | "autotest_lib.scheduler.site_monitor_db", "site_init_monitor_db", |
| 116 | _site_init_monitor_db_dummy) |
| 117 | site_init() |
| 118 | |
showard | cca334f | 2009-03-12 20:38:34 +0000 | [diff] [blame] | 119 | # Change the cwd while running to avoid issues incase we were launched from |
| 120 | # somewhere odd (such as a random NFS home directory of the person running |
| 121 | # sudo to launch us as the appropriate user). |
| 122 | os.chdir(RESULTS_DIR) |
| 123 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 124 | c = global_config.global_config |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 125 | notify_statuses_list = c.get_config_value(scheduler_config.CONFIG_SECTION, |
| 126 | "notify_email_statuses", |
| 127 | default='') |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 128 | global _notify_email_statuses |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 129 | _notify_email_statuses = [status for status in |
| 130 | re.split(r'[\s,;:]', notify_statuses_list.lower()) |
| 131 | if status] |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 132 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 133 | if options.test: |
| 134 | global _autoserv_path |
| 135 | _autoserv_path = 'autoserv_dummy' |
| 136 | global _testing_mode |
| 137 | _testing_mode = True |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 138 | |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 139 | # AUTOTEST_WEB.base_url is still a supported config option as some people |
| 140 | # may wish to override the entire url. |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 141 | global _base_url |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 142 | config_base_url = c.get_config_value(DB_CONFIG_SECTION, 'base_url', |
| 143 | default='') |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 144 | if config_base_url: |
| 145 | _base_url = config_base_url |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 146 | else: |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 147 | # For the common case of everything running on a single server you |
| 148 | # can just set the hostname in a single place in the config file. |
| 149 | server_name = c.get_config_value('SERVER', 'hostname') |
| 150 | if not server_name: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 151 | logging.critical('[SERVER] hostname missing from the config file.') |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 152 | sys.exit(1) |
| 153 | _base_url = 'http://%s/afe/' % server_name |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 154 | |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 155 | server = status_server.StatusServer(_drone_manager) |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 156 | server.start() |
| 157 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 158 | try: |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 159 | init() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 160 | dispatcher = Dispatcher() |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 161 | dispatcher.initialize(recover_hosts=options.recover_hosts) |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 162 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 163 | while not _shutdown: |
| 164 | dispatcher.tick() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 165 | time.sleep(scheduler_config.config.tick_pause_sec) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 166 | except: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 167 | email_manager.manager.log_stacktrace( |
| 168 | "Uncaught exception; terminating monitor_db") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 169 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 170 | email_manager.manager.send_queued_emails() |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 171 | server.shutdown() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 172 | _drone_manager.shutdown() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 173 | _db.disconnect() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 174 | |
| 175 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 176 | def setup_logging(): |
| 177 | log_dir = os.environ.get('AUTOTEST_SCHEDULER_LOG_DIR', None) |
| 178 | log_name = os.environ.get('AUTOTEST_SCHEDULER_LOG_NAME', None) |
| 179 | logging_manager.configure_logging( |
| 180 | scheduler_logging_config.SchedulerLoggingConfig(), log_dir=log_dir, |
| 181 | logfile_name=log_name) |
| 182 | |
| 183 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 184 | def handle_sigint(signum, frame): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 185 | global _shutdown |
| 186 | _shutdown = True |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 187 | logging.info("Shutdown request received.") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 188 | |
| 189 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 190 | def init(): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 191 | logging.info("%s> dispatcher starting", time.strftime("%X %x")) |
| 192 | logging.info("My PID is %d", os.getpid()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 193 | |
showard | 8de3713 | 2009-08-31 18:33:08 +0000 | [diff] [blame] | 194 | if utils.program_is_alive(PID_FILE_PREFIX): |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 195 | logging.critical("monitor_db already running, aborting!") |
| 196 | sys.exit(1) |
| 197 | utils.write_pid(PID_FILE_PREFIX) |
mbligh | fb67603 | 2009-04-01 18:25:38 +0000 | [diff] [blame] | 198 | |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 199 | if _testing_mode: |
| 200 | global_config.global_config.override_config_value( |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 201 | DB_CONFIG_SECTION, 'database', 'stresstest_autotest_web') |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 202 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 203 | os.environ['PATH'] = AUTOTEST_SERVER_DIR + ':' + os.environ['PATH'] |
| 204 | global _db |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 205 | _db = database_connection.DatabaseConnection(DB_CONFIG_SECTION) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 206 | _db.connect() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 207 | |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 208 | # ensure Django connection is in autocommit |
| 209 | setup_django_environment.enable_autocommit() |
showard | 844960a | 2009-05-29 18:41:18 +0000 | [diff] [blame] | 210 | # bypass the readonly connection |
| 211 | readonly_connection.ReadOnlyConnection.set_globally_disabled(True) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 212 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 213 | logging.info("Setting signal handler") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 214 | signal.signal(signal.SIGINT, handle_sigint) |
| 215 | |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 216 | drones = global_config.global_config.get_config_value( |
| 217 | scheduler_config.CONFIG_SECTION, 'drones', default='localhost') |
| 218 | drone_list = [hostname.strip() for hostname in drones.split(',')] |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 219 | results_host = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 220 | scheduler_config.CONFIG_SECTION, 'results_host', default='localhost') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 221 | _drone_manager.initialize(RESULTS_DIR, drone_list, results_host) |
| 222 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 223 | logging.info("Connected! Running...") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 224 | |
| 225 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 226 | def _autoserv_command_line(machines, extra_args, job=None, queue_entry=None, |
| 227 | verbose=True): |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 228 | """ |
| 229 | @returns The autoserv command line as a list of executable + parameters. |
| 230 | |
| 231 | @param machines - string - A machine or comma separated list of machines |
| 232 | for the (-m) flag. |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 233 | @param extra_args - list - Additional arguments to pass to autoserv. |
| 234 | @param job - Job object - If supplied, -u owner and -l name parameters |
| 235 | will be added. |
| 236 | @param queue_entry - A HostQueueEntry object - If supplied and no Job |
| 237 | object was supplied, this will be used to lookup the Job object. |
| 238 | """ |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 239 | autoserv_argv = [_autoserv_path, '-p', '-m', machines, |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 240 | '-r', drone_manager.WORKING_DIRECTORY] |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 241 | if job or queue_entry: |
| 242 | if not job: |
| 243 | job = queue_entry.job |
| 244 | autoserv_argv += ['-u', job.owner, '-l', job.name] |
showard | e9c6936 | 2009-06-30 01:58:03 +0000 | [diff] [blame] | 245 | if verbose: |
| 246 | autoserv_argv.append('--verbose') |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 247 | return autoserv_argv + extra_args |
| 248 | |
| 249 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 250 | class SchedulerError(Exception): |
| 251 | """Raised by HostScheduler when an inconsistent state occurs.""" |
| 252 | |
| 253 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 254 | class HostScheduler(object): |
| 255 | def _get_ready_hosts(self): |
| 256 | # avoid any host with a currently active queue entry against it |
| 257 | hosts = Host.fetch( |
| 258 | joins='LEFT JOIN host_queue_entries AS active_hqe ' |
| 259 | 'ON (hosts.id = active_hqe.host_id AND ' |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 260 | 'active_hqe.active)', |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 261 | where="active_hqe.host_id IS NULL " |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 262 | "AND NOT hosts.locked " |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 263 | "AND (hosts.status IS NULL OR hosts.status = 'Ready')") |
| 264 | return dict((host.id, host) for host in hosts) |
| 265 | |
| 266 | |
| 267 | @staticmethod |
| 268 | def _get_sql_id_list(id_list): |
| 269 | return ','.join(str(item_id) for item_id in id_list) |
| 270 | |
| 271 | |
| 272 | @classmethod |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 273 | def _get_many2many_dict(cls, query, id_list, flip=False): |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 274 | if not id_list: |
| 275 | return {} |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 276 | query %= cls._get_sql_id_list(id_list) |
| 277 | rows = _db.execute(query) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 278 | return cls._process_many2many_dict(rows, flip) |
| 279 | |
| 280 | |
| 281 | @staticmethod |
| 282 | def _process_many2many_dict(rows, flip=False): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 283 | result = {} |
| 284 | for row in rows: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 285 | left_id, right_id = int(row[0]), int(row[1]) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 286 | if flip: |
| 287 | left_id, right_id = right_id, left_id |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 288 | result.setdefault(left_id, set()).add(right_id) |
| 289 | return result |
| 290 | |
| 291 | |
| 292 | @classmethod |
| 293 | def _get_job_acl_groups(cls, job_ids): |
| 294 | query = """ |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 295 | SELECT jobs.id, acl_groups_users.aclgroup_id |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 296 | FROM jobs |
| 297 | INNER JOIN users ON users.login = jobs.owner |
| 298 | INNER JOIN acl_groups_users ON acl_groups_users.user_id = users.id |
| 299 | WHERE jobs.id IN (%s) |
| 300 | """ |
| 301 | return cls._get_many2many_dict(query, job_ids) |
| 302 | |
| 303 | |
| 304 | @classmethod |
| 305 | def _get_job_ineligible_hosts(cls, job_ids): |
| 306 | query = """ |
| 307 | SELECT job_id, host_id |
| 308 | FROM ineligible_host_queues |
| 309 | WHERE job_id IN (%s) |
| 310 | """ |
| 311 | return cls._get_many2many_dict(query, job_ids) |
| 312 | |
| 313 | |
| 314 | @classmethod |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 315 | def _get_job_dependencies(cls, job_ids): |
| 316 | query = """ |
| 317 | SELECT job_id, label_id |
| 318 | FROM jobs_dependency_labels |
| 319 | WHERE job_id IN (%s) |
| 320 | """ |
| 321 | return cls._get_many2many_dict(query, job_ids) |
| 322 | |
| 323 | |
| 324 | @classmethod |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 325 | def _get_host_acls(cls, host_ids): |
| 326 | query = """ |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 327 | SELECT host_id, aclgroup_id |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 328 | FROM acl_groups_hosts |
| 329 | WHERE host_id IN (%s) |
| 330 | """ |
| 331 | return cls._get_many2many_dict(query, host_ids) |
| 332 | |
| 333 | |
| 334 | @classmethod |
| 335 | def _get_label_hosts(cls, host_ids): |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 336 | if not host_ids: |
| 337 | return {}, {} |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 338 | query = """ |
| 339 | SELECT label_id, host_id |
| 340 | FROM hosts_labels |
| 341 | WHERE host_id IN (%s) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 342 | """ % cls._get_sql_id_list(host_ids) |
| 343 | rows = _db.execute(query) |
| 344 | labels_to_hosts = cls._process_many2many_dict(rows) |
| 345 | hosts_to_labels = cls._process_many2many_dict(rows, flip=True) |
| 346 | return labels_to_hosts, hosts_to_labels |
| 347 | |
| 348 | |
| 349 | @classmethod |
| 350 | def _get_labels(cls): |
| 351 | return dict((label.id, label) for label in Label.fetch()) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 352 | |
| 353 | |
| 354 | def refresh(self, pending_queue_entries): |
| 355 | self._hosts_available = self._get_ready_hosts() |
| 356 | |
| 357 | relevant_jobs = [queue_entry.job_id |
| 358 | for queue_entry in pending_queue_entries] |
| 359 | self._job_acls = self._get_job_acl_groups(relevant_jobs) |
| 360 | self._ineligible_hosts = self._get_job_ineligible_hosts(relevant_jobs) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 361 | self._job_dependencies = self._get_job_dependencies(relevant_jobs) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 362 | |
| 363 | host_ids = self._hosts_available.keys() |
| 364 | self._host_acls = self._get_host_acls(host_ids) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 365 | self._label_hosts, self._host_labels = self._get_label_hosts(host_ids) |
| 366 | |
| 367 | self._labels = self._get_labels() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 368 | |
| 369 | |
| 370 | def _is_acl_accessible(self, host_id, queue_entry): |
| 371 | job_acls = self._job_acls.get(queue_entry.job_id, set()) |
| 372 | host_acls = self._host_acls.get(host_id, set()) |
| 373 | return len(host_acls.intersection(job_acls)) > 0 |
| 374 | |
| 375 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 376 | def _check_job_dependencies(self, job_dependencies, host_labels): |
| 377 | missing = job_dependencies - host_labels |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 378 | return len(missing) == 0 |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 379 | |
| 380 | |
| 381 | def _check_only_if_needed_labels(self, job_dependencies, host_labels, |
| 382 | queue_entry): |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 383 | if not queue_entry.meta_host: |
| 384 | # bypass only_if_needed labels when a specific host is selected |
| 385 | return True |
| 386 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 387 | for label_id in host_labels: |
| 388 | label = self._labels[label_id] |
| 389 | if not label.only_if_needed: |
| 390 | # we don't care about non-only_if_needed labels |
| 391 | continue |
| 392 | if queue_entry.meta_host == label_id: |
| 393 | # if the label was requested in a metahost it's OK |
| 394 | continue |
| 395 | if label_id not in job_dependencies: |
| 396 | return False |
| 397 | return True |
| 398 | |
| 399 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 400 | def _check_atomic_group_labels(self, host_labels, queue_entry): |
| 401 | """ |
| 402 | Determine if the given HostQueueEntry's atomic group settings are okay |
| 403 | to schedule on a host with the given labels. |
| 404 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 405 | @param host_labels: A list of label ids that the host has. |
| 406 | @param queue_entry: The HostQueueEntry being considered for the host. |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 407 | |
| 408 | @returns True if atomic group settings are okay, False otherwise. |
| 409 | """ |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 410 | return (self._get_host_atomic_group_id(host_labels, queue_entry) == |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 411 | queue_entry.atomic_group_id) |
| 412 | |
| 413 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 414 | def _get_host_atomic_group_id(self, host_labels, queue_entry=None): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 415 | """ |
| 416 | Return the atomic group label id for a host with the given set of |
| 417 | labels if any, or None otherwise. Raises an exception if more than |
| 418 | one atomic group are found in the set of labels. |
| 419 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 420 | @param host_labels: A list of label ids that the host has. |
| 421 | @param queue_entry: The HostQueueEntry we're testing. Only used for |
| 422 | extra info in a potential logged error message. |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 423 | |
| 424 | @returns The id of the atomic group found on a label in host_labels |
| 425 | or None if no atomic group label is found. |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 426 | """ |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 427 | atomic_labels = [self._labels[label_id] for label_id in host_labels |
| 428 | if self._labels[label_id].atomic_group_id is not None] |
| 429 | atomic_ids = set(label.atomic_group_id for label in atomic_labels) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 430 | if not atomic_ids: |
| 431 | return None |
| 432 | if len(atomic_ids) > 1: |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 433 | logging.error('More than one Atomic Group on HQE "%s" via: %r', |
| 434 | queue_entry, atomic_labels) |
| 435 | return atomic_ids.pop() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 436 | |
| 437 | |
| 438 | def _get_atomic_group_labels(self, atomic_group_id): |
| 439 | """ |
| 440 | Lookup the label ids that an atomic_group is associated with. |
| 441 | |
| 442 | @param atomic_group_id - The id of the AtomicGroup to look up. |
| 443 | |
| 444 | @returns A generator yeilding Label ids for this atomic group. |
| 445 | """ |
| 446 | return (id for id, label in self._labels.iteritems() |
| 447 | if label.atomic_group_id == atomic_group_id |
| 448 | and not label.invalid) |
| 449 | |
| 450 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 451 | def _get_eligible_host_ids_in_group(self, group_hosts, queue_entry): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 452 | """ |
| 453 | @param group_hosts - A sequence of Host ids to test for usability |
| 454 | and eligibility against the Job associated with queue_entry. |
| 455 | @param queue_entry - The HostQueueEntry that these hosts are being |
| 456 | tested for eligibility against. |
| 457 | |
| 458 | @returns A subset of group_hosts Host ids that are eligible for the |
| 459 | supplied queue_entry. |
| 460 | """ |
| 461 | return set(host_id for host_id in group_hosts |
| 462 | if self._is_host_usable(host_id) |
| 463 | and self._is_host_eligible_for_job(host_id, queue_entry)) |
| 464 | |
| 465 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 466 | def _is_host_eligible_for_job(self, host_id, queue_entry): |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 467 | if self._is_host_invalid(host_id): |
| 468 | # if an invalid host is scheduled for a job, it's a one-time host |
| 469 | # and it therefore bypasses eligibility checks. note this can only |
| 470 | # happen for non-metahosts, because invalid hosts have their label |
| 471 | # relationships cleared. |
| 472 | return True |
| 473 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 474 | job_dependencies = self._job_dependencies.get(queue_entry.job_id, set()) |
| 475 | host_labels = self._host_labels.get(host_id, set()) |
mbligh | c993bee | 2008-10-03 03:42:34 +0000 | [diff] [blame] | 476 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 477 | return (self._is_acl_accessible(host_id, queue_entry) and |
| 478 | self._check_job_dependencies(job_dependencies, host_labels) and |
| 479 | self._check_only_if_needed_labels( |
| 480 | job_dependencies, host_labels, queue_entry) and |
| 481 | self._check_atomic_group_labels(host_labels, queue_entry)) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 482 | |
| 483 | |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 484 | def _is_host_invalid(self, host_id): |
| 485 | host_object = self._hosts_available.get(host_id, None) |
| 486 | return host_object and host_object.invalid |
| 487 | |
| 488 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 489 | def _schedule_non_metahost(self, queue_entry): |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 490 | if not self._is_host_eligible_for_job(queue_entry.host_id, queue_entry): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 491 | return None |
| 492 | return self._hosts_available.pop(queue_entry.host_id, None) |
| 493 | |
| 494 | |
| 495 | def _is_host_usable(self, host_id): |
| 496 | if host_id not in self._hosts_available: |
| 497 | # host was already used during this scheduling cycle |
| 498 | return False |
| 499 | if self._hosts_available[host_id].invalid: |
| 500 | # Invalid hosts cannot be used for metahosts. They're included in |
| 501 | # the original query because they can be used by non-metahosts. |
| 502 | return False |
| 503 | return True |
| 504 | |
| 505 | |
| 506 | def _schedule_metahost(self, queue_entry): |
| 507 | label_id = queue_entry.meta_host |
| 508 | hosts_in_label = self._label_hosts.get(label_id, set()) |
| 509 | ineligible_host_ids = self._ineligible_hosts.get(queue_entry.job_id, |
| 510 | set()) |
| 511 | |
| 512 | # must iterate over a copy so we can mutate the original while iterating |
| 513 | for host_id in list(hosts_in_label): |
| 514 | if not self._is_host_usable(host_id): |
| 515 | hosts_in_label.remove(host_id) |
| 516 | continue |
| 517 | if host_id in ineligible_host_ids: |
| 518 | continue |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 519 | if not self._is_host_eligible_for_job(host_id, queue_entry): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 520 | continue |
| 521 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 522 | # Remove the host from our cached internal state before returning |
| 523 | # the host object. |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 524 | hosts_in_label.remove(host_id) |
| 525 | return self._hosts_available.pop(host_id) |
| 526 | return None |
| 527 | |
| 528 | |
| 529 | def find_eligible_host(self, queue_entry): |
| 530 | if not queue_entry.meta_host: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 531 | assert queue_entry.host_id is not None |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 532 | return self._schedule_non_metahost(queue_entry) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 533 | assert queue_entry.atomic_group_id is None |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 534 | return self._schedule_metahost(queue_entry) |
| 535 | |
| 536 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 537 | def find_eligible_atomic_group(self, queue_entry): |
| 538 | """ |
| 539 | Given an atomic group host queue entry, locate an appropriate group |
| 540 | of hosts for the associated job to run on. |
| 541 | |
| 542 | The caller is responsible for creating new HQEs for the additional |
| 543 | hosts returned in order to run the actual job on them. |
| 544 | |
| 545 | @returns A list of Host instances in a ready state to satisfy this |
| 546 | atomic group scheduling. Hosts will all belong to the same |
| 547 | atomic group label as specified by the queue_entry. |
| 548 | An empty list will be returned if no suitable atomic |
| 549 | group could be found. |
| 550 | |
| 551 | TODO(gps): what is responsible for kicking off any attempted repairs on |
| 552 | a group of hosts? not this function, but something needs to. We do |
| 553 | not communicate that reason for returning [] outside of here... |
| 554 | For now, we'll just be unschedulable if enough hosts within one group |
| 555 | enter Repair Failed state. |
| 556 | """ |
| 557 | assert queue_entry.atomic_group_id is not None |
| 558 | job = queue_entry.job |
| 559 | assert job.synch_count and job.synch_count > 0 |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 560 | atomic_group = queue_entry.atomic_group |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 561 | if job.synch_count > atomic_group.max_number_of_machines: |
| 562 | # Such a Job and HostQueueEntry should never be possible to |
| 563 | # create using the frontend. Regardless, we can't process it. |
| 564 | # Abort it immediately and log an error on the scheduler. |
| 565 | queue_entry.set_status(models.HostQueueEntry.Status.ABORTED) |
showard | 7629f14 | 2009-03-27 21:02:02 +0000 | [diff] [blame] | 566 | logging.error( |
| 567 | 'Error: job %d synch_count=%d > requested atomic_group %d ' |
| 568 | 'max_number_of_machines=%d. Aborted host_queue_entry %d.', |
| 569 | job.id, job.synch_count, atomic_group.id, |
| 570 | atomic_group.max_number_of_machines, queue_entry.id) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 571 | return [] |
| 572 | hosts_in_label = self._label_hosts.get(queue_entry.meta_host, set()) |
| 573 | ineligible_host_ids = self._ineligible_hosts.get(queue_entry.job_id, |
| 574 | set()) |
| 575 | |
| 576 | # Look in each label associated with atomic_group until we find one with |
| 577 | # enough hosts to satisfy the job. |
| 578 | for atomic_label_id in self._get_atomic_group_labels(atomic_group.id): |
| 579 | group_hosts = set(self._label_hosts.get(atomic_label_id, set())) |
| 580 | if queue_entry.meta_host is not None: |
| 581 | # If we have a metahost label, only allow its hosts. |
| 582 | group_hosts.intersection_update(hosts_in_label) |
| 583 | group_hosts -= ineligible_host_ids |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 584 | eligible_host_ids_in_group = self._get_eligible_host_ids_in_group( |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 585 | group_hosts, queue_entry) |
| 586 | |
| 587 | # Job.synch_count is treated as "minimum synch count" when |
| 588 | # scheduling for an atomic group of hosts. The atomic group |
| 589 | # number of machines is the maximum to pick out of a single |
| 590 | # atomic group label for scheduling at one time. |
| 591 | min_hosts = job.synch_count |
| 592 | max_hosts = atomic_group.max_number_of_machines |
| 593 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 594 | if len(eligible_host_ids_in_group) < min_hosts: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 595 | # Not enough eligible hosts in this atomic group label. |
| 596 | continue |
| 597 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 598 | eligible_hosts_in_group = [self._hosts_available[id] |
| 599 | for id in eligible_host_ids_in_group] |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 600 | # So that they show up in a sane order when viewing the job. |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 601 | eligible_hosts_in_group.sort(cmp=Host.cmp_for_sort) |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 602 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 603 | # Limit ourselves to scheduling the atomic group size. |
| 604 | if len(eligible_hosts_in_group) > max_hosts: |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 605 | eligible_hosts_in_group = eligible_hosts_in_group[:max_hosts] |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 606 | |
| 607 | # Remove the selected hosts from our cached internal state |
| 608 | # of available hosts in order to return the Host objects. |
| 609 | host_list = [] |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 610 | for host in eligible_hosts_in_group: |
| 611 | hosts_in_label.discard(host.id) |
| 612 | self._hosts_available.pop(host.id) |
| 613 | host_list.append(host) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 614 | return host_list |
| 615 | |
| 616 | return [] |
| 617 | |
| 618 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 619 | class Dispatcher(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 620 | def __init__(self): |
| 621 | self._agents = [] |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 622 | self._last_clean_time = time.time() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 623 | self._host_scheduler = HostScheduler() |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 624 | user_cleanup_time = scheduler_config.config.clean_interval |
| 625 | self._periodic_cleanup = monitor_db_cleanup.UserCleanup( |
| 626 | _db, user_cleanup_time) |
| 627 | self._24hr_upkeep = monitor_db_cleanup.TwentyFourHourUpkeep(_db) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 628 | self._host_agents = {} |
| 629 | self._queue_entry_agents = {} |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 630 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 631 | |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 632 | def initialize(self, recover_hosts=True): |
| 633 | self._periodic_cleanup.initialize() |
| 634 | self._24hr_upkeep.initialize() |
| 635 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 636 | # always recover processes |
| 637 | self._recover_processes() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 638 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 639 | if recover_hosts: |
| 640 | self._recover_hosts() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 641 | |
| 642 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 643 | def tick(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 644 | _drone_manager.refresh() |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 645 | self._run_cleanup() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 646 | self._find_aborting() |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 647 | self._process_recurring_runs() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 648 | self._schedule_delay_tasks() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 649 | self._schedule_running_host_queue_entries() |
| 650 | self._schedule_special_tasks() |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 651 | self._schedule_new_jobs() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 652 | self._handle_agents() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 653 | _drone_manager.execute_actions() |
| 654 | email_manager.manager.send_queued_emails() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 655 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 656 | |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 657 | def _run_cleanup(self): |
| 658 | self._periodic_cleanup.run_cleanup_maybe() |
| 659 | self._24hr_upkeep.run_cleanup_maybe() |
showard | a3ab0d5 | 2008-11-03 19:03:47 +0000 | [diff] [blame] | 660 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 661 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 662 | def _register_agent_for_ids(self, agent_dict, object_ids, agent): |
| 663 | for object_id in object_ids: |
| 664 | agent_dict.setdefault(object_id, set()).add(agent) |
| 665 | |
| 666 | |
| 667 | def _unregister_agent_for_ids(self, agent_dict, object_ids, agent): |
| 668 | for object_id in object_ids: |
| 669 | assert object_id in agent_dict |
| 670 | agent_dict[object_id].remove(agent) |
| 671 | |
| 672 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 673 | def add_agent(self, agent): |
| 674 | self._agents.append(agent) |
| 675 | agent.dispatcher = self |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 676 | self._register_agent_for_ids(self._host_agents, agent.host_ids, agent) |
| 677 | self._register_agent_for_ids(self._queue_entry_agents, |
| 678 | agent.queue_entry_ids, agent) |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 679 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 680 | |
| 681 | def get_agents_for_entry(self, queue_entry): |
| 682 | """ |
| 683 | Find agents corresponding to the specified queue_entry. |
| 684 | """ |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 685 | return list(self._queue_entry_agents.get(queue_entry.id, set())) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 686 | |
| 687 | |
| 688 | def host_has_agent(self, host): |
| 689 | """ |
| 690 | Determine if there is currently an Agent present using this host. |
| 691 | """ |
| 692 | return bool(self._host_agents.get(host.id, None)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 693 | |
| 694 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 695 | def remove_agent(self, agent): |
| 696 | self._agents.remove(agent) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 697 | self._unregister_agent_for_ids(self._host_agents, agent.host_ids, |
| 698 | agent) |
| 699 | self._unregister_agent_for_ids(self._queue_entry_agents, |
| 700 | agent.queue_entry_ids, agent) |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 701 | |
| 702 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 703 | def _host_has_scheduled_special_task(self, host): |
| 704 | return bool(models.SpecialTask.objects.filter(host__id=host.id, |
| 705 | is_active=False, |
| 706 | is_complete=False)) |
| 707 | |
| 708 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 709 | def _recover_processes(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 710 | self._register_pidfiles() |
| 711 | _drone_manager.refresh() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 712 | self._recover_all_recoverable_entries() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 713 | self._recover_pending_entries() |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 714 | self._check_for_unrecovered_verifying_entries() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 715 | self._reverify_remaining_hosts() |
| 716 | # reinitialize drones after killing orphaned processes, since they can |
| 717 | # leave around files when they die |
| 718 | _drone_manager.execute_actions() |
| 719 | _drone_manager.reinitialize_drones() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 720 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 721 | |
| 722 | def _register_pidfiles(self): |
| 723 | # during recovery we may need to read pidfiles for both running and |
| 724 | # parsing entries |
| 725 | queue_entries = HostQueueEntry.fetch( |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 726 | where="status IN ('Running', 'Gathering', 'Parsing')") |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 727 | special_tasks = models.SpecialTask.objects.filter(is_active=True) |
| 728 | for execution_entry in itertools.chain(queue_entries, special_tasks): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 729 | for pidfile_name in _ALL_PIDFILE_NAMES: |
| 730 | pidfile_id = _drone_manager.get_pidfile_id_from( |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 731 | execution_entry.execution_path(), pidfile_name=pidfile_name) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 732 | _drone_manager.register_pidfile(pidfile_id) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 733 | |
| 734 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 735 | def _get_recovery_run_monitor(self, execution_path, pidfile_name, orphans): |
| 736 | run_monitor = PidfileRunMonitor() |
| 737 | run_monitor.attach_to_existing_process(execution_path, |
| 738 | pidfile_name=pidfile_name) |
| 739 | if run_monitor.has_process(): |
| 740 | orphans.discard(run_monitor.get_process()) |
| 741 | return run_monitor, '(process %s)' % run_monitor.get_process() |
| 742 | return None, 'without process' |
| 743 | |
| 744 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 745 | def _get_unassigned_entries(self, status): |
| 746 | for entry in HostQueueEntry.fetch(where="status = '%s'" % status): |
showard | 0db3d43 | 2009-10-12 20:29:15 +0000 | [diff] [blame] | 747 | if entry.status == status and not self.get_agents_for_entry(entry): |
| 748 | # The status can change during iteration, e.g., if job.run() |
| 749 | # sets a group of queue entries to Starting |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 750 | yield entry |
| 751 | |
| 752 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 753 | def _recover_entries_with_status(self, status, orphans, pidfile_name, |
| 754 | recover_entries_fn): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 755 | for queue_entry in self._get_unassigned_entries(status): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 756 | queue_entries = queue_entry.job.get_group_entries(queue_entry) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 757 | run_monitor, process_string = self._get_recovery_run_monitor( |
| 758 | queue_entry.execution_path(), pidfile_name, orphans) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 759 | if not run_monitor: |
| 760 | # _schedule_running_host_queue_entries should schedule and |
| 761 | # recover these entries |
| 762 | continue |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 763 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 764 | logging.info('Recovering %s entry %s %s',status.lower(), |
| 765 | ', '.join(str(entry) for entry in queue_entries), |
| 766 | process_string) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 767 | recover_entries_fn(queue_entry.job, queue_entries, run_monitor) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 768 | |
| 769 | |
showard | 6878e8b | 2009-07-20 22:37:45 +0000 | [diff] [blame] | 770 | def _check_for_remaining_orphan_processes(self, orphans): |
| 771 | if not orphans: |
| 772 | return |
| 773 | subject = 'Unrecovered orphan autoserv processes remain' |
| 774 | message = '\n'.join(str(process) for process in orphans) |
| 775 | email_manager.manager.enqueue_notify_email(subject, message) |
mbligh | 5fa9e11 | 2009-08-03 16:46:06 +0000 | [diff] [blame] | 776 | |
| 777 | die_on_orphans = global_config.global_config.get_config_value( |
| 778 | scheduler_config.CONFIG_SECTION, 'die_on_orphans', type=bool) |
| 779 | |
| 780 | if die_on_orphans: |
| 781 | raise RuntimeError(subject + '\n' + message) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 782 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 783 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 784 | def _recover_running_entries(self, orphans): |
| 785 | def recover_entries(job, queue_entries, run_monitor): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 786 | queue_task = QueueTask(job=job, queue_entries=queue_entries, |
| 787 | recover_run_monitor=run_monitor) |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 788 | self.add_agent(Agent(task=queue_task)) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 789 | |
| 790 | self._recover_entries_with_status(models.HostQueueEntry.Status.RUNNING, |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 791 | orphans, _AUTOSERV_PID_FILE, |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 792 | recover_entries) |
| 793 | |
| 794 | |
| 795 | def _recover_gathering_entries(self, orphans): |
| 796 | def recover_entries(job, queue_entries, run_monitor): |
| 797 | gather_task = GatherLogsTask(job, queue_entries, |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 798 | recover_run_monitor=run_monitor) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 799 | self.add_agent(Agent(gather_task)) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 800 | |
| 801 | self._recover_entries_with_status( |
| 802 | models.HostQueueEntry.Status.GATHERING, |
| 803 | orphans, _CRASHINFO_PID_FILE, recover_entries) |
| 804 | |
| 805 | |
| 806 | def _recover_parsing_entries(self, orphans): |
| 807 | def recover_entries(job, queue_entries, run_monitor): |
| 808 | reparse_task = FinalReparseTask(queue_entries, |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 809 | recover_run_monitor=run_monitor) |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 810 | self.add_agent(Agent(reparse_task)) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 811 | |
| 812 | self._recover_entries_with_status(models.HostQueueEntry.Status.PARSING, |
| 813 | orphans, _PARSER_PID_FILE, |
| 814 | recover_entries) |
| 815 | |
| 816 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 817 | def _recover_pending_entries(self): |
| 818 | for entry in self._get_unassigned_entries( |
| 819 | models.HostQueueEntry.Status.PENDING): |
showard | 5682407 | 2009-10-12 20:30:21 +0000 | [diff] [blame] | 820 | logging.info('Recovering Pending entry %s', entry) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 821 | entry.on_pending() |
| 822 | |
| 823 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 824 | def _recover_all_recoverable_entries(self): |
| 825 | orphans = _drone_manager.get_orphaned_autoserv_processes() |
| 826 | self._recover_running_entries(orphans) |
| 827 | self._recover_gathering_entries(orphans) |
| 828 | self._recover_parsing_entries(orphans) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 829 | self._recover_special_tasks(orphans) |
showard | 6878e8b | 2009-07-20 22:37:45 +0000 | [diff] [blame] | 830 | self._check_for_remaining_orphan_processes(orphans) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 831 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 832 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 833 | def _recover_special_tasks(self, orphans): |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 834 | """\ |
| 835 | Recovers all special tasks that have started running but have not |
| 836 | completed. |
| 837 | """ |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 838 | tasks = models.SpecialTask.objects.filter(is_active=True, |
| 839 | is_complete=False) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 840 | for task in tasks: |
showard | 9b6ec50 | 2009-08-20 23:25:17 +0000 | [diff] [blame] | 841 | if self.host_has_agent(task.host): |
| 842 | raise SchedulerError( |
| 843 | "%s already has a host agent %s." % ( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 844 | task, self._host_agents.get(task.host.id))) |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 845 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 846 | run_monitor, process_string = self._get_recovery_run_monitor( |
| 847 | task.execution_path(), _AUTOSERV_PID_FILE, orphans) |
| 848 | |
| 849 | logging.info('Recovering %s %s', task, process_string) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 850 | self._run_or_recover_special_task(task, run_monitor=run_monitor) |
showard | 6af73ad | 2009-07-28 20:00:58 +0000 | [diff] [blame] | 851 | |
| 852 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 853 | def _check_for_unrecovered_verifying_entries(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 854 | queue_entries = HostQueueEntry.fetch( |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 855 | where='status = "%s"' % models.HostQueueEntry.Status.VERIFYING) |
| 856 | unrecovered_hqes = [] |
| 857 | for queue_entry in queue_entries: |
| 858 | special_tasks = models.SpecialTask.objects.filter( |
| 859 | task__in=(models.SpecialTask.Task.CLEANUP, |
| 860 | models.SpecialTask.Task.VERIFY), |
| 861 | queue_entry__id=queue_entry.id, |
| 862 | is_complete=False) |
| 863 | if special_tasks.count() == 0: |
| 864 | unrecovered_hqes.append(queue_entry) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 865 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 866 | if unrecovered_hqes: |
| 867 | message = '\n'.join(str(hqe) for hqe in unrecovered_hqes) |
showard | e8e3707 | 2009-08-20 23:31:30 +0000 | [diff] [blame] | 868 | raise SchedulerError( |
showard | 37757f3 | 2009-10-19 18:34:24 +0000 | [diff] [blame] | 869 | '%d unrecovered verifying host queue entries:\n%s' % |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 870 | (len(unrecovered_hqes), message)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 871 | |
| 872 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 873 | def _get_prioritized_special_tasks(self): |
| 874 | """ |
| 875 | Returns all queued SpecialTasks prioritized for repair first, then |
| 876 | cleanup, then verify. |
| 877 | """ |
| 878 | queued_tasks = models.SpecialTask.objects.filter(is_active=False, |
| 879 | is_complete=False, |
| 880 | host__locked=False) |
| 881 | # exclude hosts with active queue entries unless the SpecialTask is for |
| 882 | # that queue entry |
| 883 | queued_tasks = models.Host.objects.add_join( |
| 884 | queued_tasks, 'host_queue_entries', 'host_id', |
| 885 | join_condition='host_queue_entries.active', |
| 886 | force_left_join=True) |
| 887 | queued_tasks = queued_tasks.extra( |
| 888 | where=['(host_queue_entries.id IS NULL OR ' |
| 889 | 'host_queue_entries.id = special_tasks.queue_entry_id)']) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 890 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 891 | # reorder tasks by priority |
| 892 | task_priority_order = [models.SpecialTask.Task.REPAIR, |
| 893 | models.SpecialTask.Task.CLEANUP, |
| 894 | models.SpecialTask.Task.VERIFY] |
| 895 | def task_priority_key(task): |
| 896 | return task_priority_order.index(task.task) |
| 897 | return sorted(queued_tasks, key=task_priority_key) |
| 898 | |
| 899 | |
| 900 | def _run_or_recover_special_task(self, special_task, run_monitor=None): |
| 901 | """ |
| 902 | Construct an AgentTask class to run the given SpecialTask and add it |
| 903 | to this dispatcher. |
| 904 | @param special_task: a models.SpecialTask instance |
| 905 | @run_monitor: if given, a running SpecialTask will be recovered with |
| 906 | this monitor. |
| 907 | """ |
| 908 | special_agent_task_classes = (CleanupTask, VerifyTask, RepairTask) |
| 909 | for agent_task_class in special_agent_task_classes: |
| 910 | if agent_task_class.TASK_TYPE == special_task.task: |
| 911 | agent_task = agent_task_class(task=special_task, |
| 912 | recover_run_monitor=run_monitor) |
| 913 | self.add_agent(Agent(agent_task)) |
| 914 | return |
| 915 | |
| 916 | email_manager.manager.enqueue_notify_email( |
| 917 | 'No AgentTask class for task', str(special_task)) |
| 918 | |
| 919 | |
| 920 | def _schedule_special_tasks(self): |
| 921 | """ |
| 922 | Execute queued SpecialTasks that are ready to run on idle hosts. |
| 923 | """ |
| 924 | for task in self._get_prioritized_special_tasks(): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 925 | if self.host_has_agent(task.host): |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 926 | continue |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 927 | self._run_or_recover_special_task(task) |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 928 | |
| 929 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 930 | def _reverify_remaining_hosts(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 931 | # recover active hosts that have not yet been recovered, although this |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 932 | # should never happen |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 933 | message = ('Recovering active host %s - this probably indicates a ' |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 934 | 'scheduler bug') |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 935 | self._reverify_hosts_where( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 936 | "status IN ('Repairing', 'Verifying', 'Cleaning')", |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 937 | print_message=message) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 938 | |
| 939 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 940 | def _reverify_hosts_where(self, where, |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 941 | print_message='Reverifying host %s'): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 942 | full_where='locked = 0 AND invalid = 0 AND ' + where |
| 943 | for host in Host.fetch(where=full_where): |
| 944 | if self.host_has_agent(host): |
| 945 | # host has already been recovered in some way |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 946 | continue |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 947 | if self._host_has_scheduled_special_task(host): |
| 948 | # host will have a special task scheduled on the next cycle |
| 949 | continue |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 950 | if print_message: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 951 | logging.info(print_message, host.hostname) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 952 | models.SpecialTask.objects.create( |
| 953 | task=models.SpecialTask.Task.CLEANUP, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 954 | host=models.Host.objects.get(id=host.id)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 955 | |
| 956 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 957 | def _recover_hosts(self): |
| 958 | # recover "Repair Failed" hosts |
| 959 | message = 'Reverifying dead host %s' |
| 960 | self._reverify_hosts_where("status = 'Repair Failed'", |
| 961 | print_message=message) |
mbligh | 62ba2ed | 2008-04-30 17:09:25 +0000 | [diff] [blame] | 962 | |
| 963 | |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 964 | |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 965 | def _get_pending_queue_entries(self): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 966 | # prioritize by job priority, then non-metahost over metahost, then FIFO |
| 967 | return list(HostQueueEntry.fetch( |
showard | 25cbdbd | 2009-02-17 20:57:21 +0000 | [diff] [blame] | 968 | joins='INNER JOIN jobs ON (job_id=jobs.id)', |
showard | ac9ce22 | 2008-12-03 18:19:44 +0000 | [diff] [blame] | 969 | where='NOT complete AND NOT active AND status="Queued"', |
showard | 25cbdbd | 2009-02-17 20:57:21 +0000 | [diff] [blame] | 970 | order_by='jobs.priority DESC, meta_host, job_id')) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 971 | |
| 972 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 973 | def _refresh_pending_queue_entries(self): |
| 974 | """ |
| 975 | Lookup the pending HostQueueEntries and call our HostScheduler |
| 976 | refresh() method given that list. Return the list. |
| 977 | |
| 978 | @returns A list of pending HostQueueEntries sorted in priority order. |
| 979 | """ |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 980 | queue_entries = self._get_pending_queue_entries() |
| 981 | if not queue_entries: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 982 | return [] |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 983 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 984 | self._host_scheduler.refresh(queue_entries) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 985 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 986 | return queue_entries |
| 987 | |
| 988 | |
| 989 | def _schedule_atomic_group(self, queue_entry): |
| 990 | """ |
| 991 | Schedule the given queue_entry on an atomic group of hosts. |
| 992 | |
| 993 | Returns immediately if there are insufficient available hosts. |
| 994 | |
| 995 | Creates new HostQueueEntries based off of queue_entry for the |
| 996 | scheduled hosts and starts them all running. |
| 997 | """ |
| 998 | # This is a virtual host queue entry representing an entire |
| 999 | # atomic group, find a group and schedule their hosts. |
| 1000 | group_hosts = self._host_scheduler.find_eligible_atomic_group( |
| 1001 | queue_entry) |
| 1002 | if not group_hosts: |
| 1003 | return |
showard | cbe6f94 | 2009-06-17 19:33:49 +0000 | [diff] [blame] | 1004 | |
| 1005 | logging.info('Expanding atomic group entry %s with hosts %s', |
| 1006 | queue_entry, |
| 1007 | ', '.join(host.hostname for host in group_hosts)) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1008 | # The first assigned host uses the original HostQueueEntry |
| 1009 | group_queue_entries = [queue_entry] |
| 1010 | for assigned_host in group_hosts[1:]: |
| 1011 | # Create a new HQE for every additional assigned_host. |
| 1012 | new_hqe = HostQueueEntry.clone(queue_entry) |
| 1013 | new_hqe.save() |
| 1014 | group_queue_entries.append(new_hqe) |
| 1015 | assert len(group_queue_entries) == len(group_hosts) |
| 1016 | for queue_entry, host in itertools.izip(group_queue_entries, |
| 1017 | group_hosts): |
| 1018 | self._run_queue_entry(queue_entry, host) |
| 1019 | |
| 1020 | |
| 1021 | def _schedule_new_jobs(self): |
| 1022 | queue_entries = self._refresh_pending_queue_entries() |
| 1023 | if not queue_entries: |
| 1024 | return |
| 1025 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 1026 | for queue_entry in queue_entries: |
showard | e55955f | 2009-10-07 20:48:58 +0000 | [diff] [blame] | 1027 | is_unassigned_atomic_group = ( |
| 1028 | queue_entry.atomic_group_id is not None |
| 1029 | and queue_entry.host_id is None) |
| 1030 | if is_unassigned_atomic_group: |
| 1031 | self._schedule_atomic_group(queue_entry) |
| 1032 | else: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1033 | assigned_host = self._host_scheduler.find_eligible_host( |
| 1034 | queue_entry) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 1035 | if assigned_host and not self.host_has_agent(assigned_host): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1036 | self._run_queue_entry(queue_entry, assigned_host) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 1037 | |
| 1038 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1039 | def _schedule_running_host_queue_entries(self): |
showard | 8375ce0 | 2009-10-12 20:35:13 +0000 | [diff] [blame] | 1040 | status_enum = models.HostQueueEntry.Status |
| 1041 | running_statuses = (status_enum.STARTING, status_enum.RUNNING, |
| 1042 | status_enum.GATHERING, status_enum.PARSING) |
| 1043 | sql_statuses = ', '.join(('"%s"' % s for s in running_statuses)) |
| 1044 | entries = HostQueueEntry.fetch(where="status IN (%s)" % sql_statuses) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1045 | for entry in entries: |
| 1046 | if self.get_agents_for_entry(entry): |
| 1047 | continue |
| 1048 | |
| 1049 | task_entries = entry.job.get_group_entries(entry) |
| 1050 | for task_entry in task_entries: |
| 1051 | if (task_entry.status != models.HostQueueEntry.Status.PARSING |
| 1052 | and self.host_has_agent(task_entry.host)): |
showard | 8375ce0 | 2009-10-12 20:35:13 +0000 | [diff] [blame] | 1053 | agent = tuple(self._host_agents.get(task_entry.host.id))[0] |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1054 | raise SchedulerError('Attempted to schedule on host that ' |
| 1055 | 'already has agent: %s (previous ' |
| 1056 | 'agent task: %s)' |
| 1057 | % (task_entry, agent.task)) |
| 1058 | |
| 1059 | if entry.status in (models.HostQueueEntry.Status.STARTING, |
| 1060 | models.HostQueueEntry.Status.RUNNING): |
| 1061 | params = entry.job.get_autoserv_params(task_entries) |
| 1062 | agent_task = QueueTask(job=entry.job, |
| 1063 | queue_entries=task_entries, cmd=params) |
| 1064 | elif entry.status == models.HostQueueEntry.Status.GATHERING: |
| 1065 | agent_task = GatherLogsTask( |
| 1066 | job=entry.job, queue_entries=task_entries) |
| 1067 | elif entry.status == models.HostQueueEntry.Status.PARSING: |
| 1068 | agent_task = FinalReparseTask(queue_entries=task_entries) |
| 1069 | else: |
| 1070 | raise SchedulerError('_schedule_running_host_queue_entries got ' |
| 1071 | 'entry with invalid status %s: %s' |
| 1072 | % (entry.status, entry)) |
| 1073 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1074 | self.add_agent(Agent(agent_task)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1075 | |
| 1076 | |
| 1077 | def _schedule_delay_tasks(self): |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 1078 | for entry in HostQueueEntry.fetch(where='status = "%s"' % |
| 1079 | models.HostQueueEntry.Status.WAITING): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1080 | task = entry.job.schedule_delayed_callback_task(entry) |
| 1081 | if task: |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1082 | self.add_agent(Agent(task)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1083 | |
| 1084 | |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 1085 | def _run_queue_entry(self, queue_entry, host): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1086 | queue_entry.schedule_pre_job_tasks(assigned_host=host) |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1087 | |
| 1088 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1089 | def _find_aborting(self): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1090 | for entry in HostQueueEntry.fetch(where='aborted and not complete'): |
showard | f4a2e50 | 2009-07-28 20:06:39 +0000 | [diff] [blame] | 1091 | logging.info('Aborting %s', entry) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1092 | for agent in self.get_agents_for_entry(entry): |
| 1093 | agent.abort() |
| 1094 | entry.abort(self) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1095 | |
| 1096 | |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 1097 | def _can_start_agent(self, agent, num_started_this_cycle, |
| 1098 | have_reached_limit): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1099 | # always allow zero-process agents to run |
| 1100 | if agent.num_processes == 0: |
| 1101 | return True |
| 1102 | # don't allow any nonzero-process agents to run after we've reached a |
| 1103 | # limit (this avoids starvation of many-process agents) |
| 1104 | if have_reached_limit: |
| 1105 | return False |
| 1106 | # total process throttling |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1107 | max_runnable_processes = _drone_manager.max_runnable_processes( |
| 1108 | agent.task.username) |
| 1109 | if agent.num_processes > max_runnable_processes: |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1110 | return False |
| 1111 | # if a single agent exceeds the per-cycle throttling, still allow it to |
| 1112 | # run when it's the first agent in the cycle |
| 1113 | if num_started_this_cycle == 0: |
| 1114 | return True |
| 1115 | # per-cycle throttling |
| 1116 | if (num_started_this_cycle + agent.num_processes > |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 1117 | scheduler_config.config.max_processes_started_per_cycle): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1118 | return False |
| 1119 | return True |
| 1120 | |
| 1121 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1122 | def _handle_agents(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1123 | num_started_this_cycle = 0 |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1124 | have_reached_limit = False |
| 1125 | # iterate over copy, so we can remove agents during iteration |
| 1126 | for agent in list(self._agents): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1127 | if not agent.started: |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 1128 | if not self._can_start_agent(agent, num_started_this_cycle, |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1129 | have_reached_limit): |
| 1130 | have_reached_limit = True |
| 1131 | continue |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1132 | num_started_this_cycle += agent.num_processes |
| 1133 | agent.tick() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1134 | if agent.is_done(): |
| 1135 | logging.info("agent finished") |
| 1136 | self.remove_agent(agent) |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1137 | logging.info('%d running processes', |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1138 | _drone_manager.total_running_processes()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1139 | |
| 1140 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1141 | def _process_recurring_runs(self): |
| 1142 | recurring_runs = models.RecurringRun.objects.filter( |
| 1143 | start_date__lte=datetime.datetime.now()) |
| 1144 | for rrun in recurring_runs: |
| 1145 | # Create job from template |
| 1146 | job = rrun.job |
| 1147 | info = rpc_utils.get_job_info(job) |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1148 | options = job.get_object_dict() |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1149 | |
| 1150 | host_objects = info['hosts'] |
| 1151 | one_time_hosts = info['one_time_hosts'] |
| 1152 | metahost_objects = info['meta_hosts'] |
| 1153 | dependencies = info['dependencies'] |
| 1154 | atomic_group = info['atomic_group'] |
| 1155 | |
| 1156 | for host in one_time_hosts or []: |
| 1157 | this_host = models.Host.create_one_time_host(host.hostname) |
| 1158 | host_objects.append(this_host) |
| 1159 | |
| 1160 | try: |
| 1161 | rpc_utils.create_new_job(owner=rrun.owner.login, |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1162 | options=options, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1163 | host_objects=host_objects, |
| 1164 | metahost_objects=metahost_objects, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1165 | atomic_group=atomic_group) |
| 1166 | |
| 1167 | except Exception, ex: |
| 1168 | logging.exception(ex) |
| 1169 | #TODO send email |
| 1170 | |
| 1171 | if rrun.loop_count == 1: |
| 1172 | rrun.delete() |
| 1173 | else: |
| 1174 | if rrun.loop_count != 0: # if not infinite loop |
| 1175 | # calculate new start_date |
| 1176 | difference = datetime.timedelta(seconds=rrun.loop_period) |
| 1177 | rrun.start_date = rrun.start_date + difference |
| 1178 | rrun.loop_count -= 1 |
| 1179 | rrun.save() |
| 1180 | |
| 1181 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1182 | class PidfileRunMonitor(object): |
| 1183 | """ |
| 1184 | Client must call either run() to start a new process or |
| 1185 | attach_to_existing_process(). |
| 1186 | """ |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1187 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1188 | class _PidfileException(Exception): |
| 1189 | """ |
| 1190 | Raised when there's some unexpected behavior with the pid file, but only |
| 1191 | used internally (never allowed to escape this class). |
| 1192 | """ |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1193 | |
| 1194 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1195 | def __init__(self): |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1196 | self.lost_process = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1197 | self._start_time = None |
| 1198 | self.pidfile_id = None |
| 1199 | self._state = drone_manager.PidfileContents() |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1200 | |
| 1201 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1202 | def _add_nice_command(self, command, nice_level): |
| 1203 | if not nice_level: |
| 1204 | return command |
| 1205 | return ['nice', '-n', str(nice_level)] + command |
| 1206 | |
| 1207 | |
| 1208 | def _set_start_time(self): |
| 1209 | self._start_time = time.time() |
| 1210 | |
| 1211 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1212 | def run(self, command, working_directory, num_processes, nice_level=None, |
| 1213 | log_file=None, pidfile_name=None, paired_with_pidfile=None, |
| 1214 | username=None): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1215 | assert command is not None |
| 1216 | if nice_level is not None: |
| 1217 | command = ['nice', '-n', str(nice_level)] + command |
| 1218 | self._set_start_time() |
| 1219 | self.pidfile_id = _drone_manager.execute_command( |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1220 | command, working_directory, pidfile_name=pidfile_name, |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1221 | num_processes=num_processes, log_file=log_file, |
| 1222 | paired_with_pidfile=paired_with_pidfile, username=username) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1223 | |
| 1224 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1225 | def attach_to_existing_process(self, execution_path, |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1226 | pidfile_name=_AUTOSERV_PID_FILE): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1227 | self._set_start_time() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1228 | self.pidfile_id = _drone_manager.get_pidfile_id_from( |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1229 | execution_path, pidfile_name=pidfile_name) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1230 | _drone_manager.register_pidfile(self.pidfile_id) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1231 | |
| 1232 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1233 | def kill(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1234 | if self.has_process(): |
| 1235 | _drone_manager.kill_process(self.get_process()) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1236 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1237 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1238 | def has_process(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1239 | self._get_pidfile_info() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1240 | return self._state.process is not None |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1241 | |
| 1242 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1243 | def get_process(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1244 | self._get_pidfile_info() |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1245 | assert self._state.process is not None |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1246 | return self._state.process |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1247 | |
| 1248 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1249 | def _read_pidfile(self, use_second_read=False): |
| 1250 | assert self.pidfile_id is not None, ( |
| 1251 | 'You must call run() or attach_to_existing_process()') |
| 1252 | contents = _drone_manager.get_pidfile_contents( |
| 1253 | self.pidfile_id, use_second_read=use_second_read) |
| 1254 | if contents.is_invalid(): |
| 1255 | self._state = drone_manager.PidfileContents() |
| 1256 | raise self._PidfileException(contents) |
| 1257 | self._state = contents |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1258 | |
| 1259 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1260 | def _handle_pidfile_error(self, error, message=''): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1261 | message = error + '\nProcess: %s\nPidfile: %s\n%s' % ( |
| 1262 | self._state.process, self.pidfile_id, message) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1263 | email_manager.manager.enqueue_notify_email(error, message) |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1264 | self.on_lost_process(self._state.process) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1265 | |
| 1266 | |
| 1267 | def _get_pidfile_info_helper(self): |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1268 | if self.lost_process: |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1269 | return |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1270 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1271 | self._read_pidfile() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1272 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1273 | if self._state.process is None: |
| 1274 | self._handle_no_process() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1275 | return |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1276 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1277 | if self._state.exit_status is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1278 | # double check whether or not autoserv is running |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1279 | if _drone_manager.is_process_running(self._state.process): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1280 | return |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1281 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1282 | # pid but no running process - maybe process *just* exited |
| 1283 | self._read_pidfile(use_second_read=True) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1284 | if self._state.exit_status is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1285 | # autoserv exited without writing an exit code |
| 1286 | # to the pidfile |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1287 | self._handle_pidfile_error( |
| 1288 | 'autoserv died without writing exit code') |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1289 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1290 | |
| 1291 | def _get_pidfile_info(self): |
| 1292 | """\ |
| 1293 | After completion, self._state will contain: |
| 1294 | pid=None, exit_status=None if autoserv has not yet run |
| 1295 | pid!=None, exit_status=None if autoserv is running |
| 1296 | pid!=None, exit_status!=None if autoserv has completed |
| 1297 | """ |
| 1298 | try: |
| 1299 | self._get_pidfile_info_helper() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1300 | except self._PidfileException, exc: |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1301 | self._handle_pidfile_error('Pidfile error', traceback.format_exc()) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1302 | |
| 1303 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1304 | def _handle_no_process(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1305 | """\ |
| 1306 | Called when no pidfile is found or no pid is in the pidfile. |
| 1307 | """ |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1308 | message = 'No pid found at %s' % self.pidfile_id |
showard | ec6a3b9 | 2009-09-25 20:29:13 +0000 | [diff] [blame] | 1309 | if time.time() - self._start_time > _get_pidfile_timeout_secs(): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1310 | email_manager.manager.enqueue_notify_email( |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1311 | 'Process has failed to write pidfile', message) |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1312 | self.on_lost_process() |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1313 | |
| 1314 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1315 | def on_lost_process(self, process=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1316 | """\ |
| 1317 | Called when autoserv has exited without writing an exit status, |
| 1318 | or we've timed out waiting for autoserv to write a pid to the |
| 1319 | pidfile. In either case, we just return failure and the caller |
| 1320 | should signal some kind of warning. |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1321 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1322 | process is unimportant here, as it shouldn't be used by anyone. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1323 | """ |
| 1324 | self.lost_process = True |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1325 | self._state.process = process |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1326 | self._state.exit_status = 1 |
| 1327 | self._state.num_tests_failed = 0 |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1328 | |
| 1329 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1330 | def exit_code(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1331 | self._get_pidfile_info() |
| 1332 | return self._state.exit_status |
| 1333 | |
| 1334 | |
| 1335 | def num_tests_failed(self): |
showard | 6bba3d1 | 2009-08-20 23:31:41 +0000 | [diff] [blame] | 1336 | """@returns The number of tests that failed or -1 if unknown.""" |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1337 | self._get_pidfile_info() |
showard | 6bba3d1 | 2009-08-20 23:31:41 +0000 | [diff] [blame] | 1338 | if self._state.num_tests_failed is None: |
| 1339 | return -1 |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1340 | return self._state.num_tests_failed |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1341 | |
| 1342 | |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1343 | def try_copy_results_on_drone(self, **kwargs): |
| 1344 | if self.has_process(): |
| 1345 | # copy results logs into the normal place for job results |
| 1346 | _drone_manager.copy_results_on_drone(self.get_process(), **kwargs) |
| 1347 | |
| 1348 | |
| 1349 | def try_copy_to_results_repository(self, source, **kwargs): |
| 1350 | if self.has_process(): |
| 1351 | _drone_manager.copy_to_results_repository(self.get_process(), |
| 1352 | source, **kwargs) |
| 1353 | |
| 1354 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1355 | class Agent(object): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1356 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1357 | An agent for use by the Dispatcher class to perform a task. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1358 | |
| 1359 | The following methods are required on all task objects: |
| 1360 | poll() - Called periodically to let the task check its status and |
| 1361 | update its internal state. If the task succeeded. |
| 1362 | is_done() - Returns True if the task is finished. |
| 1363 | abort() - Called when an abort has been requested. The task must |
| 1364 | set its aborted attribute to True if it actually aborted. |
| 1365 | |
| 1366 | The following attributes are required on all task objects: |
| 1367 | aborted - bool, True if this task was aborted. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1368 | success - bool, True if this task succeeded. |
| 1369 | queue_entry_ids - A sequence of HostQueueEntry ids this task handles. |
| 1370 | host_ids - A sequence of Host ids this task represents. |
| 1371 | |
| 1372 | The following attribute is written to all task objects: |
| 1373 | agent - A reference to the Agent instance that the task has been |
| 1374 | added to. |
| 1375 | """ |
| 1376 | |
| 1377 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1378 | def __init__(self, task): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1379 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1380 | @param task: A task as described in the class docstring. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1381 | @param num_processes: The number of subprocesses the Agent represents. |
| 1382 | This is used by the Dispatcher for managing the load on the |
| 1383 | system. Defaults to 1. |
| 1384 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1385 | self.task = task |
| 1386 | task.agent = self |
| 1387 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1388 | # This is filled in by Dispatcher.add_agent() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1389 | self.dispatcher = None |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1390 | self.num_processes = task.num_processes |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1391 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1392 | self.queue_entry_ids = task.queue_entry_ids |
| 1393 | self.host_ids = task.host_ids |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1394 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1395 | self.started = False |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1396 | self.finished = False |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1397 | |
| 1398 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1399 | def tick(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1400 | self.started = True |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1401 | if not self.finished: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1402 | self.task.poll() |
| 1403 | if self.task.is_done(): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1404 | self.finished = True |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 1405 | |
| 1406 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1407 | def is_done(self): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1408 | return self.finished |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1409 | |
| 1410 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1411 | def abort(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1412 | if self.task: |
| 1413 | self.task.abort() |
| 1414 | if self.task.aborted: |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1415 | # tasks can choose to ignore aborts |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1416 | self.finished = True |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1417 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1418 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1419 | class DelayedCallTask(object): |
| 1420 | """ |
| 1421 | A task object like AgentTask for an Agent to run that waits for the |
| 1422 | specified amount of time to have elapsed before calling the supplied |
| 1423 | callback once and finishing. If the callback returns anything, it is |
| 1424 | assumed to be a new Agent instance and will be added to the dispatcher. |
| 1425 | |
| 1426 | @attribute end_time: The absolute posix time after which this task will |
| 1427 | call its callback when it is polled and be finished. |
| 1428 | |
| 1429 | Also has all attributes required by the Agent class. |
| 1430 | """ |
| 1431 | def __init__(self, delay_seconds, callback, now_func=None): |
| 1432 | """ |
| 1433 | @param delay_seconds: The delay in seconds from now that this task |
| 1434 | will call the supplied callback and be done. |
| 1435 | @param callback: A callable to be called by this task once after at |
| 1436 | least delay_seconds time has elapsed. It must return None |
| 1437 | or a new Agent instance. |
| 1438 | @param now_func: A time.time like function. Default: time.time. |
| 1439 | Used for testing. |
| 1440 | """ |
| 1441 | assert delay_seconds > 0 |
| 1442 | assert callable(callback) |
| 1443 | if not now_func: |
| 1444 | now_func = time.time |
| 1445 | self._now_func = now_func |
| 1446 | self._callback = callback |
| 1447 | |
| 1448 | self.end_time = self._now_func() + delay_seconds |
| 1449 | |
| 1450 | # These attributes are required by Agent. |
| 1451 | self.aborted = False |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1452 | self.host_ids = () |
| 1453 | self.success = False |
| 1454 | self.queue_entry_ids = () |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1455 | self.num_processes = 0 |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1456 | # This is filled in by Agent.add_task(). |
| 1457 | self.agent = None |
| 1458 | |
| 1459 | |
| 1460 | def poll(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1461 | if not self.is_done() and self._now_func() >= self.end_time: |
| 1462 | self._callback() |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1463 | self.success = True |
| 1464 | |
| 1465 | |
| 1466 | def is_done(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1467 | return self.success or self.aborted |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1468 | |
| 1469 | |
| 1470 | def abort(self): |
| 1471 | self.aborted = True |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1472 | |
| 1473 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1474 | class AgentTask(object): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1475 | def __init__(self, cmd=None, working_directory=None, |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1476 | recover_run_monitor=None, username=None, num_processes=1): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1477 | """ |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1478 | @param username: login of user responsible for this task. may be None. |
| 1479 | @param num_processes: number of autoserv processes launched by this |
| 1480 | AgentTask. this includes forking the autoserv process may do. |
| 1481 | it may be only approximate. |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1482 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1483 | self.done = False |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1484 | self.cmd = cmd |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1485 | self._working_directory = working_directory |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1486 | self.username = username |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1487 | self.num_processes = num_processes |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1488 | self.agent = None |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1489 | self.monitor = recover_run_monitor |
| 1490 | self.started = bool(recover_run_monitor) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1491 | self.success = None |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1492 | self.aborted = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1493 | self.queue_entry_ids = [] |
| 1494 | self.host_ids = [] |
| 1495 | self.log_file = None |
| 1496 | |
| 1497 | |
| 1498 | def _set_ids(self, host=None, queue_entries=None): |
| 1499 | if queue_entries and queue_entries != [None]: |
| 1500 | self.host_ids = [entry.host.id for entry in queue_entries] |
| 1501 | self.queue_entry_ids = [entry.id for entry in queue_entries] |
| 1502 | else: |
| 1503 | assert host |
| 1504 | self.host_ids = [host.id] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1505 | |
| 1506 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1507 | def poll(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1508 | if not self.started: |
| 1509 | self.start() |
| 1510 | self.tick() |
| 1511 | |
| 1512 | |
| 1513 | def tick(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1514 | if self.monitor: |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1515 | exit_code = self.monitor.exit_code() |
| 1516 | if exit_code is None: |
| 1517 | return |
| 1518 | success = (exit_code == 0) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1519 | else: |
| 1520 | success = False |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1521 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1522 | self.finished(success) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1523 | |
| 1524 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1525 | def is_done(self): |
| 1526 | return self.done |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1527 | |
| 1528 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1529 | def finished(self, success): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1530 | if self.done: |
| 1531 | return |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1532 | self.done = True |
| 1533 | self.success = success |
| 1534 | self.epilog() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1535 | |
| 1536 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1537 | def prolog(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1538 | assert not self.monitor |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1539 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1540 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1541 | def cleanup(self): |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1542 | if self.monitor and self.log_file: |
| 1543 | self.monitor.try_copy_to_results_repository(self.log_file) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1544 | |
| 1545 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1546 | def epilog(self): |
| 1547 | self.cleanup() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1548 | |
| 1549 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1550 | def start(self): |
| 1551 | assert self.agent |
| 1552 | |
| 1553 | if not self.started: |
| 1554 | self.prolog() |
| 1555 | self.run() |
| 1556 | |
| 1557 | self.started = True |
| 1558 | |
| 1559 | |
| 1560 | def abort(self): |
| 1561 | if self.monitor: |
| 1562 | self.monitor.kill() |
| 1563 | self.done = True |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1564 | self.aborted = True |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1565 | self.cleanup() |
| 1566 | |
| 1567 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1568 | def _get_consistent_execution_path(self, execution_entries): |
| 1569 | first_execution_path = execution_entries[0].execution_path() |
| 1570 | for execution_entry in execution_entries[1:]: |
| 1571 | assert execution_entry.execution_path() == first_execution_path, ( |
| 1572 | '%s (%s) != %s (%s)' % (execution_entry.execution_path(), |
| 1573 | execution_entry, |
| 1574 | first_execution_path, |
| 1575 | execution_entries[0])) |
| 1576 | return first_execution_path |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1577 | |
| 1578 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1579 | def _copy_results(self, execution_entries, use_monitor=None): |
| 1580 | """ |
| 1581 | @param execution_entries: list of objects with execution_path() method |
| 1582 | """ |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1583 | if use_monitor is not None and not use_monitor.has_process(): |
| 1584 | return |
| 1585 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1586 | assert len(execution_entries) > 0 |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1587 | if use_monitor is None: |
| 1588 | assert self.monitor |
| 1589 | use_monitor = self.monitor |
| 1590 | assert use_monitor.has_process() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1591 | execution_path = self._get_consistent_execution_path(execution_entries) |
| 1592 | results_path = execution_path + '/' |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1593 | use_monitor.try_copy_to_results_repository(results_path) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1594 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1595 | |
| 1596 | def _parse_results(self, queue_entries): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1597 | for queue_entry in queue_entries: |
| 1598 | queue_entry.set_status(models.HostQueueEntry.Status.PARSING) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1599 | |
| 1600 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1601 | def _copy_and_parse_results(self, queue_entries, use_monitor=None): |
| 1602 | self._copy_results(queue_entries, use_monitor) |
| 1603 | self._parse_results(queue_entries) |
| 1604 | |
| 1605 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1606 | def run(self, pidfile_name=_AUTOSERV_PID_FILE, paired_with_pidfile=None): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1607 | assert not self.monitor |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1608 | if self.cmd: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1609 | self.monitor = PidfileRunMonitor() |
| 1610 | self.monitor.run(self.cmd, self._working_directory, |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1611 | num_processes=self.num_processes, |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1612 | nice_level=AUTOSERV_NICE_LEVEL, |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1613 | log_file=self.log_file, |
| 1614 | pidfile_name=pidfile_name, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1615 | paired_with_pidfile=paired_with_pidfile, |
| 1616 | username=self.username) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1617 | |
| 1618 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1619 | class TaskWithJobKeyvals(object): |
| 1620 | """AgentTask mixin providing functionality to help with job keyval files.""" |
| 1621 | _KEYVAL_FILE = 'keyval' |
| 1622 | def _format_keyval(self, key, value): |
| 1623 | return '%s=%s' % (key, value) |
| 1624 | |
| 1625 | |
| 1626 | def _keyval_path(self): |
| 1627 | """Subclasses must override this""" |
| 1628 | raise NotImplemented |
| 1629 | |
| 1630 | |
| 1631 | def _write_keyval_after_job(self, field, value): |
| 1632 | assert self.monitor |
| 1633 | if not self.monitor.has_process(): |
| 1634 | return |
| 1635 | _drone_manager.write_lines_to_file( |
| 1636 | self._keyval_path(), [self._format_keyval(field, value)], |
| 1637 | paired_with_process=self.monitor.get_process()) |
| 1638 | |
| 1639 | |
| 1640 | def _job_queued_keyval(self, job): |
| 1641 | return 'job_queued', int(time.mktime(job.created_on.timetuple())) |
| 1642 | |
| 1643 | |
| 1644 | def _write_job_finished(self): |
| 1645 | self._write_keyval_after_job("job_finished", int(time.time())) |
| 1646 | |
| 1647 | |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1648 | def _write_keyvals_before_job_helper(self, keyval_dict, keyval_path): |
| 1649 | keyval_contents = '\n'.join(self._format_keyval(key, value) |
| 1650 | for key, value in keyval_dict.iteritems()) |
| 1651 | # always end with a newline to allow additional keyvals to be written |
| 1652 | keyval_contents += '\n' |
| 1653 | _drone_manager.attach_file_to_execution(self._working_directory, |
| 1654 | keyval_contents, |
| 1655 | file_path=keyval_path) |
| 1656 | |
| 1657 | |
| 1658 | def _write_keyvals_before_job(self, keyval_dict): |
| 1659 | self._write_keyvals_before_job_helper(keyval_dict, self._keyval_path()) |
| 1660 | |
| 1661 | |
| 1662 | def _write_host_keyvals(self, host): |
| 1663 | keyval_path = os.path.join(self._working_directory, 'host_keyvals', |
| 1664 | host.hostname) |
| 1665 | platform, all_labels = host.platform_and_labels() |
| 1666 | keyval_dict = dict(platform=platform, labels=','.join(all_labels)) |
| 1667 | self._write_keyvals_before_job_helper(keyval_dict, keyval_path) |
| 1668 | |
| 1669 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1670 | class SpecialAgentTask(AgentTask, TaskWithJobKeyvals): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1671 | """ |
| 1672 | Subclass for AgentTasks that correspond to a SpecialTask entry in the DB. |
| 1673 | """ |
| 1674 | |
| 1675 | TASK_TYPE = None |
| 1676 | host = None |
| 1677 | queue_entry = None |
| 1678 | |
| 1679 | def __init__(self, task, extra_command_args, **kwargs): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1680 | assert (self.TASK_TYPE is not None, |
| 1681 | 'self.TASK_TYPE must be overridden') |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1682 | |
| 1683 | self.host = Host(id=task.host.id) |
| 1684 | self.queue_entry = None |
| 1685 | if task.queue_entry: |
| 1686 | self.queue_entry = HostQueueEntry(id=task.queue_entry.id) |
| 1687 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1688 | self.task = task |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1689 | kwargs['working_directory'] = task.execution_path() |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1690 | if task.requested_by: |
| 1691 | kwargs['username'] = task.requested_by.login |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1692 | self._extra_command_args = extra_command_args |
| 1693 | super(SpecialAgentTask, self).__init__(**kwargs) |
| 1694 | |
| 1695 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1696 | def _keyval_path(self): |
| 1697 | return os.path.join(self._working_directory, self._KEYVAL_FILE) |
| 1698 | |
| 1699 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1700 | def prolog(self): |
| 1701 | super(SpecialAgentTask, self).prolog() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1702 | self.cmd = _autoserv_command_line(self.host.hostname, |
| 1703 | self._extra_command_args, |
| 1704 | queue_entry=self.queue_entry) |
| 1705 | self._working_directory = self.task.execution_path() |
| 1706 | self.task.activate() |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1707 | self._write_host_keyvals(self.host) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1708 | |
| 1709 | |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1710 | def _fail_queue_entry(self): |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1711 | assert self.queue_entry |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1712 | |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1713 | if self.queue_entry.meta_host: |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1714 | return # don't fail metahost entries, they'll be reassigned |
| 1715 | |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1716 | self.queue_entry.update_from_database() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1717 | if self.queue_entry.status != models.HostQueueEntry.Status.QUEUED: |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1718 | return # entry has been aborted |
| 1719 | |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1720 | self.queue_entry.set_execution_subdir() |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1721 | queued_key, queued_time = self._job_queued_keyval( |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1722 | self.queue_entry.job) |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1723 | self._write_keyval_after_job(queued_key, queued_time) |
| 1724 | self._write_job_finished() |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1725 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1726 | # copy results logs into the normal place for job results |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1727 | self.monitor.try_copy_results_on_drone( |
| 1728 | source_path=self._working_directory + '/', |
| 1729 | destination_path=self.queue_entry.execution_path() + '/') |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1730 | |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1731 | self._copy_results([self.queue_entry]) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1732 | self.queue_entry.handle_host_failure() |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1733 | if self.queue_entry.job.parse_failed_repair: |
| 1734 | self._parse_results([self.queue_entry]) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1735 | |
| 1736 | pidfile_id = _drone_manager.get_pidfile_id_from( |
| 1737 | self.queue_entry.execution_path(), |
| 1738 | pidfile_name=_AUTOSERV_PID_FILE) |
| 1739 | _drone_manager.register_pidfile(pidfile_id) |
| 1740 | |
| 1741 | |
| 1742 | def cleanup(self): |
| 1743 | super(SpecialAgentTask, self).cleanup() |
showard | e60e44e | 2009-11-13 20:45:38 +0000 | [diff] [blame] | 1744 | |
| 1745 | # We will consider an aborted task to be "Failed" |
| 1746 | self.task.finish(bool(self.success)) |
| 1747 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 1748 | if self.monitor: |
| 1749 | if self.monitor.has_process(): |
| 1750 | self._copy_results([self.task]) |
| 1751 | if self.monitor.pidfile_id is not None: |
| 1752 | _drone_manager.unregister_pidfile(self.monitor.pidfile_id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1753 | |
| 1754 | |
| 1755 | class RepairTask(SpecialAgentTask): |
| 1756 | TASK_TYPE = models.SpecialTask.Task.REPAIR |
| 1757 | |
| 1758 | |
| 1759 | def __init__(self, task, recover_run_monitor=None): |
| 1760 | """\ |
| 1761 | queue_entry: queue entry to mark failed if this repair fails. |
| 1762 | """ |
| 1763 | protection = host_protections.Protection.get_string( |
| 1764 | task.host.protection) |
| 1765 | # normalize the protection name |
| 1766 | protection = host_protections.Protection.get_attr_name(protection) |
| 1767 | |
| 1768 | super(RepairTask, self).__init__( |
| 1769 | task, ['-R', '--host-protection', protection], |
| 1770 | recover_run_monitor=recover_run_monitor) |
| 1771 | |
| 1772 | # *don't* include the queue entry in IDs -- if the queue entry is |
| 1773 | # aborted, we want to leave the repair task running |
| 1774 | self._set_ids(host=self.host) |
| 1775 | |
| 1776 | |
| 1777 | def prolog(self): |
| 1778 | super(RepairTask, self).prolog() |
| 1779 | logging.info("repair_task starting") |
| 1780 | self.host.set_status(models.Host.Status.REPAIRING) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1781 | |
| 1782 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1783 | def epilog(self): |
| 1784 | super(RepairTask, self).epilog() |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 1785 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1786 | if self.success: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1787 | self.host.set_status(models.Host.Status.READY) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1788 | else: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1789 | self.host.set_status(models.Host.Status.REPAIR_FAILED) |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1790 | if self.queue_entry: |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1791 | self._fail_queue_entry() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1792 | |
| 1793 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1794 | class PreJobTask(SpecialAgentTask): |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1795 | def _copy_to_results_repository(self): |
| 1796 | if not self.queue_entry or self.queue_entry.meta_host: |
| 1797 | return |
| 1798 | |
| 1799 | self.queue_entry.set_execution_subdir() |
| 1800 | log_name = os.path.basename(self.task.execution_path()) |
| 1801 | source = os.path.join(self.task.execution_path(), 'debug', |
| 1802 | 'autoserv.DEBUG') |
| 1803 | destination = os.path.join( |
| 1804 | self.queue_entry.execution_path(), log_name) |
| 1805 | |
| 1806 | self.monitor.try_copy_to_results_repository( |
| 1807 | source, destination_path=destination) |
| 1808 | |
| 1809 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1810 | def epilog(self): |
| 1811 | super(PreJobTask, self).epilog() |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1812 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1813 | if self.success: |
| 1814 | return |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1815 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1816 | self._copy_to_results_repository() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1817 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1818 | if self.host.protection == host_protections.Protection.DO_NOT_VERIFY: |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 1819 | # effectively ignore failure for these hosts |
| 1820 | self.success = True |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1821 | return |
| 1822 | |
| 1823 | if self.queue_entry: |
| 1824 | self.queue_entry.requeue() |
| 1825 | |
| 1826 | if models.SpecialTask.objects.filter( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1827 | task=models.SpecialTask.Task.REPAIR, |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1828 | queue_entry__id=self.queue_entry.id): |
| 1829 | self.host.set_status(models.Host.Status.REPAIR_FAILED) |
| 1830 | self._fail_queue_entry() |
| 1831 | return |
| 1832 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1833 | queue_entry = models.HostQueueEntry.objects.get( |
| 1834 | id=self.queue_entry.id) |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1835 | else: |
| 1836 | queue_entry = None |
| 1837 | |
| 1838 | models.SpecialTask.objects.create( |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1839 | host=models.Host.objects.get(id=self.host.id), |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1840 | task=models.SpecialTask.Task.REPAIR, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1841 | queue_entry=queue_entry, |
| 1842 | requested_by=self.task.requested_by) |
showard | 58721a8 | 2009-08-20 23:32:40 +0000 | [diff] [blame] | 1843 | |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1844 | |
| 1845 | class VerifyTask(PreJobTask): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1846 | TASK_TYPE = models.SpecialTask.Task.VERIFY |
| 1847 | |
| 1848 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1849 | def __init__(self, task, recover_run_monitor=None): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1850 | super(VerifyTask, self).__init__( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1851 | task, ['-v'], recover_run_monitor=recover_run_monitor) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1852 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1853 | self._set_ids(host=self.host, queue_entries=[self.queue_entry]) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1854 | |
| 1855 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1856 | def prolog(self): |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1857 | super(VerifyTask, self).prolog() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1858 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1859 | logging.info("starting verify on %s", self.host.hostname) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1860 | if self.queue_entry: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1861 | self.queue_entry.set_status(models.HostQueueEntry.Status.VERIFYING) |
| 1862 | self.host.set_status(models.Host.Status.VERIFYING) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1863 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1864 | # Delete any other queued verifies for this host. One verify will do |
| 1865 | # and there's no need to keep records of other requests. |
| 1866 | queued_verifies = models.SpecialTask.objects.filter( |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1867 | host__id=self.host.id, |
| 1868 | task=models.SpecialTask.Task.VERIFY, |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1869 | is_active=False, is_complete=False) |
| 1870 | queued_verifies = queued_verifies.exclude(id=self.task.id) |
| 1871 | queued_verifies.delete() |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1872 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1873 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1874 | def epilog(self): |
| 1875 | super(VerifyTask, self).epilog() |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1876 | if self.success: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1877 | if self.queue_entry: |
| 1878 | self.queue_entry.on_pending() |
| 1879 | else: |
| 1880 | self.host.set_status(models.Host.Status.READY) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1881 | |
| 1882 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1883 | class QueueTask(AgentTask, TaskWithJobKeyvals): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1884 | def __init__(self, job, queue_entries, cmd=None, recover_run_monitor=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1885 | self.job = job |
| 1886 | self.queue_entries = queue_entries |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1887 | self.group_name = queue_entries[0].get_group_name() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1888 | super(QueueTask, self).__init__( |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1889 | cmd=cmd, working_directory=self._execution_path(), |
| 1890 | recover_run_monitor=recover_run_monitor, |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1891 | username=job.owner, num_processes=len(queue_entries)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1892 | self._set_ids(queue_entries=queue_entries) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1893 | |
| 1894 | |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 1895 | def _keyval_path(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1896 | return os.path.join(self._execution_path(), self._KEYVAL_FILE) |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 1897 | |
| 1898 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1899 | def _execution_path(self): |
| 1900 | return self.queue_entries[0].execution_path() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1901 | |
| 1902 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1903 | def prolog(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1904 | for entry in self.queue_entries: |
| 1905 | if entry.status not in (models.HostQueueEntry.Status.STARTING, |
| 1906 | models.HostQueueEntry.Status.RUNNING): |
| 1907 | raise SchedulerError('Queue task attempting to start ' |
| 1908 | 'entry with invalid status %s: %s' |
| 1909 | % (entry.status, entry)) |
| 1910 | if entry.host.status not in (models.Host.Status.PENDING, |
| 1911 | models.Host.Status.RUNNING): |
| 1912 | raise SchedulerError('Queue task attempting to start on queue ' |
| 1913 | 'entry with invalid host status %s: %s' |
| 1914 | % (entry.host.status, entry)) |
| 1915 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1916 | queued_key, queued_time = self._job_queued_keyval(self.job) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1917 | keyval_dict = {queued_key: queued_time} |
| 1918 | if self.group_name: |
| 1919 | keyval_dict['host_group_name'] = self.group_name |
| 1920 | self._write_keyvals_before_job(keyval_dict) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1921 | for queue_entry in self.queue_entries: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1922 | self._write_host_keyvals(queue_entry.host) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1923 | queue_entry.set_status(models.HostQueueEntry.Status.RUNNING) |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 1924 | queue_entry.update_field('started_on', datetime.datetime.now()) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1925 | queue_entry.host.set_status(models.Host.Status.RUNNING) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1926 | queue_entry.host.update_field('dirty', 1) |
showard | c6a5687 | 2009-07-28 20:11:58 +0000 | [diff] [blame] | 1927 | if self.job.synch_count == 1 and len(self.queue_entries) == 1: |
| 1928 | # TODO(gps): Remove this if nothing needs it anymore. |
| 1929 | # A potential user is: tko/parser |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1930 | self.job.write_to_machines_file(self.queue_entries[0]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1931 | |
| 1932 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1933 | def _write_lost_process_error_file(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1934 | error_file_path = os.path.join(self._execution_path(), 'job_failure') |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1935 | _drone_manager.write_lines_to_file(error_file_path, |
| 1936 | [_LOST_PROCESS_ERROR]) |
| 1937 | |
| 1938 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1939 | def _finish_task(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1940 | if not self.monitor: |
| 1941 | return |
| 1942 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1943 | self._write_job_finished() |
| 1944 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1945 | if self.monitor.lost_process: |
| 1946 | self._write_lost_process_error_file() |
showard | 4ac4754 | 2009-08-31 18:32:19 +0000 | [diff] [blame] | 1947 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1948 | for queue_entry in self.queue_entries: |
| 1949 | queue_entry.set_status(models.HostQueueEntry.Status.GATHERING) |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1950 | |
| 1951 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1952 | def _write_status_comment(self, comment): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1953 | _drone_manager.write_lines_to_file( |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1954 | os.path.join(self._execution_path(), 'status.log'), |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1955 | ['INFO\t----\t----\t' + comment], |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1956 | paired_with_process=self.monitor.get_process()) |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1957 | |
| 1958 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1959 | def _log_abort(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1960 | if not self.monitor or not self.monitor.has_process(): |
| 1961 | return |
| 1962 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1963 | # build up sets of all the aborted_by and aborted_on values |
| 1964 | aborted_by, aborted_on = set(), set() |
| 1965 | for queue_entry in self.queue_entries: |
| 1966 | if queue_entry.aborted_by: |
| 1967 | aborted_by.add(queue_entry.aborted_by) |
| 1968 | t = int(time.mktime(queue_entry.aborted_on.timetuple())) |
| 1969 | aborted_on.add(t) |
| 1970 | |
| 1971 | # extract some actual, unique aborted by value and write it out |
| 1972 | assert len(aborted_by) <= 1 |
| 1973 | if len(aborted_by) == 1: |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1974 | aborted_by_value = aborted_by.pop() |
| 1975 | aborted_on_value = max(aborted_on) |
| 1976 | else: |
| 1977 | aborted_by_value = 'autotest_system' |
| 1978 | aborted_on_value = int(time.time()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1979 | |
showard | a038235 | 2009-02-11 23:36:43 +0000 | [diff] [blame] | 1980 | self._write_keyval_after_job("aborted_by", aborted_by_value) |
| 1981 | self._write_keyval_after_job("aborted_on", aborted_on_value) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1982 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1983 | aborted_on_string = str(datetime.datetime.fromtimestamp( |
| 1984 | aborted_on_value)) |
| 1985 | self._write_status_comment('Job aborted by %s on %s' % |
| 1986 | (aborted_by_value, aborted_on_string)) |
jadmanski | c2ac77f | 2008-05-16 21:44:04 +0000 | [diff] [blame] | 1987 | |
| 1988 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1989 | def abort(self): |
| 1990 | super(QueueTask, self).abort() |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1991 | self._log_abort() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1992 | self._finish_task() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1993 | |
| 1994 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1995 | def epilog(self): |
| 1996 | super(QueueTask, self).epilog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1997 | self._finish_task() |
| 1998 | logging.info("queue_task finished with success=%s", self.success) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1999 | |
| 2000 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2001 | class PostJobTask(AgentTask): |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 2002 | def __init__(self, queue_entries, pidfile_name, logfile_name, num_processes, |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2003 | recover_run_monitor=None): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2004 | self._queue_entries = queue_entries |
| 2005 | self._pidfile_name = pidfile_name |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2006 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2007 | self._execution_path = self._get_consistent_execution_path( |
| 2008 | queue_entries) |
| 2009 | self._results_dir = _drone_manager.absolute_path(self._execution_path) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2010 | self._autoserv_monitor = PidfileRunMonitor() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2011 | self._autoserv_monitor.attach_to_existing_process(self._execution_path) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2012 | self._paired_with_pidfile = self._autoserv_monitor.pidfile_id |
| 2013 | |
| 2014 | if _testing_mode: |
| 2015 | command = 'true' |
| 2016 | else: |
| 2017 | command = self._generate_command(self._results_dir) |
| 2018 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2019 | super(PostJobTask, self).__init__( |
| 2020 | cmd=command, working_directory=self._execution_path, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2021 | recover_run_monitor=recover_run_monitor, |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 2022 | username=queue_entries[0].job.owner, |
| 2023 | num_processes=num_processes) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2024 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2025 | self.log_file = os.path.join(self._execution_path, logfile_name) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2026 | self._final_status = self._determine_final_status() |
| 2027 | |
| 2028 | |
| 2029 | def _generate_command(self, results_dir): |
| 2030 | raise NotImplementedError('Subclasses must override this') |
| 2031 | |
| 2032 | |
| 2033 | def _job_was_aborted(self): |
| 2034 | was_aborted = None |
| 2035 | for queue_entry in self._queue_entries: |
| 2036 | queue_entry.update_from_database() |
| 2037 | if was_aborted is None: # first queue entry |
| 2038 | was_aborted = bool(queue_entry.aborted) |
| 2039 | elif was_aborted != bool(queue_entry.aborted): # subsequent entries |
| 2040 | email_manager.manager.enqueue_notify_email( |
| 2041 | 'Inconsistent abort state', |
| 2042 | 'Queue entries have inconsistent abort state: ' + |
| 2043 | ', '.join('%s (%s)' % (queue_entry, queue_entry.aborted))) |
| 2044 | # don't crash here, just assume true |
| 2045 | return True |
| 2046 | return was_aborted |
| 2047 | |
| 2048 | |
| 2049 | def _determine_final_status(self): |
| 2050 | if self._job_was_aborted(): |
| 2051 | return models.HostQueueEntry.Status.ABORTED |
| 2052 | |
| 2053 | # we'll use a PidfileRunMonitor to read the autoserv exit status |
| 2054 | if self._autoserv_monitor.exit_code() == 0: |
| 2055 | return models.HostQueueEntry.Status.COMPLETED |
| 2056 | return models.HostQueueEntry.Status.FAILED |
| 2057 | |
| 2058 | |
| 2059 | def run(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2060 | # Make sure we actually have results to work with. |
| 2061 | # This should never happen in normal operation. |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 2062 | if not self._autoserv_monitor.has_process(): |
| 2063 | email_manager.manager.enqueue_notify_email( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2064 | 'No results in post-job task', |
| 2065 | 'No results in post-job task at %s' % |
| 2066 | self._autoserv_monitor.pidfile_id) |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 2067 | self.finished(False) |
| 2068 | return |
| 2069 | |
| 2070 | super(PostJobTask, self).run( |
| 2071 | pidfile_name=self._pidfile_name, |
| 2072 | paired_with_pidfile=self._paired_with_pidfile) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2073 | |
| 2074 | |
| 2075 | def _set_all_statuses(self, status): |
| 2076 | for queue_entry in self._queue_entries: |
| 2077 | queue_entry.set_status(status) |
| 2078 | |
| 2079 | |
| 2080 | def abort(self): |
| 2081 | # override AgentTask.abort() to avoid killing the process and ending |
| 2082 | # the task. post-job tasks continue when the job is aborted. |
| 2083 | pass |
| 2084 | |
| 2085 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2086 | class GatherLogsTask(PostJobTask): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2087 | """ |
| 2088 | Task responsible for |
| 2089 | * gathering uncollected logs (if Autoserv crashed hard or was killed) |
| 2090 | * copying logs to the results repository |
| 2091 | * spawning CleanupTasks for hosts, if necessary |
| 2092 | * spawning a FinalReparseTask for the job |
| 2093 | """ |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2094 | def __init__(self, job, queue_entries, recover_run_monitor=None): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2095 | self._job = job |
| 2096 | super(GatherLogsTask, self).__init__( |
| 2097 | queue_entries, pidfile_name=_CRASHINFO_PID_FILE, |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2098 | logfile_name='.collect_crashinfo.log', |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 2099 | num_processes=len(queue_entries), |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2100 | recover_run_monitor=recover_run_monitor) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2101 | self._set_ids(queue_entries=queue_entries) |
| 2102 | |
| 2103 | |
| 2104 | def _generate_command(self, results_dir): |
| 2105 | host_list = ','.join(queue_entry.host.hostname |
| 2106 | for queue_entry in self._queue_entries) |
| 2107 | return [_autoserv_path , '-p', '--collect-crashinfo', '-m', host_list, |
| 2108 | '-r', results_dir] |
| 2109 | |
| 2110 | |
| 2111 | def prolog(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2112 | for queue_entry in self._queue_entries: |
| 2113 | if queue_entry.status != models.HostQueueEntry.Status.GATHERING: |
| 2114 | raise SchedulerError('Gather task attempting to start on ' |
| 2115 | 'non-gathering entry: %s' % queue_entry) |
| 2116 | if queue_entry.host.status != models.Host.Status.RUNNING: |
| 2117 | raise SchedulerError('Gather task attempting to start on queue ' |
showard | 0c5c18d | 2009-09-24 22:20:45 +0000 | [diff] [blame] | 2118 | 'entry with non-running host status %s: %s' |
| 2119 | % (queue_entry.host.status, queue_entry)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2120 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2121 | super(GatherLogsTask, self).prolog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2122 | |
| 2123 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2124 | def epilog(self): |
| 2125 | super(GatherLogsTask, self).epilog() |
showard | b562645 | 2009-06-30 01:57:28 +0000 | [diff] [blame] | 2126 | |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 2127 | self._copy_and_parse_results(self._queue_entries, |
| 2128 | use_monitor=self._autoserv_monitor) |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2129 | self._reboot_hosts() |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 2130 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2131 | |
| 2132 | def _reboot_hosts(self): |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 2133 | if self._autoserv_monitor.has_process(): |
| 2134 | final_success = (self._final_status == |
| 2135 | models.HostQueueEntry.Status.COMPLETED) |
| 2136 | num_tests_failed = self._autoserv_monitor.num_tests_failed() |
| 2137 | else: |
| 2138 | final_success = False |
| 2139 | num_tests_failed = 0 |
| 2140 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2141 | reboot_after = self._job.reboot_after |
| 2142 | do_reboot = ( |
| 2143 | # always reboot after aborted jobs |
| 2144 | self._final_status == models.HostQueueEntry.Status.ABORTED |
| 2145 | or reboot_after == models.RebootAfter.ALWAYS |
| 2146 | or (reboot_after == models.RebootAfter.IF_ALL_TESTS_PASSED |
| 2147 | and final_success and num_tests_failed == 0)) |
| 2148 | |
| 2149 | for queue_entry in self._queue_entries: |
| 2150 | if do_reboot: |
| 2151 | # don't pass the queue entry to the CleanupTask. if the cleanup |
| 2152 | # fails, the job doesn't care -- it's over. |
| 2153 | models.SpecialTask.objects.create( |
| 2154 | host=models.Host.objects.get(id=queue_entry.host.id), |
| 2155 | task=models.SpecialTask.Task.CLEANUP, |
| 2156 | requested_by=self._job.owner_model()) |
| 2157 | else: |
| 2158 | queue_entry.host.set_status(models.Host.Status.READY) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2159 | |
| 2160 | |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 2161 | def run(self): |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 2162 | autoserv_exit_code = self._autoserv_monitor.exit_code() |
| 2163 | # only run if Autoserv exited due to some signal. if we have no exit |
| 2164 | # code, assume something bad (and signal-like) happened. |
| 2165 | if autoserv_exit_code is None or os.WIFSIGNALED(autoserv_exit_code): |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 2166 | super(GatherLogsTask, self).run() |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 2167 | else: |
| 2168 | self.finished(True) |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 2169 | |
| 2170 | |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 2171 | class CleanupTask(PreJobTask): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2172 | TASK_TYPE = models.SpecialTask.Task.CLEANUP |
| 2173 | |
| 2174 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2175 | def __init__(self, task, recover_run_monitor=None): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2176 | super(CleanupTask, self).__init__( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2177 | task, ['--cleanup'], recover_run_monitor=recover_run_monitor) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2178 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2179 | self._set_ids(host=self.host, queue_entries=[self.queue_entry]) |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 2180 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2181 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2182 | def prolog(self): |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 2183 | super(CleanupTask, self).prolog() |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2184 | logging.info("starting cleanup task for host: %s", self.host.hostname) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2185 | self.host.set_status(models.Host.Status.CLEANING) |
| 2186 | if self.queue_entry: |
| 2187 | self.queue_entry.set_status(models.HostQueueEntry.Status.VERIFYING) |
| 2188 | |
| 2189 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 2190 | def _finish_epilog(self): |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 2191 | if not self.queue_entry or not self.success: |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 2192 | return |
| 2193 | |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 2194 | do_not_verify_protection = host_protections.Protection.DO_NOT_VERIFY |
| 2195 | should_run_verify = ( |
| 2196 | self.queue_entry.job.run_verify |
| 2197 | and self.host.protection != do_not_verify_protection) |
| 2198 | if should_run_verify: |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2199 | entry = models.HostQueueEntry.objects.get(id=self.queue_entry.id) |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 2200 | models.SpecialTask.objects.create( |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2201 | host=models.Host.objects.get(id=self.host.id), |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 2202 | queue_entry=entry, |
| 2203 | task=models.SpecialTask.Task.VERIFY) |
| 2204 | else: |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 2205 | self.queue_entry.on_pending() |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2206 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2207 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2208 | def epilog(self): |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 2209 | super(CleanupTask, self).epilog() |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2210 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2211 | if self.success: |
| 2212 | self.host.update_field('dirty', 0) |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 2213 | self.host.set_status(models.Host.Status.READY) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2214 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 2215 | self._finish_epilog() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2216 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2217 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2218 | class FinalReparseTask(PostJobTask): |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2219 | _num_running_parses = 0 |
| 2220 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2221 | def __init__(self, queue_entries, recover_run_monitor=None): |
| 2222 | super(FinalReparseTask, self).__init__( |
| 2223 | queue_entries, pidfile_name=_PARSER_PID_FILE, |
| 2224 | logfile_name='.parse.log', |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 2225 | num_processes=0, # don't include parser processes in accounting |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2226 | recover_run_monitor=recover_run_monitor) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2227 | # don't use _set_ids, since we don't want to set the host_ids |
| 2228 | self.queue_entry_ids = [entry.id for entry in queue_entries] |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2229 | self._parse_started = self.started |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2230 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2231 | |
| 2232 | @classmethod |
| 2233 | def _increment_running_parses(cls): |
| 2234 | cls._num_running_parses += 1 |
| 2235 | |
| 2236 | |
| 2237 | @classmethod |
| 2238 | def _decrement_running_parses(cls): |
| 2239 | cls._num_running_parses -= 1 |
| 2240 | |
| 2241 | |
| 2242 | @classmethod |
| 2243 | def _can_run_new_parse(cls): |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 2244 | return (cls._num_running_parses < |
| 2245 | scheduler_config.config.max_parse_processes) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2246 | |
| 2247 | |
| 2248 | def prolog(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2249 | for queue_entry in self._queue_entries: |
| 2250 | if queue_entry.status != models.HostQueueEntry.Status.PARSING: |
| 2251 | raise SchedulerError('Parse task attempting to start on ' |
| 2252 | 'non-parsing entry: %s' % queue_entry) |
| 2253 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2254 | super(FinalReparseTask, self).prolog() |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2255 | |
| 2256 | |
| 2257 | def epilog(self): |
| 2258 | super(FinalReparseTask, self).epilog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2259 | self._set_all_statuses(self._final_status) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2260 | |
| 2261 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2262 | def _generate_command(self, results_dir): |
mbligh | 9e93640 | 2009-05-13 20:42:17 +0000 | [diff] [blame] | 2263 | return [_parser_path, '--write-pidfile', '-l', '2', '-r', '-o', '-P', |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2264 | results_dir] |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2265 | |
| 2266 | |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 2267 | def tick(self): |
| 2268 | # override tick to keep trying to start until the parse count goes down |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2269 | # and we can, at which point we revert to default behavior |
| 2270 | if self._parse_started: |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 2271 | super(FinalReparseTask, self).tick() |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2272 | else: |
| 2273 | self._try_starting_parse() |
| 2274 | |
| 2275 | |
| 2276 | def run(self): |
| 2277 | # override run() to not actually run unless we can |
| 2278 | self._try_starting_parse() |
| 2279 | |
| 2280 | |
| 2281 | def _try_starting_parse(self): |
| 2282 | if not self._can_run_new_parse(): |
| 2283 | return |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2284 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2285 | # actually run the parse command |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2286 | super(FinalReparseTask, self).run() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2287 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2288 | self._increment_running_parses() |
| 2289 | self._parse_started = True |
| 2290 | |
| 2291 | |
| 2292 | def finished(self, success): |
| 2293 | super(FinalReparseTask, self).finished(success) |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 2294 | if self._parse_started: |
| 2295 | self._decrement_running_parses() |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2296 | |
| 2297 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2298 | class DBError(Exception): |
| 2299 | """Raised by the DBObject constructor when its select fails.""" |
| 2300 | |
| 2301 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2302 | class DBObject(object): |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2303 | """A miniature object relational model for the database.""" |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2304 | |
| 2305 | # Subclasses MUST override these: |
| 2306 | _table_name = '' |
| 2307 | _fields = () |
| 2308 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2309 | # A mapping from (type, id) to the instance of the object for that |
| 2310 | # particular id. This prevents us from creating new Job() and Host() |
| 2311 | # instances for every HostQueueEntry object that we instantiate as |
| 2312 | # multiple HQEs often share the same Job. |
| 2313 | _instances_by_type_and_id = weakref.WeakValueDictionary() |
| 2314 | _initialized = False |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2315 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2316 | |
| 2317 | def __new__(cls, id=None, **kwargs): |
| 2318 | """ |
| 2319 | Look to see if we already have an instance for this particular type |
| 2320 | and id. If so, use it instead of creating a duplicate instance. |
| 2321 | """ |
| 2322 | if id is not None: |
| 2323 | instance = cls._instances_by_type_and_id.get((cls, id)) |
| 2324 | if instance: |
| 2325 | return instance |
| 2326 | return super(DBObject, cls).__new__(cls, id=id, **kwargs) |
| 2327 | |
| 2328 | |
| 2329 | def __init__(self, id=None, row=None, new_record=False, always_query=True): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2330 | assert bool(id) or bool(row) |
| 2331 | if id is not None and row is not None: |
| 2332 | assert id == row[0] |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2333 | assert self._table_name, '_table_name must be defined in your class' |
| 2334 | assert self._fields, '_fields must be defined in your class' |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2335 | if not new_record: |
| 2336 | if self._initialized and not always_query: |
| 2337 | return # We've already been initialized. |
| 2338 | if id is None: |
| 2339 | id = row[0] |
| 2340 | # Tell future constructors to use us instead of re-querying while |
| 2341 | # this instance is still around. |
| 2342 | self._instances_by_type_and_id[(type(self), id)] = self |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2343 | |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2344 | self.__table = self._table_name |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2345 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2346 | self.__new_record = new_record |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2347 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2348 | if row is None: |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 2349 | row = self._fetch_row_from_db(id) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2350 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2351 | if self._initialized: |
| 2352 | differences = self._compare_fields_in_row(row) |
| 2353 | if differences: |
showard | 7629f14 | 2009-03-27 21:02:02 +0000 | [diff] [blame] | 2354 | logging.warn( |
| 2355 | 'initialized %s %s instance requery is updating: %s', |
| 2356 | type(self), self.id, differences) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2357 | self._update_fields_from_row(row) |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2358 | self._initialized = True |
| 2359 | |
| 2360 | |
| 2361 | @classmethod |
| 2362 | def _clear_instance_cache(cls): |
| 2363 | """Used for testing, clear the internal instance cache.""" |
| 2364 | cls._instances_by_type_and_id.clear() |
| 2365 | |
| 2366 | |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 2367 | def _fetch_row_from_db(self, row_id): |
| 2368 | sql = 'SELECT * FROM %s WHERE ID=%%s' % self.__table |
| 2369 | rows = _db.execute(sql, (row_id,)) |
| 2370 | if not rows: |
showard | 76e29d1 | 2009-04-15 21:53:10 +0000 | [diff] [blame] | 2371 | raise DBError("row not found (table=%s, row id=%s)" |
| 2372 | % (self.__table, row_id)) |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 2373 | return rows[0] |
| 2374 | |
| 2375 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2376 | def _assert_row_length(self, row): |
| 2377 | assert len(row) == len(self._fields), ( |
| 2378 | "table = %s, row = %s/%d, fields = %s/%d" % ( |
| 2379 | self.__table, row, len(row), self._fields, len(self._fields))) |
| 2380 | |
| 2381 | |
| 2382 | def _compare_fields_in_row(self, row): |
| 2383 | """ |
showard | dae680a | 2009-10-12 20:26:43 +0000 | [diff] [blame] | 2384 | Given a row as returned by a SELECT query, compare it to our existing in |
| 2385 | memory fields. Fractional seconds are stripped from datetime values |
| 2386 | before comparison. |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2387 | |
| 2388 | @param row - A sequence of values corresponding to fields named in |
| 2389 | The class attribute _fields. |
| 2390 | |
| 2391 | @returns A dictionary listing the differences keyed by field name |
| 2392 | containing tuples of (current_value, row_value). |
| 2393 | """ |
| 2394 | self._assert_row_length(row) |
| 2395 | differences = {} |
showard | dae680a | 2009-10-12 20:26:43 +0000 | [diff] [blame] | 2396 | datetime_cmp_fmt = '%Y-%m-%d %H:%M:%S' # Leave off the microseconds. |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2397 | for field, row_value in itertools.izip(self._fields, row): |
| 2398 | current_value = getattr(self, field) |
showard | dae680a | 2009-10-12 20:26:43 +0000 | [diff] [blame] | 2399 | if (isinstance(current_value, datetime.datetime) |
| 2400 | and isinstance(row_value, datetime.datetime)): |
| 2401 | current_value = current_value.strftime(datetime_cmp_fmt) |
| 2402 | row_value = row_value.strftime(datetime_cmp_fmt) |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2403 | if current_value != row_value: |
| 2404 | differences[field] = (current_value, row_value) |
| 2405 | return differences |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2406 | |
| 2407 | |
| 2408 | def _update_fields_from_row(self, row): |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2409 | """ |
| 2410 | Update our field attributes using a single row returned by SELECT. |
| 2411 | |
| 2412 | @param row - A sequence of values corresponding to fields named in |
| 2413 | the class fields list. |
| 2414 | """ |
| 2415 | self._assert_row_length(row) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2416 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2417 | self._valid_fields = set() |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2418 | for field, value in itertools.izip(self._fields, row): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2419 | setattr(self, field, value) |
| 2420 | self._valid_fields.add(field) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2421 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2422 | self._valid_fields.remove('id') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2423 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2424 | |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 2425 | def update_from_database(self): |
| 2426 | assert self.id is not None |
| 2427 | row = self._fetch_row_from_db(self.id) |
| 2428 | self._update_fields_from_row(row) |
| 2429 | |
| 2430 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2431 | def count(self, where, table = None): |
| 2432 | if not table: |
| 2433 | table = self.__table |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2434 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2435 | rows = _db.execute(""" |
| 2436 | SELECT count(*) FROM %s |
| 2437 | WHERE %s |
| 2438 | """ % (table, where)) |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 2439 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2440 | assert len(rows) == 1 |
| 2441 | |
| 2442 | return int(rows[0][0]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2443 | |
| 2444 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2445 | def update_field(self, field, value): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2446 | assert field in self._valid_fields |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2447 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2448 | if getattr(self, field) == value: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2449 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2450 | |
mbligh | f8c624d | 2008-07-03 16:58:45 +0000 | [diff] [blame] | 2451 | query = "UPDATE %s SET %s = %%s WHERE id = %%s" % (self.__table, field) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2452 | _db.execute(query, (value, self.id)) |
| 2453 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2454 | setattr(self, field, value) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2455 | |
| 2456 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2457 | def save(self): |
| 2458 | if self.__new_record: |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2459 | keys = self._fields[1:] # avoid id |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2460 | columns = ','.join([str(key) for key in keys]) |
showard | 76e29d1 | 2009-04-15 21:53:10 +0000 | [diff] [blame] | 2461 | values = [] |
| 2462 | for key in keys: |
| 2463 | value = getattr(self, key) |
| 2464 | if value is None: |
| 2465 | values.append('NULL') |
| 2466 | else: |
| 2467 | values.append('"%s"' % value) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2468 | values_str = ','.join(values) |
| 2469 | query = ('INSERT INTO %s (%s) VALUES (%s)' % |
| 2470 | (self.__table, columns, values_str)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2471 | _db.execute(query) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2472 | # Update our id to the one the database just assigned to us. |
| 2473 | self.id = _db.execute('SELECT LAST_INSERT_ID()')[0][0] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2474 | |
| 2475 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2476 | def delete(self): |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2477 | self._instances_by_type_and_id.pop((type(self), id), None) |
| 2478 | self._initialized = False |
| 2479 | self._valid_fields.clear() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2480 | query = 'DELETE FROM %s WHERE id=%%s' % self.__table |
| 2481 | _db.execute(query, (self.id,)) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2482 | |
| 2483 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 2484 | @staticmethod |
| 2485 | def _prefix_with(string, prefix): |
| 2486 | if string: |
| 2487 | string = prefix + string |
| 2488 | return string |
| 2489 | |
| 2490 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2491 | @classmethod |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 2492 | def fetch(cls, where='', params=(), joins='', order_by=''): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2493 | """ |
| 2494 | Construct instances of our class based on the given database query. |
| 2495 | |
| 2496 | @yields One class instance for each row fetched. |
| 2497 | """ |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 2498 | order_by = cls._prefix_with(order_by, 'ORDER BY ') |
| 2499 | where = cls._prefix_with(where, 'WHERE ') |
| 2500 | query = ('SELECT %(table)s.* FROM %(table)s %(joins)s ' |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2501 | '%(where)s %(order_by)s' % {'table' : cls._table_name, |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 2502 | 'joins' : joins, |
| 2503 | 'where' : where, |
| 2504 | 'order_by' : order_by}) |
| 2505 | rows = _db.execute(query, params) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2506 | return [cls(id=row[0], row=row) for row in rows] |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2507 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2508 | |
| 2509 | class IneligibleHostQueue(DBObject): |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2510 | _table_name = 'ineligible_host_queues' |
| 2511 | _fields = ('id', 'job_id', 'host_id') |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 2512 | |
| 2513 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2514 | class AtomicGroup(DBObject): |
| 2515 | _table_name = 'atomic_groups' |
showard | 205fd60 | 2009-03-21 00:17:35 +0000 | [diff] [blame] | 2516 | _fields = ('id', 'name', 'description', 'max_number_of_machines', |
| 2517 | 'invalid') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2518 | |
| 2519 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 2520 | class Label(DBObject): |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2521 | _table_name = 'labels' |
| 2522 | _fields = ('id', 'name', 'kernel_config', 'platform', 'invalid', |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2523 | 'only_if_needed', 'atomic_group_id') |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 2524 | |
| 2525 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 2526 | def __repr__(self): |
| 2527 | return 'Label(name=%r, id=%d, atomic_group_id=%r)' % ( |
| 2528 | self.name, self.id, self.atomic_group_id) |
| 2529 | |
| 2530 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2531 | class Host(DBObject): |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2532 | _table_name = 'hosts' |
| 2533 | _fields = ('id', 'hostname', 'locked', 'synch_id', 'status', |
| 2534 | 'invalid', 'protection', 'locked_by_id', 'lock_time', 'dirty') |
| 2535 | |
| 2536 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2537 | def set_status(self,status): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2538 | logging.info('%s -> %s', self.hostname, status) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2539 | self.update_field('status',status) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2540 | |
| 2541 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2542 | def platform_and_labels(self): |
showard | d8e548a | 2008-09-09 03:04:57 +0000 | [diff] [blame] | 2543 | """ |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2544 | Returns a tuple (platform_name, list_of_all_label_names). |
showard | d8e548a | 2008-09-09 03:04:57 +0000 | [diff] [blame] | 2545 | """ |
| 2546 | rows = _db.execute(""" |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2547 | SELECT labels.name, labels.platform |
showard | d8e548a | 2008-09-09 03:04:57 +0000 | [diff] [blame] | 2548 | FROM labels |
| 2549 | INNER JOIN hosts_labels ON labels.id = hosts_labels.label_id |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2550 | WHERE hosts_labels.host_id = %s |
showard | d8e548a | 2008-09-09 03:04:57 +0000 | [diff] [blame] | 2551 | ORDER BY labels.name |
| 2552 | """, (self.id,)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2553 | platform = None |
| 2554 | all_labels = [] |
| 2555 | for label_name, is_platform in rows: |
| 2556 | if is_platform: |
| 2557 | platform = label_name |
| 2558 | all_labels.append(label_name) |
| 2559 | return platform, all_labels |
| 2560 | |
| 2561 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 2562 | _ALPHANUM_HOST_RE = re.compile(r'^([a-z-]+)(\d+)$', re.IGNORECASE) |
| 2563 | |
| 2564 | |
| 2565 | @classmethod |
| 2566 | def cmp_for_sort(cls, a, b): |
| 2567 | """ |
| 2568 | A comparison function for sorting Host objects by hostname. |
| 2569 | |
| 2570 | This strips any trailing numeric digits, ignores leading 0s and |
| 2571 | compares hostnames by the leading name and the trailing digits as a |
| 2572 | number. If both hostnames do not match this pattern, they are simply |
| 2573 | compared as lower case strings. |
| 2574 | |
| 2575 | Example of how hostnames will be sorted: |
| 2576 | |
| 2577 | alice, host1, host2, host09, host010, host10, host11, yolkfolk |
| 2578 | |
| 2579 | This hopefully satisfy most people's hostname sorting needs regardless |
| 2580 | of their exact naming schemes. Nobody sane should have both a host10 |
| 2581 | and host010 (but the algorithm works regardless). |
| 2582 | """ |
| 2583 | lower_a = a.hostname.lower() |
| 2584 | lower_b = b.hostname.lower() |
| 2585 | match_a = cls._ALPHANUM_HOST_RE.match(lower_a) |
| 2586 | match_b = cls._ALPHANUM_HOST_RE.match(lower_b) |
| 2587 | if match_a and match_b: |
| 2588 | name_a, number_a_str = match_a.groups() |
| 2589 | name_b, number_b_str = match_b.groups() |
| 2590 | number_a = int(number_a_str.lstrip('0')) |
| 2591 | number_b = int(number_b_str.lstrip('0')) |
| 2592 | result = cmp((name_a, number_a), (name_b, number_b)) |
| 2593 | if result == 0 and lower_a != lower_b: |
| 2594 | # If they compared equal above but the lower case names are |
| 2595 | # indeed different, don't report equality. abc012 != abc12. |
| 2596 | return cmp(lower_a, lower_b) |
| 2597 | return result |
| 2598 | else: |
| 2599 | return cmp(lower_a, lower_b) |
| 2600 | |
| 2601 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2602 | class HostQueueEntry(DBObject): |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2603 | _table_name = 'host_queue_entries' |
| 2604 | _fields = ('id', 'job_id', 'host_id', 'status', 'meta_host', |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2605 | 'active', 'complete', 'deleted', 'execution_subdir', |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 2606 | 'atomic_group_id', 'aborted', 'started_on') |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2607 | |
| 2608 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2609 | def __init__(self, id=None, row=None, **kwargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2610 | assert id or row |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2611 | super(HostQueueEntry, self).__init__(id=id, row=row, **kwargs) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2612 | self.job = Job(self.job_id) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2613 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2614 | if self.host_id: |
| 2615 | self.host = Host(self.host_id) |
| 2616 | else: |
| 2617 | self.host = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2618 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2619 | if self.atomic_group_id: |
| 2620 | self.atomic_group = AtomicGroup(self.atomic_group_id, |
| 2621 | always_query=False) |
| 2622 | else: |
| 2623 | self.atomic_group = None |
| 2624 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2625 | self.queue_log_path = os.path.join(self.job.tag(), |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2626 | 'queue.log.' + str(self.id)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2627 | |
| 2628 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2629 | @classmethod |
| 2630 | def clone(cls, template): |
| 2631 | """ |
| 2632 | Creates a new row using the values from a template instance. |
| 2633 | |
| 2634 | The new instance will not exist in the database or have a valid |
| 2635 | id attribute until its save() method is called. |
| 2636 | """ |
| 2637 | assert isinstance(template, cls) |
| 2638 | new_row = [getattr(template, field) for field in cls._fields] |
| 2639 | clone = cls(row=new_row, new_record=True) |
| 2640 | clone.id = None |
| 2641 | return clone |
| 2642 | |
| 2643 | |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2644 | def _view_job_url(self): |
| 2645 | return "%s#tab_id=view_job&object_id=%s" % (_base_url, self.job.id) |
| 2646 | |
| 2647 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2648 | def get_labels(self): |
| 2649 | """ |
| 2650 | Get all labels associated with this host queue entry (either via the |
| 2651 | meta_host or as a job dependency label). The labels yielded are not |
| 2652 | guaranteed to be unique. |
| 2653 | |
| 2654 | @yields Label instances associated with this host_queue_entry. |
| 2655 | """ |
| 2656 | if self.meta_host: |
| 2657 | yield Label(id=self.meta_host, always_query=False) |
| 2658 | labels = Label.fetch( |
| 2659 | joins="JOIN jobs_dependency_labels AS deps " |
| 2660 | "ON (labels.id = deps.label_id)", |
| 2661 | where="deps.job_id = %d" % self.job.id) |
| 2662 | for label in labels: |
| 2663 | yield label |
| 2664 | |
| 2665 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2666 | def set_host(self, host): |
| 2667 | if host: |
| 2668 | self.queue_log_record('Assigning host ' + host.hostname) |
| 2669 | self.update_field('host_id', host.id) |
| 2670 | self.update_field('active', True) |
| 2671 | self.block_host(host.id) |
| 2672 | else: |
| 2673 | self.queue_log_record('Releasing host') |
| 2674 | self.unblock_host(self.host.id) |
| 2675 | self.update_field('host_id', None) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2676 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2677 | self.host = host |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2678 | |
| 2679 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2680 | def get_host(self): |
| 2681 | return self.host |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2682 | |
| 2683 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2684 | def queue_log_record(self, log_line): |
| 2685 | now = str(datetime.datetime.now()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2686 | _drone_manager.write_lines_to_file(self.queue_log_path, |
| 2687 | [now + ' ' + log_line]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2688 | |
| 2689 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2690 | def block_host(self, host_id): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2691 | logging.info("creating block %s/%s", self.job.id, host_id) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2692 | row = [0, self.job.id, host_id] |
| 2693 | block = IneligibleHostQueue(row=row, new_record=True) |
| 2694 | block.save() |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2695 | |
| 2696 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2697 | def unblock_host(self, host_id): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2698 | logging.info("removing block %s/%s", self.job.id, host_id) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2699 | blocks = IneligibleHostQueue.fetch( |
| 2700 | 'job_id=%d and host_id=%d' % (self.job.id, host_id)) |
| 2701 | for block in blocks: |
| 2702 | block.delete() |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2703 | |
| 2704 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2705 | def set_execution_subdir(self, subdir=None): |
| 2706 | if subdir is None: |
| 2707 | assert self.get_host() |
| 2708 | subdir = self.get_host().hostname |
| 2709 | self.update_field('execution_subdir', subdir) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2710 | |
| 2711 | |
showard | 6355f6b | 2008-12-05 18:52:13 +0000 | [diff] [blame] | 2712 | def _get_hostname(self): |
| 2713 | if self.host: |
| 2714 | return self.host.hostname |
| 2715 | return 'no host' |
| 2716 | |
| 2717 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2718 | def __str__(self): |
showard | 828fc4c | 2009-09-14 20:31:00 +0000 | [diff] [blame] | 2719 | flags = [] |
| 2720 | if self.active: |
| 2721 | flags.append('active') |
| 2722 | if self.complete: |
| 2723 | flags.append('complete') |
| 2724 | if self.deleted: |
| 2725 | flags.append('deleted') |
| 2726 | if self.aborted: |
| 2727 | flags.append('aborted') |
| 2728 | flags_str = ','.join(flags) |
| 2729 | if flags_str: |
| 2730 | flags_str = ' [%s]' % flags_str |
| 2731 | return "%s/%d (%d) %s%s" % (self._get_hostname(), self.job.id, self.id, |
| 2732 | self.status, flags_str) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2733 | |
| 2734 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2735 | def set_status(self, status): |
showard | 5682407 | 2009-10-12 20:30:21 +0000 | [diff] [blame] | 2736 | logging.info("%s -> %s", self, status) |
mbligh | f8c624d | 2008-07-03 16:58:45 +0000 | [diff] [blame] | 2737 | |
showard | 5682407 | 2009-10-12 20:30:21 +0000 | [diff] [blame] | 2738 | self.update_field('status', status) |
mbligh | f8c624d | 2008-07-03 16:58:45 +0000 | [diff] [blame] | 2739 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2740 | if status in (models.HostQueueEntry.Status.QUEUED, |
| 2741 | models.HostQueueEntry.Status.PARSING): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2742 | self.update_field('complete', False) |
| 2743 | self.update_field('active', False) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2744 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2745 | if status in (models.HostQueueEntry.Status.PENDING, |
| 2746 | models.HostQueueEntry.Status.RUNNING, |
| 2747 | models.HostQueueEntry.Status.VERIFYING, |
| 2748 | models.HostQueueEntry.Status.STARTING, |
| 2749 | models.HostQueueEntry.Status.GATHERING): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2750 | self.update_field('complete', False) |
| 2751 | self.update_field('active', True) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2752 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2753 | if status in (models.HostQueueEntry.Status.FAILED, |
| 2754 | models.HostQueueEntry.Status.COMPLETED, |
| 2755 | models.HostQueueEntry.Status.STOPPED, |
| 2756 | models.HostQueueEntry.Status.ABORTED): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2757 | self.update_field('complete', True) |
| 2758 | self.update_field('active', False) |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 2759 | self._on_complete() |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2760 | |
| 2761 | should_email_status = (status.lower() in _notify_email_statuses or |
| 2762 | 'all' in _notify_email_statuses) |
| 2763 | if should_email_status: |
| 2764 | self._email_on_status(status) |
| 2765 | |
| 2766 | self._email_on_job_complete() |
| 2767 | |
| 2768 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 2769 | def _on_complete(self): |
| 2770 | if not self.execution_subdir: |
| 2771 | return |
| 2772 | # unregister any possible pidfiles associated with this queue entry |
| 2773 | for pidfile_name in _ALL_PIDFILE_NAMES: |
| 2774 | pidfile_id = _drone_manager.get_pidfile_id_from( |
| 2775 | self.execution_path(), pidfile_name=pidfile_name) |
| 2776 | _drone_manager.unregister_pidfile(pidfile_id) |
| 2777 | |
| 2778 | |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2779 | def _email_on_status(self, status): |
showard | 6355f6b | 2008-12-05 18:52:13 +0000 | [diff] [blame] | 2780 | hostname = self._get_hostname() |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2781 | |
| 2782 | subject = 'Autotest: Job ID: %s "%s" Host: %s %s' % ( |
| 2783 | self.job.id, self.job.name, hostname, status) |
| 2784 | body = "Job ID: %s\nJob Name: %s\nHost: %s\nStatus: %s\n%s\n" % ( |
| 2785 | self.job.id, self.job.name, hostname, status, |
| 2786 | self._view_job_url()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2787 | email_manager.manager.send_email(self.job.email_list, subject, body) |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 2788 | |
| 2789 | |
| 2790 | def _email_on_job_complete(self): |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2791 | if not self.job.is_finished(): |
| 2792 | return |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 2793 | |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2794 | summary_text = [] |
showard | 6355f6b | 2008-12-05 18:52:13 +0000 | [diff] [blame] | 2795 | hosts_queue = HostQueueEntry.fetch('job_id = %s' % self.job.id) |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2796 | for queue_entry in hosts_queue: |
| 2797 | summary_text.append("Host: %s Status: %s" % |
showard | 6355f6b | 2008-12-05 18:52:13 +0000 | [diff] [blame] | 2798 | (queue_entry._get_hostname(), |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2799 | queue_entry.status)) |
| 2800 | |
| 2801 | summary_text = "\n".join(summary_text) |
| 2802 | status_counts = models.Job.objects.get_status_counts( |
| 2803 | [self.job.id])[self.job.id] |
| 2804 | status = ', '.join('%d %s' % (count, status) for status, count |
| 2805 | in status_counts.iteritems()) |
| 2806 | |
| 2807 | subject = 'Autotest: Job ID: %s "%s" %s' % ( |
| 2808 | self.job.id, self.job.name, status) |
| 2809 | body = "Job ID: %s\nJob Name: %s\nStatus: %s\n%s\nSummary:\n%s" % ( |
| 2810 | self.job.id, self.job.name, status, self._view_job_url(), |
| 2811 | summary_text) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2812 | email_manager.manager.send_email(self.job.email_list, subject, body) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2813 | |
| 2814 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2815 | def schedule_pre_job_tasks(self, assigned_host=None): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2816 | if self.meta_host is not None or self.atomic_group: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2817 | assert assigned_host |
| 2818 | # ensure results dir exists for the queue log |
showard | 08356c1 | 2009-06-15 20:24:01 +0000 | [diff] [blame] | 2819 | if self.host_id is None: |
| 2820 | self.set_host(assigned_host) |
| 2821 | else: |
| 2822 | assert assigned_host.id == self.host_id |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2823 | |
showard | cfd4a7e | 2009-07-11 01:47:33 +0000 | [diff] [blame] | 2824 | logging.info("%s/%s/%s scheduled on %s, status=%s", |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2825 | self.job.name, self.meta_host, self.atomic_group_id, |
| 2826 | self.host.hostname, self.status) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2827 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2828 | self._do_schedule_pre_job_tasks() |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2829 | |
| 2830 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2831 | def _do_schedule_pre_job_tasks(self): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2832 | # Every host goes thru the Verifying stage (which may or may not |
| 2833 | # actually do anything as determined by get_pre_job_tasks). |
| 2834 | self.set_status(models.HostQueueEntry.Status.VERIFYING) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2835 | self.job.schedule_pre_job_tasks(queue_entry=self) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2836 | |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2837 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2838 | def requeue(self): |
showard | cfd4a7e | 2009-07-11 01:47:33 +0000 | [diff] [blame] | 2839 | assert self.host |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2840 | self.set_status(models.HostQueueEntry.Status.QUEUED) |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 2841 | self.update_field('started_on', None) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 2842 | # verify/cleanup failure sets the execution subdir, so reset it here |
| 2843 | self.set_execution_subdir('') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2844 | if self.meta_host: |
| 2845 | self.set_host(None) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2846 | |
| 2847 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2848 | def handle_host_failure(self): |
| 2849 | """\ |
| 2850 | Called when this queue entry's host has failed verification and |
| 2851 | repair. |
| 2852 | """ |
| 2853 | assert not self.meta_host |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2854 | self.set_status(models.HostQueueEntry.Status.FAILED) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2855 | self.job.stop_if_necessary() |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2856 | |
| 2857 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2858 | @property |
| 2859 | def aborted_by(self): |
| 2860 | self._load_abort_info() |
| 2861 | return self._aborted_by |
| 2862 | |
| 2863 | |
| 2864 | @property |
| 2865 | def aborted_on(self): |
| 2866 | self._load_abort_info() |
| 2867 | return self._aborted_on |
| 2868 | |
| 2869 | |
| 2870 | def _load_abort_info(self): |
| 2871 | """ Fetch info about who aborted the job. """ |
| 2872 | if hasattr(self, "_aborted_by"): |
| 2873 | return |
| 2874 | rows = _db.execute(""" |
| 2875 | SELECT users.login, aborted_host_queue_entries.aborted_on |
| 2876 | FROM aborted_host_queue_entries |
| 2877 | INNER JOIN users |
| 2878 | ON users.id = aborted_host_queue_entries.aborted_by_id |
| 2879 | WHERE aborted_host_queue_entries.queue_entry_id = %s |
| 2880 | """, (self.id,)) |
| 2881 | if rows: |
| 2882 | self._aborted_by, self._aborted_on = rows[0] |
| 2883 | else: |
| 2884 | self._aborted_by = self._aborted_on = None |
| 2885 | |
| 2886 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2887 | def on_pending(self): |
| 2888 | """ |
| 2889 | Called when an entry in a synchronous job has passed verify. If the |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2890 | job is ready to run, sets the entries to STARTING. Otherwise, it leaves |
| 2891 | them in PENDING. |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2892 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2893 | self.set_status(models.HostQueueEntry.Status.PENDING) |
| 2894 | self.host.set_status(models.Host.Status.PENDING) |
showard | b000a8d | 2009-07-28 20:02:07 +0000 | [diff] [blame] | 2895 | |
| 2896 | # Some debug code here: sends an email if an asynchronous job does not |
| 2897 | # immediately enter Starting. |
| 2898 | # TODO: Remove this once we figure out why asynchronous jobs are getting |
| 2899 | # stuck in Pending. |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2900 | self.job.run_if_ready(queue_entry=self) |
| 2901 | if (self.job.synch_count == 1 and |
| 2902 | self.status == models.HostQueueEntry.Status.PENDING): |
showard | b000a8d | 2009-07-28 20:02:07 +0000 | [diff] [blame] | 2903 | subject = 'Job %s (id %s)' % (self.job.name, self.job.id) |
| 2904 | message = 'Asynchronous job stuck in Pending' |
| 2905 | email_manager.manager.enqueue_notify_email(subject, message) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2906 | |
| 2907 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2908 | def abort(self, dispatcher): |
| 2909 | assert self.aborted and not self.complete |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 2910 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2911 | Status = models.HostQueueEntry.Status |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2912 | if self.status in (Status.GATHERING, Status.PARSING): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2913 | # do nothing; post-job tasks will finish and then mark this entry |
| 2914 | # with status "Aborted" and take care of the host |
| 2915 | return |
| 2916 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2917 | if self.status in (Status.STARTING, Status.PENDING, Status.RUNNING): |
| 2918 | assert not dispatcher.get_agents_for_entry(self) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2919 | self.host.set_status(models.Host.Status.READY) |
| 2920 | elif self.status == Status.VERIFYING: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2921 | models.SpecialTask.objects.create( |
| 2922 | task=models.SpecialTask.Task.CLEANUP, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2923 | host=models.Host.objects.get(id=self.host.id), |
| 2924 | requested_by=self.job.owner_model()) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2925 | |
| 2926 | self.set_status(Status.ABORTED) |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 2927 | self.job.abort_delay_ready_task() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2928 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2929 | |
| 2930 | def get_group_name(self): |
| 2931 | atomic_group = self.atomic_group |
| 2932 | if not atomic_group: |
| 2933 | return '' |
| 2934 | |
| 2935 | # Look at any meta_host and dependency labels and pick the first |
| 2936 | # one that also specifies this atomic group. Use that label name |
| 2937 | # as the group name if possible (it is more specific). |
| 2938 | for label in self.get_labels(): |
| 2939 | if label.atomic_group_id: |
| 2940 | assert label.atomic_group_id == atomic_group.id |
| 2941 | return label.name |
| 2942 | return atomic_group.name |
| 2943 | |
| 2944 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2945 | def execution_tag(self): |
| 2946 | assert self.execution_subdir |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 2947 | return "%s/%s" % (self.job.tag(), self.execution_subdir) |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 2948 | |
| 2949 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2950 | def execution_path(self): |
| 2951 | return self.execution_tag() |
| 2952 | |
| 2953 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2954 | class Job(DBObject): |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2955 | _table_name = 'jobs' |
| 2956 | _fields = ('id', 'owner', 'name', 'priority', 'control_file', |
| 2957 | 'control_type', 'created_on', 'synch_count', 'timeout', |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 2958 | 'run_verify', 'email_list', 'reboot_before', 'reboot_after', |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 2959 | 'parse_failed_repair', 'max_runtime_hrs') |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2960 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2961 | # This does not need to be a column in the DB. The delays are likely to |
| 2962 | # be configured short. If the scheduler is stopped and restarted in |
| 2963 | # the middle of a job's delay cycle, the delay cycle will either be |
| 2964 | # repeated or skipped depending on the number of Pending machines found |
| 2965 | # when the restarted scheduler recovers to track it. Not a problem. |
| 2966 | # |
| 2967 | # A reference to the DelayedCallTask that will wake up the job should |
| 2968 | # no other HQEs change state in time. Its end_time attribute is used |
| 2969 | # by our run_with_ready_delay() method to determine if the wait is over. |
| 2970 | _delay_ready_task = None |
| 2971 | |
| 2972 | # TODO(gps): On scheduler start/recovery we need to call HQE.on_pending() on |
| 2973 | # all status='Pending' atomic group HQEs incase a delay was running when the |
| 2974 | # scheduler was restarted and no more hosts ever successfully exit Verify. |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2975 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2976 | def __init__(self, id=None, row=None, **kwargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2977 | assert id or row |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2978 | super(Job, self).__init__(id=id, row=row, **kwargs) |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2979 | self._owner_model = None # caches model instance of owner |
| 2980 | |
| 2981 | |
| 2982 | def owner_model(self): |
| 2983 | # work around the fact that the Job owner field is a string, not a |
| 2984 | # foreign key |
| 2985 | if not self._owner_model: |
| 2986 | self._owner_model = models.User.objects.get(login=self.owner) |
| 2987 | return self._owner_model |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2988 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2989 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2990 | def is_server_job(self): |
| 2991 | return self.control_type != 2 |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2992 | |
| 2993 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2994 | def tag(self): |
| 2995 | return "%s-%s" % (self.id, self.owner) |
| 2996 | |
| 2997 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2998 | def get_host_queue_entries(self): |
| 2999 | rows = _db.execute(""" |
| 3000 | SELECT * FROM host_queue_entries |
| 3001 | WHERE job_id= %s |
| 3002 | """, (self.id,)) |
| 3003 | entries = [HostQueueEntry(row=i) for i in rows] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3004 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3005 | assert len(entries)>0 |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3006 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3007 | return entries |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3008 | |
| 3009 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3010 | def set_status(self, status, update_queues=False): |
| 3011 | self.update_field('status',status) |
| 3012 | |
| 3013 | if update_queues: |
| 3014 | for queue_entry in self.get_host_queue_entries(): |
| 3015 | queue_entry.set_status(status) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3016 | |
| 3017 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3018 | def _atomic_and_has_started(self): |
| 3019 | """ |
| 3020 | @returns True if any of the HostQueueEntries associated with this job |
| 3021 | have entered the Status.STARTING state or beyond. |
| 3022 | """ |
| 3023 | atomic_entries = models.HostQueueEntry.objects.filter( |
| 3024 | job=self.id, atomic_group__isnull=False) |
| 3025 | if atomic_entries.count() <= 0: |
| 3026 | return False |
| 3027 | |
showard | af8b4ca | 2009-06-16 18:47:26 +0000 | [diff] [blame] | 3028 | # These states may *only* be reached if Job.run() has been called. |
| 3029 | started_statuses = (models.HostQueueEntry.Status.STARTING, |
| 3030 | models.HostQueueEntry.Status.RUNNING, |
| 3031 | models.HostQueueEntry.Status.COMPLETED) |
| 3032 | |
| 3033 | started_entries = atomic_entries.filter(status__in=started_statuses) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3034 | return started_entries.count() > 0 |
| 3035 | |
| 3036 | |
showard | 708b352 | 2009-08-20 23:26:15 +0000 | [diff] [blame] | 3037 | def _hosts_assigned_count(self): |
| 3038 | """The number of HostQueueEntries assigned a Host for this job.""" |
| 3039 | entries = models.HostQueueEntry.objects.filter(job=self.id, |
| 3040 | host__isnull=False) |
| 3041 | return entries.count() |
| 3042 | |
| 3043 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3044 | def _pending_count(self): |
| 3045 | """The number of HostQueueEntries for this job in the Pending state.""" |
| 3046 | pending_entries = models.HostQueueEntry.objects.filter( |
| 3047 | job=self.id, status=models.HostQueueEntry.Status.PENDING) |
| 3048 | return pending_entries.count() |
| 3049 | |
| 3050 | |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame^] | 3051 | def _max_hosts_needed_to_run(self, atomic_group): |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 3052 | """ |
| 3053 | @param atomic_group: The AtomicGroup associated with this job that we |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame^] | 3054 | are using to set an upper bound on the threshold. |
| 3055 | @returns The maximum number of HostQueueEntries assigned a Host before |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 3056 | this job can run. |
| 3057 | """ |
| 3058 | return min(self._hosts_assigned_count(), |
| 3059 | atomic_group.max_number_of_machines) |
| 3060 | |
| 3061 | |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame^] | 3062 | def _min_hosts_needed_to_run(self): |
| 3063 | """Return the minumum number of hsots needed to run this job.""" |
| 3064 | return self.synch_count |
| 3065 | |
| 3066 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3067 | def is_ready(self): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3068 | # NOTE: Atomic group jobs stop reporting ready after they have been |
| 3069 | # started to avoid launching multiple copies of one atomic job. |
| 3070 | # Only possible if synch_count is less than than half the number of |
| 3071 | # machines in the atomic group. |
showard | b000a8d | 2009-07-28 20:02:07 +0000 | [diff] [blame] | 3072 | pending_count = self._pending_count() |
| 3073 | atomic_and_has_started = self._atomic_and_has_started() |
| 3074 | ready = (pending_count >= self.synch_count |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 3075 | and not atomic_and_has_started) |
showard | b000a8d | 2009-07-28 20:02:07 +0000 | [diff] [blame] | 3076 | |
| 3077 | if not ready: |
| 3078 | logging.info( |
| 3079 | 'Job %s not ready: %s pending, %s required ' |
| 3080 | '(Atomic and started: %s)', |
| 3081 | self, pending_count, self.synch_count, |
| 3082 | atomic_and_has_started) |
| 3083 | |
| 3084 | return ready |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3085 | |
| 3086 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3087 | def num_machines(self, clause = None): |
| 3088 | sql = "job_id=%s" % self.id |
| 3089 | if clause: |
| 3090 | sql += " AND (%s)" % clause |
| 3091 | return self.count(sql, table='host_queue_entries') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3092 | |
| 3093 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3094 | def num_queued(self): |
| 3095 | return self.num_machines('not complete') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3096 | |
| 3097 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3098 | def num_active(self): |
| 3099 | return self.num_machines('active') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3100 | |
| 3101 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3102 | def num_complete(self): |
| 3103 | return self.num_machines('complete') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3104 | |
| 3105 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3106 | def is_finished(self): |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 3107 | return self.num_complete() == self.num_machines() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3108 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3109 | |
showard | 6bb7c29 | 2009-01-30 01:44:51 +0000 | [diff] [blame] | 3110 | def _not_yet_run_entries(self, include_verifying=True): |
| 3111 | statuses = [models.HostQueueEntry.Status.QUEUED, |
| 3112 | models.HostQueueEntry.Status.PENDING] |
| 3113 | if include_verifying: |
| 3114 | statuses.append(models.HostQueueEntry.Status.VERIFYING) |
| 3115 | return models.HostQueueEntry.objects.filter(job=self.id, |
| 3116 | status__in=statuses) |
| 3117 | |
| 3118 | |
| 3119 | def _stop_all_entries(self): |
| 3120 | entries_to_stop = self._not_yet_run_entries( |
| 3121 | include_verifying=False) |
| 3122 | for child_entry in entries_to_stop: |
showard | 4f9e537 | 2009-01-07 21:33:38 +0000 | [diff] [blame] | 3123 | assert not child_entry.complete, ( |
| 3124 | '%s status=%s, active=%s, complete=%s' % |
| 3125 | (child_entry.id, child_entry.status, child_entry.active, |
| 3126 | child_entry.complete)) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3127 | if child_entry.status == models.HostQueueEntry.Status.PENDING: |
| 3128 | child_entry.host.status = models.Host.Status.READY |
| 3129 | child_entry.host.save() |
| 3130 | child_entry.status = models.HostQueueEntry.Status.STOPPED |
| 3131 | child_entry.save() |
| 3132 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3133 | def stop_if_necessary(self): |
showard | 6bb7c29 | 2009-01-30 01:44:51 +0000 | [diff] [blame] | 3134 | not_yet_run = self._not_yet_run_entries() |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3135 | if not_yet_run.count() < self.synch_count: |
showard | 6bb7c29 | 2009-01-30 01:44:51 +0000 | [diff] [blame] | 3136 | self._stop_all_entries() |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 3137 | |
| 3138 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3139 | def write_to_machines_file(self, queue_entry): |
| 3140 | hostname = queue_entry.get_host().hostname |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 3141 | file_path = os.path.join(self.tag(), '.machines') |
| 3142 | _drone_manager.write_lines_to_file(file_path, [hostname]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3143 | |
| 3144 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3145 | def _next_group_name(self, group_name=''): |
| 3146 | """@returns a directory name to use for the next host group results.""" |
| 3147 | if group_name: |
| 3148 | # Sanitize for use as a pathname. |
| 3149 | group_name = group_name.replace(os.path.sep, '_') |
| 3150 | if group_name.startswith('.'): |
| 3151 | group_name = '_' + group_name[1:] |
| 3152 | # Add a separator between the group name and 'group%d'. |
| 3153 | group_name += '.' |
| 3154 | group_count_re = re.compile(r'%sgroup(\d+)' % re.escape(group_name)) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3155 | query = models.HostQueueEntry.objects.filter( |
| 3156 | job=self.id).values('execution_subdir').distinct() |
| 3157 | subdirs = (entry['execution_subdir'] for entry in query) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3158 | group_matches = (group_count_re.match(subdir) for subdir in subdirs) |
| 3159 | ids = [int(match.group(1)) for match in group_matches if match] |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3160 | if ids: |
| 3161 | next_id = max(ids) + 1 |
| 3162 | else: |
| 3163 | next_id = 0 |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3164 | return '%sgroup%d' % (group_name, next_id) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3165 | |
| 3166 | |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 3167 | def _write_control_file(self, execution_path): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 3168 | control_path = _drone_manager.attach_file_to_execution( |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 3169 | execution_path, self.control_file) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 3170 | return control_path |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3171 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 3172 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3173 | def get_group_entries(self, queue_entry_from_group): |
showard | 8375ce0 | 2009-10-12 20:35:13 +0000 | [diff] [blame] | 3174 | """ |
| 3175 | @param queue_entry_from_group: A HostQueueEntry instance to find other |
| 3176 | group entries on this job for. |
| 3177 | |
| 3178 | @returns A list of HostQueueEntry objects all executing this job as |
| 3179 | part of the same group as the one supplied (having the same |
| 3180 | execution_subdir). |
| 3181 | """ |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3182 | execution_subdir = queue_entry_from_group.execution_subdir |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 3183 | return list(HostQueueEntry.fetch( |
| 3184 | where='job_id=%s AND execution_subdir=%s', |
| 3185 | params=(self.id, execution_subdir))) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3186 | |
| 3187 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3188 | def get_autoserv_params(self, queue_entries): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 3189 | assert queue_entries |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 3190 | execution_path = queue_entries[0].execution_path() |
| 3191 | control_path = self._write_control_file(execution_path) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3192 | hostnames = ','.join([entry.get_host().hostname |
| 3193 | for entry in queue_entries]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3194 | |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 3195 | execution_tag = queue_entries[0].execution_tag() |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 3196 | params = _autoserv_command_line( |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 3197 | hostnames, |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 3198 | ['-P', execution_tag, '-n', |
| 3199 | _drone_manager.absolute_path(control_path)], |
showard | e9c6936 | 2009-06-30 01:58:03 +0000 | [diff] [blame] | 3200 | job=self, verbose=False) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3201 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3202 | if not self.is_server_job(): |
| 3203 | params.append('-c') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3204 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 3205 | return params |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 3206 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3207 | |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 3208 | def _should_run_cleanup(self, queue_entry): |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 3209 | if self.reboot_before == models.RebootBefore.ALWAYS: |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 3210 | return True |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 3211 | elif self.reboot_before == models.RebootBefore.IF_DIRTY: |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 3212 | return queue_entry.get_host().dirty |
| 3213 | return False |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 3214 | |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 3215 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3216 | def _should_run_verify(self, queue_entry): |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 3217 | do_not_verify = (queue_entry.host.protection == |
| 3218 | host_protections.Protection.DO_NOT_VERIFY) |
| 3219 | if do_not_verify: |
| 3220 | return False |
| 3221 | return self.run_verify |
| 3222 | |
| 3223 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3224 | def schedule_pre_job_tasks(self, queue_entry): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3225 | """ |
| 3226 | Get a list of tasks to perform before the host_queue_entry |
| 3227 | may be used to run this Job (such as Cleanup & Verify). |
| 3228 | |
| 3229 | @returns A list of tasks to be done to the given queue_entry before |
mbligh | 6fbdb80 | 2009-08-03 16:42:55 +0000 | [diff] [blame] | 3230 | it should be considered be ready to run this job. The last |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3231 | task in the list calls HostQueueEntry.on_pending(), which |
| 3232 | continues the flow of the job. |
| 3233 | """ |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 3234 | if self._should_run_cleanup(queue_entry): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3235 | task = models.SpecialTask.Task.CLEANUP |
| 3236 | elif self._should_run_verify(queue_entry): |
| 3237 | task = models.SpecialTask.Task.VERIFY |
| 3238 | else: |
| 3239 | queue_entry.on_pending() |
| 3240 | return |
| 3241 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 3242 | queue_entry = models.HostQueueEntry.objects.get(id=queue_entry.id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3243 | models.SpecialTask.objects.create( |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 3244 | host=models.Host.objects.get(id=queue_entry.host_id), |
| 3245 | queue_entry=queue_entry, task=task) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 3246 | |
| 3247 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3248 | def _assign_new_group(self, queue_entries, group_name=''): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3249 | if len(queue_entries) == 1: |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3250 | group_subdir_name = queue_entries[0].get_host().hostname |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3251 | else: |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3252 | group_subdir_name = self._next_group_name(group_name) |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 3253 | logging.info('Running synchronous job %d hosts %s as %s', |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3254 | self.id, [entry.host.hostname for entry in queue_entries], |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3255 | group_subdir_name) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3256 | |
| 3257 | for queue_entry in queue_entries: |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3258 | queue_entry.set_execution_subdir(group_subdir_name) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3259 | |
| 3260 | |
| 3261 | def _choose_group_to_run(self, include_queue_entry): |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3262 | """ |
| 3263 | @returns A tuple containing a list of HostQueueEntry instances to be |
| 3264 | used to run this Job, a string group name to suggest giving |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 3265 | to this job in the results database. |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3266 | """ |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3267 | atomic_group = include_queue_entry.atomic_group |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3268 | chosen_entries = [include_queue_entry] |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3269 | if atomic_group: |
| 3270 | num_entries_wanted = atomic_group.max_number_of_machines |
| 3271 | else: |
| 3272 | num_entries_wanted = self.synch_count |
| 3273 | num_entries_wanted -= len(chosen_entries) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3274 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3275 | if num_entries_wanted > 0: |
| 3276 | where_clause = 'job_id = %s AND status = "Pending" AND id != %s' |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 3277 | pending_entries = list(HostQueueEntry.fetch( |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3278 | where=where_clause, |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 3279 | params=(self.id, include_queue_entry.id))) |
| 3280 | |
| 3281 | # Sort the chosen hosts by hostname before slicing. |
| 3282 | def cmp_queue_entries_by_hostname(entry_a, entry_b): |
| 3283 | return Host.cmp_for_sort(entry_a.host, entry_b.host) |
| 3284 | pending_entries.sort(cmp=cmp_queue_entries_by_hostname) |
| 3285 | chosen_entries += pending_entries[:num_entries_wanted] |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3286 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3287 | # Sanity check. We'll only ever be called if this can be met. |
showard | 828fc4c | 2009-09-14 20:31:00 +0000 | [diff] [blame] | 3288 | if len(chosen_entries) < self.synch_count: |
| 3289 | message = ('job %s got less than %s chosen entries: %s' % ( |
| 3290 | self.id, self.synch_count, chosen_entries)) |
| 3291 | logging.error(message) |
| 3292 | email_manager.manager.enqueue_notify_email( |
| 3293 | 'Job not started, too few chosen entries', message) |
| 3294 | return [] |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3295 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3296 | group_name = include_queue_entry.get_group_name() |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3297 | |
| 3298 | self._assign_new_group(chosen_entries, group_name=group_name) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3299 | return chosen_entries |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3300 | |
| 3301 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3302 | def run_if_ready(self, queue_entry): |
| 3303 | """ |
showard | 8375ce0 | 2009-10-12 20:35:13 +0000 | [diff] [blame] | 3304 | Run this job by kicking its HQEs into status='Starting' if enough |
| 3305 | hosts are ready for it to run. |
| 3306 | |
| 3307 | Cleans up by kicking HQEs into status='Stopped' if this Job is not |
| 3308 | ready to run. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3309 | """ |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 3310 | if not self.is_ready(): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3311 | self.stop_if_necessary() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3312 | elif queue_entry.atomic_group: |
| 3313 | self.run_with_ready_delay(queue_entry) |
| 3314 | else: |
| 3315 | self.run(queue_entry) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3316 | |
| 3317 | |
| 3318 | def run_with_ready_delay(self, queue_entry): |
| 3319 | """ |
| 3320 | Start a delay to wait for more hosts to enter Pending state before |
| 3321 | launching an atomic group job. Once set, the a delay cannot be reset. |
| 3322 | |
| 3323 | @param queue_entry: The HostQueueEntry object to get atomic group |
| 3324 | info from and pass to run_if_ready when the delay is up. |
| 3325 | |
| 3326 | @returns An Agent to run the job as appropriate or None if a delay |
| 3327 | has already been set. |
| 3328 | """ |
| 3329 | assert queue_entry.job_id == self.id |
| 3330 | assert queue_entry.atomic_group |
| 3331 | delay = scheduler_config.config.secs_to_wait_for_atomic_group_hosts |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 3332 | over_max_threshold = (self._pending_count() >= |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame^] | 3333 | self._max_hosts_needed_to_run(queue_entry.atomic_group)) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3334 | delay_expired = (self._delay_ready_task and |
| 3335 | time.time() >= self._delay_ready_task.end_time) |
| 3336 | |
| 3337 | # Delay is disabled or we already have enough? Do not wait to run. |
| 3338 | if not delay or over_max_threshold or delay_expired: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3339 | self.run(queue_entry) |
| 3340 | else: |
| 3341 | queue_entry.set_status(models.HostQueueEntry.Status.WAITING) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3342 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3343 | |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame^] | 3344 | def request_abort(self): |
| 3345 | """Request that this Job be aborted on the next scheduler cycle.""" |
| 3346 | queue_entries = HostQueueEntry.fetch(where="job_id=%s" % self.id) |
| 3347 | for hqe in queue_entries: |
| 3348 | hqe.update_field('aborted', True) |
| 3349 | |
| 3350 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3351 | def schedule_delayed_callback_task(self, queue_entry): |
| 3352 | queue_entry.set_status(models.HostQueueEntry.Status.PENDING) |
| 3353 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3354 | if self._delay_ready_task: |
| 3355 | return None |
| 3356 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3357 | delay = scheduler_config.config.secs_to_wait_for_atomic_group_hosts |
| 3358 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3359 | def run_job_after_delay(): |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 3360 | logging.info('Job %s done waiting for extra hosts.', self) |
| 3361 | # Check to see if the job is still relevant. It could have aborted |
| 3362 | # while we were waiting or hosts could have disappearred, etc. |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame^] | 3363 | if self._pending_count() < self._min_hosts_needed_to_run(): |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 3364 | logging.info('Job %s had too few Pending hosts after waiting ' |
| 3365 | 'for extras. Not running.', self) |
showard | d07a5f3 | 2009-12-07 19:36:20 +0000 | [diff] [blame^] | 3366 | self.request_abort() |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 3367 | return |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3368 | return self.run(queue_entry) |
| 3369 | |
showard | 708b352 | 2009-08-20 23:26:15 +0000 | [diff] [blame] | 3370 | logging.info('Job %s waiting up to %s seconds for more hosts.', |
| 3371 | self.id, delay) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3372 | self._delay_ready_task = DelayedCallTask(delay_seconds=delay, |
| 3373 | callback=run_job_after_delay) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3374 | return self._delay_ready_task |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3375 | |
| 3376 | |
| 3377 | def run(self, queue_entry): |
| 3378 | """ |
| 3379 | @param queue_entry: The HostQueueEntry instance calling this method. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3380 | """ |
| 3381 | if queue_entry.atomic_group and self._atomic_and_has_started(): |
| 3382 | logging.error('Job.run() called on running atomic Job %d ' |
| 3383 | 'with HQE %s.', self.id, queue_entry) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3384 | return |
| 3385 | queue_entries = self._choose_group_to_run(queue_entry) |
showard | 828fc4c | 2009-09-14 20:31:00 +0000 | [diff] [blame] | 3386 | if queue_entries: |
| 3387 | self._finish_run(queue_entries) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 3388 | |
| 3389 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3390 | def _finish_run(self, queue_entries): |
showard | b2ccdda | 2008-10-28 20:39:05 +0000 | [diff] [blame] | 3391 | for queue_entry in queue_entries: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 3392 | queue_entry.set_status(models.HostQueueEntry.Status.STARTING) |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 3393 | self.abort_delay_ready_task() |
| 3394 | |
| 3395 | |
| 3396 | def abort_delay_ready_task(self): |
| 3397 | """Abort the delayed task associated with this job, if any.""" |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3398 | if self._delay_ready_task: |
| 3399 | # Cancel any pending callback that would try to run again |
| 3400 | # as we are already running. |
| 3401 | self._delay_ready_task.abort() |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 3402 | |
showard | d201482 | 2009-10-12 20:26:58 +0000 | [diff] [blame] | 3403 | |
showard | b000a8d | 2009-07-28 20:02:07 +0000 | [diff] [blame] | 3404 | def __str__(self): |
| 3405 | return '%s-%s' % (self.id, self.owner) |
| 3406 | |
| 3407 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3408 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3409 | main() |