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 | 402934a | 2009-12-21 22:20:47 +0000 | [diff] [blame] | 8 | import common |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 9 | import datetime, errno, optparse, os, pwd, Queue, re, shutil, signal |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 10 | import smtplib, socket, stat, subprocess, sys, tempfile, time, traceback |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 11 | import itertools, logging, weakref, gc |
showard | 402934a | 2009-12-21 22:20:47 +0000 | [diff] [blame] | 12 | |
mbligh | 8bcd23a | 2009-02-03 19:14:06 +0000 | [diff] [blame] | 13 | import MySQLdb |
showard | 402934a | 2009-12-21 22:20:47 +0000 | [diff] [blame] | 14 | |
showard | 043c62a | 2009-06-10 19:48:57 +0000 | [diff] [blame] | 15 | from autotest_lib.scheduler import scheduler_logging_config |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 16 | from autotest_lib.frontend import setup_django_environment |
showard | 402934a | 2009-12-21 22:20:47 +0000 | [diff] [blame] | 17 | |
| 18 | import django.db |
| 19 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 20 | from autotest_lib.client.common_lib import global_config, logging_manager |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 21 | from autotest_lib.client.common_lib import host_protections, utils |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 22 | from autotest_lib.database import database_connection |
showard | 844960a | 2009-05-29 18:41:18 +0000 | [diff] [blame] | 23 | from autotest_lib.frontend.afe import models, rpc_utils, readonly_connection |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 24 | from autotest_lib.frontend.afe import model_attributes |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 25 | from autotest_lib.scheduler import drone_manager, drones, email_manager |
showard | 043c62a | 2009-06-10 19:48:57 +0000 | [diff] [blame] | 26 | from autotest_lib.scheduler import monitor_db_cleanup |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 27 | from autotest_lib.scheduler import status_server, scheduler_config |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 28 | from autotest_lib.scheduler import gc_stats, metahost_scheduler |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 29 | from autotest_lib.scheduler import scheduler_models |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 30 | BABYSITTER_PID_FILE_PREFIX = 'monitor_db_babysitter' |
| 31 | PID_FILE_PREFIX = 'monitor_db' |
mbligh | b090f14 | 2008-02-27 21:33:46 +0000 | [diff] [blame] | 32 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 33 | RESULTS_DIR = '.' |
| 34 | AUTOSERV_NICE_LEVEL = 10 |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 35 | DB_CONFIG_SECTION = 'AUTOTEST_WEB' |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 36 | AUTOTEST_PATH = os.path.join(os.path.dirname(__file__), '..') |
| 37 | |
| 38 | if os.environ.has_key('AUTOTEST_DIR'): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 39 | AUTOTEST_PATH = os.environ['AUTOTEST_DIR'] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 40 | AUTOTEST_SERVER_DIR = os.path.join(AUTOTEST_PATH, 'server') |
| 41 | AUTOTEST_TKO_DIR = os.path.join(AUTOTEST_PATH, 'tko') |
| 42 | |
| 43 | if AUTOTEST_SERVER_DIR not in sys.path: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 44 | sys.path.insert(0, AUTOTEST_SERVER_DIR) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 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 |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 58 | _drone_manager = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 59 | |
| 60 | |
showard | ec6a3b9 | 2009-09-25 20:29:13 +0000 | [diff] [blame] | 61 | def _get_pidfile_timeout_secs(): |
| 62 | """@returns How long to wait for autoserv to write pidfile.""" |
| 63 | pidfile_timeout_mins = global_config.global_config.get_config_value( |
| 64 | scheduler_config.CONFIG_SECTION, 'pidfile_timeout_mins', type=int) |
| 65 | return pidfile_timeout_mins * 60 |
| 66 | |
| 67 | |
mbligh | 83c1e9e | 2009-05-01 23:10:41 +0000 | [diff] [blame] | 68 | def _site_init_monitor_db_dummy(): |
| 69 | return {} |
| 70 | |
| 71 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 72 | get_site_metahost_schedulers = utils.import_site_function( |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 73 | __file__, 'autotest_lib.scheduler.site_metahost_scheduler', |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 74 | 'get_metahost_schedulers', lambda : ()) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 75 | |
| 76 | |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame^] | 77 | def _verify_default_drone_set_exists(): |
| 78 | if (models.DroneSet.drone_sets_enabled() and |
| 79 | not models.DroneSet.default_drone_set_name()): |
| 80 | raise SchedulerError('Drone sets are enabled, but no default is set') |
| 81 | |
| 82 | |
| 83 | def _sanity_check(): |
| 84 | """Make sure the configs are consistent before starting the scheduler""" |
| 85 | _verify_default_drone_set_exists() |
| 86 | |
| 87 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 88 | def main(): |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 89 | try: |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 90 | try: |
| 91 | main_without_exception_handling() |
| 92 | except SystemExit: |
| 93 | raise |
| 94 | except: |
| 95 | logging.exception('Exception escaping in monitor_db') |
| 96 | raise |
| 97 | finally: |
| 98 | utils.delete_pid_file_if_exists(PID_FILE_PREFIX) |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | def main_without_exception_handling(): |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 102 | setup_logging() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 103 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 104 | usage = 'usage: %prog [options] results_dir' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 105 | parser = optparse.OptionParser(usage) |
| 106 | parser.add_option('--recover-hosts', help='Try to recover dead hosts', |
| 107 | action='store_true') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 108 | parser.add_option('--test', help='Indicate that scheduler is under ' + |
| 109 | 'test and should use dummy autoserv and no parsing', |
| 110 | action='store_true') |
| 111 | (options, args) = parser.parse_args() |
| 112 | if len(args) != 1: |
| 113 | parser.print_usage() |
| 114 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 115 | |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 116 | scheduler_enabled = global_config.global_config.get_config_value( |
| 117 | scheduler_config.CONFIG_SECTION, 'enable_scheduler', type=bool) |
| 118 | |
| 119 | if not scheduler_enabled: |
| 120 | msg = ("Scheduler not enabled, set enable_scheduler to true in the " |
| 121 | "global_config's SCHEDULER section to enabled it. Exiting.") |
mbligh | 6fbdb80 | 2009-08-03 16:42:55 +0000 | [diff] [blame] | 122 | logging.error(msg) |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 123 | sys.exit(1) |
| 124 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 125 | global RESULTS_DIR |
| 126 | RESULTS_DIR = args[0] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 127 | |
mbligh | 83c1e9e | 2009-05-01 23:10:41 +0000 | [diff] [blame] | 128 | site_init = utils.import_site_function(__file__, |
| 129 | "autotest_lib.scheduler.site_monitor_db", "site_init_monitor_db", |
| 130 | _site_init_monitor_db_dummy) |
| 131 | site_init() |
| 132 | |
showard | cca334f | 2009-03-12 20:38:34 +0000 | [diff] [blame] | 133 | # Change the cwd while running to avoid issues incase we were launched from |
| 134 | # somewhere odd (such as a random NFS home directory of the person running |
| 135 | # sudo to launch us as the appropriate user). |
| 136 | os.chdir(RESULTS_DIR) |
| 137 | |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 138 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 139 | if options.test: |
| 140 | global _autoserv_path |
| 141 | _autoserv_path = 'autoserv_dummy' |
| 142 | global _testing_mode |
| 143 | _testing_mode = True |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 144 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 145 | server = status_server.StatusServer() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 146 | server.start() |
| 147 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 148 | try: |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 149 | initialize() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 150 | dispatcher = Dispatcher() |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 151 | dispatcher.initialize(recover_hosts=options.recover_hosts) |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 152 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 153 | while not _shutdown: |
| 154 | dispatcher.tick() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 155 | time.sleep(scheduler_config.config.tick_pause_sec) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 156 | except: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 157 | email_manager.manager.log_stacktrace( |
| 158 | "Uncaught exception; terminating monitor_db") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 159 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 160 | email_manager.manager.send_queued_emails() |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 161 | server.shutdown() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 162 | _drone_manager.shutdown() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 163 | _db.disconnect() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 164 | |
| 165 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 166 | def setup_logging(): |
| 167 | log_dir = os.environ.get('AUTOTEST_SCHEDULER_LOG_DIR', None) |
| 168 | log_name = os.environ.get('AUTOTEST_SCHEDULER_LOG_NAME', None) |
| 169 | logging_manager.configure_logging( |
| 170 | scheduler_logging_config.SchedulerLoggingConfig(), log_dir=log_dir, |
| 171 | logfile_name=log_name) |
| 172 | |
| 173 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 174 | def handle_sigint(signum, frame): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 175 | global _shutdown |
| 176 | _shutdown = True |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 177 | logging.info("Shutdown request received.") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 178 | |
| 179 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 180 | def initialize(): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 181 | logging.info("%s> dispatcher starting", time.strftime("%X %x")) |
| 182 | logging.info("My PID is %d", os.getpid()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 183 | |
showard | 8de3713 | 2009-08-31 18:33:08 +0000 | [diff] [blame] | 184 | if utils.program_is_alive(PID_FILE_PREFIX): |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 185 | logging.critical("monitor_db already running, aborting!") |
| 186 | sys.exit(1) |
| 187 | utils.write_pid(PID_FILE_PREFIX) |
mbligh | fb67603 | 2009-04-01 18:25:38 +0000 | [diff] [blame] | 188 | |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 189 | if _testing_mode: |
| 190 | global_config.global_config.override_config_value( |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 191 | DB_CONFIG_SECTION, 'database', 'stresstest_autotest_web') |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 192 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 193 | os.environ['PATH'] = AUTOTEST_SERVER_DIR + ':' + os.environ['PATH'] |
| 194 | global _db |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 195 | _db = database_connection.DatabaseConnection(DB_CONFIG_SECTION) |
showard | b21b8c8 | 2009-12-07 19:39:39 +0000 | [diff] [blame] | 196 | _db.connect(db_type='django') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 197 | |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 198 | # ensure Django connection is in autocommit |
| 199 | setup_django_environment.enable_autocommit() |
showard | 844960a | 2009-05-29 18:41:18 +0000 | [diff] [blame] | 200 | # bypass the readonly connection |
| 201 | readonly_connection.ReadOnlyConnection.set_globally_disabled(True) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 202 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 203 | logging.info("Setting signal handler") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 204 | signal.signal(signal.SIGINT, handle_sigint) |
| 205 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 206 | initialize_globals() |
| 207 | scheduler_models.initialize() |
| 208 | |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 209 | drones = global_config.global_config.get_config_value( |
| 210 | scheduler_config.CONFIG_SECTION, 'drones', default='localhost') |
| 211 | drone_list = [hostname.strip() for hostname in drones.split(',')] |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 212 | results_host = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 213 | scheduler_config.CONFIG_SECTION, 'results_host', default='localhost') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 214 | _drone_manager.initialize(RESULTS_DIR, drone_list, results_host) |
| 215 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 216 | logging.info("Connected! Running...") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 217 | |
| 218 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 219 | def initialize_globals(): |
| 220 | global _drone_manager |
| 221 | _drone_manager = drone_manager.instance() |
| 222 | |
| 223 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 224 | def _autoserv_command_line(machines, extra_args, job=None, queue_entry=None, |
| 225 | verbose=True): |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 226 | """ |
| 227 | @returns The autoserv command line as a list of executable + parameters. |
| 228 | |
| 229 | @param machines - string - A machine or comma separated list of machines |
| 230 | for the (-m) flag. |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 231 | @param extra_args - list - Additional arguments to pass to autoserv. |
| 232 | @param job - Job object - If supplied, -u owner and -l name parameters |
| 233 | will be added. |
| 234 | @param queue_entry - A HostQueueEntry object - If supplied and no Job |
| 235 | object was supplied, this will be used to lookup the Job object. |
| 236 | """ |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 237 | autoserv_argv = [_autoserv_path, '-p', |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 238 | '-r', drone_manager.WORKING_DIRECTORY] |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 239 | if machines: |
| 240 | autoserv_argv += ['-m', machines] |
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 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 254 | class HostScheduler(metahost_scheduler.HostSchedulingUtility): |
| 255 | """Handles the logic for choosing when to run jobs and on which hosts. |
| 256 | |
| 257 | This class makes several queries to the database on each tick, building up |
| 258 | some auxiliary data structures and using them to determine which hosts are |
| 259 | eligible to run which jobs, taking into account all the various factors that |
| 260 | affect that. |
| 261 | |
| 262 | In the past this was done with one or two very large, complex database |
| 263 | queries. It has proven much simpler and faster to build these auxiliary |
| 264 | data structures and perform the logic in Python. |
| 265 | """ |
| 266 | def __init__(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 267 | self._metahost_schedulers = metahost_scheduler.get_metahost_schedulers() |
| 268 | |
| 269 | # load site-specific scheduler selected in global_config |
| 270 | site_schedulers_str = global_config.global_config.get_config_value( |
| 271 | scheduler_config.CONFIG_SECTION, 'site_metahost_schedulers', |
| 272 | default='') |
| 273 | site_schedulers = set(site_schedulers_str.split(',')) |
| 274 | for scheduler in get_site_metahost_schedulers(): |
| 275 | if type(scheduler).__name__ in site_schedulers: |
| 276 | # always prepend, so site schedulers take precedence |
| 277 | self._metahost_schedulers = ( |
| 278 | [scheduler] + self._metahost_schedulers) |
| 279 | logging.info('Metahost schedulers: %s', |
| 280 | ', '.join(type(scheduler).__name__ for scheduler |
| 281 | in self._metahost_schedulers)) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 282 | |
| 283 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 284 | def _get_ready_hosts(self): |
| 285 | # avoid any host with a currently active queue entry against it |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 286 | hosts = scheduler_models.Host.fetch( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 287 | joins='LEFT JOIN afe_host_queue_entries AS active_hqe ' |
| 288 | 'ON (afe_hosts.id = active_hqe.host_id AND ' |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 289 | 'active_hqe.active)', |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 290 | where="active_hqe.host_id IS NULL " |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 291 | "AND NOT afe_hosts.locked " |
| 292 | "AND (afe_hosts.status IS NULL " |
| 293 | "OR afe_hosts.status = 'Ready')") |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 294 | return dict((host.id, host) for host in hosts) |
| 295 | |
| 296 | |
| 297 | @staticmethod |
| 298 | def _get_sql_id_list(id_list): |
| 299 | return ','.join(str(item_id) for item_id in id_list) |
| 300 | |
| 301 | |
| 302 | @classmethod |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 303 | def _get_many2many_dict(cls, query, id_list, flip=False): |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 304 | if not id_list: |
| 305 | return {} |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 306 | query %= cls._get_sql_id_list(id_list) |
| 307 | rows = _db.execute(query) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 308 | return cls._process_many2many_dict(rows, flip) |
| 309 | |
| 310 | |
| 311 | @staticmethod |
| 312 | def _process_many2many_dict(rows, flip=False): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 313 | result = {} |
| 314 | for row in rows: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 315 | left_id, right_id = int(row[0]), int(row[1]) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 316 | if flip: |
| 317 | left_id, right_id = right_id, left_id |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 318 | result.setdefault(left_id, set()).add(right_id) |
| 319 | return result |
| 320 | |
| 321 | |
| 322 | @classmethod |
| 323 | def _get_job_acl_groups(cls, job_ids): |
| 324 | query = """ |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 325 | SELECT afe_jobs.id, afe_acl_groups_users.aclgroup_id |
| 326 | FROM afe_jobs |
| 327 | INNER JOIN afe_users ON afe_users.login = afe_jobs.owner |
| 328 | INNER JOIN afe_acl_groups_users ON |
| 329 | afe_acl_groups_users.user_id = afe_users.id |
| 330 | WHERE afe_jobs.id IN (%s) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 331 | """ |
| 332 | return cls._get_many2many_dict(query, job_ids) |
| 333 | |
| 334 | |
| 335 | @classmethod |
| 336 | def _get_job_ineligible_hosts(cls, job_ids): |
| 337 | query = """ |
| 338 | SELECT job_id, host_id |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 339 | FROM afe_ineligible_host_queues |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 340 | WHERE job_id IN (%s) |
| 341 | """ |
| 342 | return cls._get_many2many_dict(query, job_ids) |
| 343 | |
| 344 | |
| 345 | @classmethod |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 346 | def _get_job_dependencies(cls, job_ids): |
| 347 | query = """ |
| 348 | SELECT job_id, label_id |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 349 | FROM afe_jobs_dependency_labels |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 350 | WHERE job_id IN (%s) |
| 351 | """ |
| 352 | return cls._get_many2many_dict(query, job_ids) |
| 353 | |
| 354 | |
| 355 | @classmethod |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 356 | def _get_host_acls(cls, host_ids): |
| 357 | query = """ |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 358 | SELECT host_id, aclgroup_id |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 359 | FROM afe_acl_groups_hosts |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 360 | WHERE host_id IN (%s) |
| 361 | """ |
| 362 | return cls._get_many2many_dict(query, host_ids) |
| 363 | |
| 364 | |
| 365 | @classmethod |
| 366 | def _get_label_hosts(cls, host_ids): |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 367 | if not host_ids: |
| 368 | return {}, {} |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 369 | query = """ |
| 370 | SELECT label_id, host_id |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 371 | FROM afe_hosts_labels |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 372 | WHERE host_id IN (%s) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 373 | """ % cls._get_sql_id_list(host_ids) |
| 374 | rows = _db.execute(query) |
| 375 | labels_to_hosts = cls._process_many2many_dict(rows) |
| 376 | hosts_to_labels = cls._process_many2many_dict(rows, flip=True) |
| 377 | return labels_to_hosts, hosts_to_labels |
| 378 | |
| 379 | |
| 380 | @classmethod |
| 381 | def _get_labels(cls): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 382 | return dict((label.id, label) for label |
| 383 | in scheduler_models.Label.fetch()) |
| 384 | |
| 385 | |
| 386 | def recovery_on_startup(self): |
| 387 | for metahost_scheduler in self._metahost_schedulers: |
| 388 | metahost_scheduler.recovery_on_startup() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 389 | |
| 390 | |
| 391 | def refresh(self, pending_queue_entries): |
| 392 | self._hosts_available = self._get_ready_hosts() |
| 393 | |
| 394 | relevant_jobs = [queue_entry.job_id |
| 395 | for queue_entry in pending_queue_entries] |
| 396 | self._job_acls = self._get_job_acl_groups(relevant_jobs) |
| 397 | self._ineligible_hosts = self._get_job_ineligible_hosts(relevant_jobs) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 398 | self._job_dependencies = self._get_job_dependencies(relevant_jobs) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 399 | |
| 400 | host_ids = self._hosts_available.keys() |
| 401 | self._host_acls = self._get_host_acls(host_ids) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 402 | self._label_hosts, self._host_labels = self._get_label_hosts(host_ids) |
| 403 | |
| 404 | self._labels = self._get_labels() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 405 | |
jamesren | e21bf41 | 2010-02-26 02:30:07 +0000 | [diff] [blame] | 406 | |
| 407 | def tick(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 408 | for metahost_scheduler in self._metahost_schedulers: |
| 409 | metahost_scheduler.tick() |
| 410 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 411 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 412 | def hosts_in_label(self, label_id): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 413 | return set(self._label_hosts.get(label_id, ())) |
| 414 | |
| 415 | |
| 416 | def remove_host_from_label(self, host_id, label_id): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 417 | self._label_hosts[label_id].remove(host_id) |
| 418 | |
| 419 | |
| 420 | def pop_host(self, host_id): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 421 | return self._hosts_available.pop(host_id) |
| 422 | |
| 423 | |
| 424 | def ineligible_hosts_for_entry(self, queue_entry): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 425 | return set(self._ineligible_hosts.get(queue_entry.job_id, ())) |
| 426 | |
| 427 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 428 | def _is_acl_accessible(self, host_id, queue_entry): |
| 429 | job_acls = self._job_acls.get(queue_entry.job_id, set()) |
| 430 | host_acls = self._host_acls.get(host_id, set()) |
| 431 | return len(host_acls.intersection(job_acls)) > 0 |
| 432 | |
| 433 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 434 | def _check_job_dependencies(self, job_dependencies, host_labels): |
| 435 | missing = job_dependencies - host_labels |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 436 | return len(missing) == 0 |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 437 | |
| 438 | |
| 439 | def _check_only_if_needed_labels(self, job_dependencies, host_labels, |
| 440 | queue_entry): |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 441 | if not queue_entry.meta_host: |
| 442 | # bypass only_if_needed labels when a specific host is selected |
| 443 | return True |
| 444 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 445 | for label_id in host_labels: |
| 446 | label = self._labels[label_id] |
| 447 | if not label.only_if_needed: |
| 448 | # we don't care about non-only_if_needed labels |
| 449 | continue |
| 450 | if queue_entry.meta_host == label_id: |
| 451 | # if the label was requested in a metahost it's OK |
| 452 | continue |
| 453 | if label_id not in job_dependencies: |
| 454 | return False |
| 455 | return True |
| 456 | |
| 457 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 458 | def _check_atomic_group_labels(self, host_labels, queue_entry): |
| 459 | """ |
| 460 | Determine if the given HostQueueEntry's atomic group settings are okay |
| 461 | to schedule on a host with the given labels. |
| 462 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 463 | @param host_labels: A list of label ids that the host has. |
| 464 | @param queue_entry: The HostQueueEntry being considered for the host. |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 465 | |
| 466 | @returns True if atomic group settings are okay, False otherwise. |
| 467 | """ |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 468 | return (self._get_host_atomic_group_id(host_labels, queue_entry) == |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 469 | queue_entry.atomic_group_id) |
| 470 | |
| 471 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 472 | def _get_host_atomic_group_id(self, host_labels, queue_entry=None): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 473 | """ |
| 474 | Return the atomic group label id for a host with the given set of |
| 475 | labels if any, or None otherwise. Raises an exception if more than |
| 476 | one atomic group are found in the set of labels. |
| 477 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 478 | @param host_labels: A list of label ids that the host has. |
| 479 | @param queue_entry: The HostQueueEntry we're testing. Only used for |
| 480 | extra info in a potential logged error message. |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 481 | |
| 482 | @returns The id of the atomic group found on a label in host_labels |
| 483 | or None if no atomic group label is found. |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 484 | """ |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 485 | atomic_labels = [self._labels[label_id] for label_id in host_labels |
| 486 | if self._labels[label_id].atomic_group_id is not None] |
| 487 | atomic_ids = set(label.atomic_group_id for label in atomic_labels) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 488 | if not atomic_ids: |
| 489 | return None |
| 490 | if len(atomic_ids) > 1: |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 491 | logging.error('More than one Atomic Group on HQE "%s" via: %r', |
| 492 | queue_entry, atomic_labels) |
| 493 | return atomic_ids.pop() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 494 | |
| 495 | |
| 496 | def _get_atomic_group_labels(self, atomic_group_id): |
| 497 | """ |
| 498 | Lookup the label ids that an atomic_group is associated with. |
| 499 | |
| 500 | @param atomic_group_id - The id of the AtomicGroup to look up. |
| 501 | |
| 502 | @returns A generator yeilding Label ids for this atomic group. |
| 503 | """ |
| 504 | return (id for id, label in self._labels.iteritems() |
| 505 | if label.atomic_group_id == atomic_group_id |
| 506 | and not label.invalid) |
| 507 | |
| 508 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 509 | def _get_eligible_host_ids_in_group(self, group_hosts, queue_entry): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 510 | """ |
| 511 | @param group_hosts - A sequence of Host ids to test for usability |
| 512 | and eligibility against the Job associated with queue_entry. |
| 513 | @param queue_entry - The HostQueueEntry that these hosts are being |
| 514 | tested for eligibility against. |
| 515 | |
| 516 | @returns A subset of group_hosts Host ids that are eligible for the |
| 517 | supplied queue_entry. |
| 518 | """ |
| 519 | return set(host_id for host_id in group_hosts |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 520 | if self.is_host_usable(host_id) |
| 521 | and self.is_host_eligible_for_job(host_id, queue_entry)) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 522 | |
| 523 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 524 | def is_host_eligible_for_job(self, host_id, queue_entry): |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 525 | if self._is_host_invalid(host_id): |
| 526 | # if an invalid host is scheduled for a job, it's a one-time host |
| 527 | # and it therefore bypasses eligibility checks. note this can only |
| 528 | # happen for non-metahosts, because invalid hosts have their label |
| 529 | # relationships cleared. |
| 530 | return True |
| 531 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 532 | job_dependencies = self._job_dependencies.get(queue_entry.job_id, set()) |
| 533 | host_labels = self._host_labels.get(host_id, set()) |
mbligh | c993bee | 2008-10-03 03:42:34 +0000 | [diff] [blame] | 534 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 535 | return (self._is_acl_accessible(host_id, queue_entry) and |
| 536 | self._check_job_dependencies(job_dependencies, host_labels) and |
| 537 | self._check_only_if_needed_labels( |
| 538 | job_dependencies, host_labels, queue_entry) and |
| 539 | self._check_atomic_group_labels(host_labels, queue_entry)) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 540 | |
| 541 | |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 542 | def _is_host_invalid(self, host_id): |
| 543 | host_object = self._hosts_available.get(host_id, None) |
| 544 | return host_object and host_object.invalid |
| 545 | |
| 546 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 547 | def _schedule_non_metahost(self, queue_entry): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 548 | 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] | 549 | return None |
| 550 | return self._hosts_available.pop(queue_entry.host_id, None) |
| 551 | |
| 552 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 553 | def is_host_usable(self, host_id): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 554 | if host_id not in self._hosts_available: |
| 555 | # host was already used during this scheduling cycle |
| 556 | return False |
| 557 | if self._hosts_available[host_id].invalid: |
| 558 | # Invalid hosts cannot be used for metahosts. They're included in |
| 559 | # the original query because they can be used by non-metahosts. |
| 560 | return False |
| 561 | return True |
| 562 | |
| 563 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 564 | def schedule_entry(self, queue_entry): |
| 565 | if queue_entry.host_id is not None: |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 566 | return self._schedule_non_metahost(queue_entry) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 567 | |
| 568 | for scheduler in self._metahost_schedulers: |
| 569 | if scheduler.can_schedule_metahost(queue_entry): |
| 570 | scheduler.schedule_metahost(queue_entry, self) |
| 571 | return None |
| 572 | |
| 573 | raise SchedulerError('No metahost scheduler to handle %s' % queue_entry) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 574 | |
| 575 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 576 | def find_eligible_atomic_group(self, queue_entry): |
| 577 | """ |
| 578 | Given an atomic group host queue entry, locate an appropriate group |
| 579 | of hosts for the associated job to run on. |
| 580 | |
| 581 | The caller is responsible for creating new HQEs for the additional |
| 582 | hosts returned in order to run the actual job on them. |
| 583 | |
| 584 | @returns A list of Host instances in a ready state to satisfy this |
| 585 | atomic group scheduling. Hosts will all belong to the same |
| 586 | atomic group label as specified by the queue_entry. |
| 587 | An empty list will be returned if no suitable atomic |
| 588 | group could be found. |
| 589 | |
| 590 | TODO(gps): what is responsible for kicking off any attempted repairs on |
| 591 | a group of hosts? not this function, but something needs to. We do |
| 592 | not communicate that reason for returning [] outside of here... |
| 593 | For now, we'll just be unschedulable if enough hosts within one group |
| 594 | enter Repair Failed state. |
| 595 | """ |
| 596 | assert queue_entry.atomic_group_id is not None |
| 597 | job = queue_entry.job |
| 598 | assert job.synch_count and job.synch_count > 0 |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 599 | atomic_group = queue_entry.atomic_group |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 600 | if job.synch_count > atomic_group.max_number_of_machines: |
| 601 | # Such a Job and HostQueueEntry should never be possible to |
| 602 | # create using the frontend. Regardless, we can't process it. |
| 603 | # Abort it immediately and log an error on the scheduler. |
| 604 | queue_entry.set_status(models.HostQueueEntry.Status.ABORTED) |
showard | 7629f14 | 2009-03-27 21:02:02 +0000 | [diff] [blame] | 605 | logging.error( |
| 606 | 'Error: job %d synch_count=%d > requested atomic_group %d ' |
| 607 | 'max_number_of_machines=%d. Aborted host_queue_entry %d.', |
| 608 | job.id, job.synch_count, atomic_group.id, |
| 609 | atomic_group.max_number_of_machines, queue_entry.id) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 610 | return [] |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 611 | hosts_in_label = self.hosts_in_label(queue_entry.meta_host) |
| 612 | ineligible_host_ids = self.ineligible_hosts_for_entry(queue_entry) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 613 | |
| 614 | # Look in each label associated with atomic_group until we find one with |
| 615 | # enough hosts to satisfy the job. |
| 616 | for atomic_label_id in self._get_atomic_group_labels(atomic_group.id): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 617 | group_hosts = set(self.hosts_in_label(atomic_label_id)) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 618 | if queue_entry.meta_host is not None: |
| 619 | # If we have a metahost label, only allow its hosts. |
| 620 | group_hosts.intersection_update(hosts_in_label) |
| 621 | group_hosts -= ineligible_host_ids |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 622 | eligible_host_ids_in_group = self._get_eligible_host_ids_in_group( |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 623 | group_hosts, queue_entry) |
| 624 | |
| 625 | # Job.synch_count is treated as "minimum synch count" when |
| 626 | # scheduling for an atomic group of hosts. The atomic group |
| 627 | # number of machines is the maximum to pick out of a single |
| 628 | # atomic group label for scheduling at one time. |
| 629 | min_hosts = job.synch_count |
| 630 | max_hosts = atomic_group.max_number_of_machines |
| 631 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 632 | if len(eligible_host_ids_in_group) < min_hosts: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 633 | # Not enough eligible hosts in this atomic group label. |
| 634 | continue |
| 635 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 636 | eligible_hosts_in_group = [self._hosts_available[id] |
| 637 | for id in eligible_host_ids_in_group] |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 638 | # So that they show up in a sane order when viewing the job. |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 639 | eligible_hosts_in_group.sort(cmp=scheduler_models.Host.cmp_for_sort) |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 640 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 641 | # Limit ourselves to scheduling the atomic group size. |
| 642 | if len(eligible_hosts_in_group) > max_hosts: |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 643 | eligible_hosts_in_group = eligible_hosts_in_group[:max_hosts] |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 644 | |
| 645 | # Remove the selected hosts from our cached internal state |
| 646 | # of available hosts in order to return the Host objects. |
| 647 | host_list = [] |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 648 | for host in eligible_hosts_in_group: |
| 649 | hosts_in_label.discard(host.id) |
| 650 | self._hosts_available.pop(host.id) |
| 651 | host_list.append(host) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 652 | return host_list |
| 653 | |
| 654 | return [] |
| 655 | |
| 656 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 657 | class Dispatcher(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 658 | def __init__(self): |
| 659 | self._agents = [] |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 660 | self._last_clean_time = time.time() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 661 | self._host_scheduler = HostScheduler() |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 662 | user_cleanup_time = scheduler_config.config.clean_interval |
| 663 | self._periodic_cleanup = monitor_db_cleanup.UserCleanup( |
| 664 | _db, user_cleanup_time) |
| 665 | self._24hr_upkeep = monitor_db_cleanup.TwentyFourHourUpkeep(_db) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 666 | self._host_agents = {} |
| 667 | self._queue_entry_agents = {} |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 668 | self._tick_count = 0 |
| 669 | self._last_garbage_stats_time = time.time() |
| 670 | self._seconds_between_garbage_stats = 60 * ( |
| 671 | global_config.global_config.get_config_value( |
| 672 | scheduler_config.CONFIG_SECTION, |
| 673 | 'gc_stats_interval_mins', type=int, default=6*60)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 674 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 675 | |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 676 | def initialize(self, recover_hosts=True): |
| 677 | self._periodic_cleanup.initialize() |
| 678 | self._24hr_upkeep.initialize() |
| 679 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 680 | # always recover processes |
| 681 | self._recover_processes() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 682 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 683 | if recover_hosts: |
| 684 | self._recover_hosts() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 685 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 686 | self._host_scheduler.recovery_on_startup() |
| 687 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 688 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 689 | def tick(self): |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 690 | self._garbage_collection() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 691 | _drone_manager.refresh() |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 692 | self._run_cleanup() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 693 | self._find_aborting() |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 694 | self._process_recurring_runs() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 695 | self._schedule_delay_tasks() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 696 | self._schedule_running_host_queue_entries() |
| 697 | self._schedule_special_tasks() |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 698 | self._schedule_new_jobs() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 699 | self._handle_agents() |
jamesren | e21bf41 | 2010-02-26 02:30:07 +0000 | [diff] [blame] | 700 | self._host_scheduler.tick() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 701 | _drone_manager.execute_actions() |
| 702 | email_manager.manager.send_queued_emails() |
showard | 402934a | 2009-12-21 22:20:47 +0000 | [diff] [blame] | 703 | django.db.reset_queries() |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 704 | self._tick_count += 1 |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 705 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 706 | |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 707 | def _run_cleanup(self): |
| 708 | self._periodic_cleanup.run_cleanup_maybe() |
| 709 | self._24hr_upkeep.run_cleanup_maybe() |
showard | a3ab0d5 | 2008-11-03 19:03:47 +0000 | [diff] [blame] | 710 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 711 | |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 712 | def _garbage_collection(self): |
| 713 | threshold_time = time.time() - self._seconds_between_garbage_stats |
| 714 | if threshold_time < self._last_garbage_stats_time: |
| 715 | # Don't generate these reports very often. |
| 716 | return |
| 717 | |
| 718 | self._last_garbage_stats_time = time.time() |
| 719 | # Force a full level 0 collection (because we can, it doesn't hurt |
| 720 | # at this interval). |
| 721 | gc.collect() |
| 722 | logging.info('Logging garbage collector stats on tick %d.', |
| 723 | self._tick_count) |
| 724 | gc_stats._log_garbage_collector_stats() |
| 725 | |
| 726 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 727 | def _register_agent_for_ids(self, agent_dict, object_ids, agent): |
| 728 | for object_id in object_ids: |
| 729 | agent_dict.setdefault(object_id, set()).add(agent) |
| 730 | |
| 731 | |
| 732 | def _unregister_agent_for_ids(self, agent_dict, object_ids, agent): |
| 733 | for object_id in object_ids: |
| 734 | assert object_id in agent_dict |
| 735 | agent_dict[object_id].remove(agent) |
| 736 | |
| 737 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 738 | def add_agent_task(self, agent_task): |
| 739 | agent = Agent(agent_task) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 740 | self._agents.append(agent) |
| 741 | agent.dispatcher = self |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 742 | self._register_agent_for_ids(self._host_agents, agent.host_ids, agent) |
| 743 | self._register_agent_for_ids(self._queue_entry_agents, |
| 744 | agent.queue_entry_ids, agent) |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 745 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 746 | |
| 747 | def get_agents_for_entry(self, queue_entry): |
| 748 | """ |
| 749 | Find agents corresponding to the specified queue_entry. |
| 750 | """ |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 751 | return list(self._queue_entry_agents.get(queue_entry.id, set())) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 752 | |
| 753 | |
| 754 | def host_has_agent(self, host): |
| 755 | """ |
| 756 | Determine if there is currently an Agent present using this host. |
| 757 | """ |
| 758 | return bool(self._host_agents.get(host.id, None)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 759 | |
| 760 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 761 | def remove_agent(self, agent): |
| 762 | self._agents.remove(agent) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 763 | self._unregister_agent_for_ids(self._host_agents, agent.host_ids, |
| 764 | agent) |
| 765 | self._unregister_agent_for_ids(self._queue_entry_agents, |
| 766 | agent.queue_entry_ids, agent) |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 767 | |
| 768 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 769 | def _host_has_scheduled_special_task(self, host): |
| 770 | return bool(models.SpecialTask.objects.filter(host__id=host.id, |
| 771 | is_active=False, |
| 772 | is_complete=False)) |
| 773 | |
| 774 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 775 | def _recover_processes(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 776 | agent_tasks = self._create_recovery_agent_tasks() |
| 777 | self._register_pidfiles(agent_tasks) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 778 | _drone_manager.refresh() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 779 | self._recover_tasks(agent_tasks) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 780 | self._recover_pending_entries() |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 781 | self._check_for_unrecovered_verifying_entries() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 782 | self._reverify_remaining_hosts() |
| 783 | # reinitialize drones after killing orphaned processes, since they can |
| 784 | # leave around files when they die |
| 785 | _drone_manager.execute_actions() |
| 786 | _drone_manager.reinitialize_drones() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 787 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 788 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 789 | def _create_recovery_agent_tasks(self): |
| 790 | return (self._get_queue_entry_agent_tasks() |
| 791 | + self._get_special_task_agent_tasks(is_active=True)) |
| 792 | |
| 793 | |
| 794 | def _get_queue_entry_agent_tasks(self): |
| 795 | # host queue entry statuses handled directly by AgentTasks (Verifying is |
| 796 | # handled through SpecialTasks, so is not listed here) |
| 797 | statuses = (models.HostQueueEntry.Status.STARTING, |
| 798 | models.HostQueueEntry.Status.RUNNING, |
| 799 | models.HostQueueEntry.Status.GATHERING, |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 800 | models.HostQueueEntry.Status.PARSING, |
| 801 | models.HostQueueEntry.Status.ARCHIVING) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 802 | status_list = ','.join("'%s'" % status for status in statuses) |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 803 | queue_entries = scheduler_models.HostQueueEntry.fetch( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 804 | where='status IN (%s)' % status_list) |
| 805 | |
| 806 | agent_tasks = [] |
| 807 | used_queue_entries = set() |
| 808 | for entry in queue_entries: |
| 809 | if self.get_agents_for_entry(entry): |
| 810 | # already being handled |
| 811 | continue |
| 812 | if entry in used_queue_entries: |
| 813 | # already picked up by a synchronous job |
| 814 | continue |
| 815 | agent_task = self._get_agent_task_for_queue_entry(entry) |
| 816 | agent_tasks.append(agent_task) |
| 817 | used_queue_entries.update(agent_task.queue_entries) |
| 818 | return agent_tasks |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 819 | |
| 820 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 821 | def _get_special_task_agent_tasks(self, is_active=False): |
| 822 | special_tasks = models.SpecialTask.objects.filter( |
| 823 | is_active=is_active, is_complete=False) |
| 824 | return [self._get_agent_task_for_special_task(task) |
| 825 | for task in special_tasks] |
| 826 | |
| 827 | |
| 828 | def _get_agent_task_for_queue_entry(self, queue_entry): |
| 829 | """ |
| 830 | Construct an AgentTask instance for the given active HostQueueEntry, |
| 831 | if one can currently run it. |
| 832 | @param queue_entry: a HostQueueEntry |
| 833 | @returns an AgentTask to run the queue entry |
| 834 | """ |
| 835 | task_entries = queue_entry.job.get_group_entries(queue_entry) |
| 836 | self._check_for_duplicate_host_entries(task_entries) |
| 837 | |
| 838 | if queue_entry.status in (models.HostQueueEntry.Status.STARTING, |
| 839 | models.HostQueueEntry.Status.RUNNING): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 840 | if queue_entry.is_hostless(): |
| 841 | return HostlessQueueTask(queue_entry=queue_entry) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 842 | return QueueTask(queue_entries=task_entries) |
| 843 | if queue_entry.status == models.HostQueueEntry.Status.GATHERING: |
| 844 | return GatherLogsTask(queue_entries=task_entries) |
| 845 | if queue_entry.status == models.HostQueueEntry.Status.PARSING: |
| 846 | return FinalReparseTask(queue_entries=task_entries) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 847 | if queue_entry.status == models.HostQueueEntry.Status.ARCHIVING: |
| 848 | return ArchiveResultsTask(queue_entries=task_entries) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 849 | |
| 850 | raise SchedulerError('_get_agent_task_for_queue_entry got entry with ' |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 851 | 'invalid status %s: %s' |
| 852 | % (queue_entry.status, queue_entry)) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 853 | |
| 854 | |
| 855 | def _check_for_duplicate_host_entries(self, task_entries): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 856 | non_host_statuses = (models.HostQueueEntry.Status.PARSING, |
| 857 | models.HostQueueEntry.Status.ARCHIVING) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 858 | for task_entry in task_entries: |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 859 | using_host = (task_entry.host is not None |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 860 | and task_entry.status not in non_host_statuses) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 861 | if using_host: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 862 | self._assert_host_has_no_agent(task_entry) |
| 863 | |
| 864 | |
| 865 | def _assert_host_has_no_agent(self, entry): |
| 866 | """ |
| 867 | @param entry: a HostQueueEntry or a SpecialTask |
| 868 | """ |
| 869 | if self.host_has_agent(entry.host): |
| 870 | agent = tuple(self._host_agents.get(entry.host.id))[0] |
| 871 | raise SchedulerError( |
| 872 | 'While scheduling %s, host %s already has a host agent %s' |
| 873 | % (entry, entry.host, agent.task)) |
| 874 | |
| 875 | |
| 876 | def _get_agent_task_for_special_task(self, special_task): |
| 877 | """ |
| 878 | Construct an AgentTask class to run the given SpecialTask and add it |
| 879 | to this dispatcher. |
| 880 | @param special_task: a models.SpecialTask instance |
| 881 | @returns an AgentTask to run this SpecialTask |
| 882 | """ |
| 883 | self._assert_host_has_no_agent(special_task) |
| 884 | |
| 885 | special_agent_task_classes = (CleanupTask, VerifyTask, RepairTask) |
| 886 | for agent_task_class in special_agent_task_classes: |
| 887 | if agent_task_class.TASK_TYPE == special_task.task: |
| 888 | return agent_task_class(task=special_task) |
| 889 | |
| 890 | raise SchedulerError('No AgentTask class for task', str(special_task)) |
| 891 | |
| 892 | |
| 893 | def _register_pidfiles(self, agent_tasks): |
| 894 | for agent_task in agent_tasks: |
| 895 | agent_task.register_necessary_pidfiles() |
| 896 | |
| 897 | |
| 898 | def _recover_tasks(self, agent_tasks): |
| 899 | orphans = _drone_manager.get_orphaned_autoserv_processes() |
| 900 | |
| 901 | for agent_task in agent_tasks: |
| 902 | agent_task.recover() |
| 903 | if agent_task.monitor and agent_task.monitor.has_process(): |
| 904 | orphans.discard(agent_task.monitor.get_process()) |
| 905 | self.add_agent_task(agent_task) |
| 906 | |
| 907 | self._check_for_remaining_orphan_processes(orphans) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 908 | |
| 909 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 910 | def _get_unassigned_entries(self, status): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 911 | for entry in scheduler_models.HostQueueEntry.fetch(where="status = '%s'" |
| 912 | % status): |
showard | 0db3d43 | 2009-10-12 20:29:15 +0000 | [diff] [blame] | 913 | if entry.status == status and not self.get_agents_for_entry(entry): |
| 914 | # The status can change during iteration, e.g., if job.run() |
| 915 | # sets a group of queue entries to Starting |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 916 | yield entry |
| 917 | |
| 918 | |
showard | 6878e8b | 2009-07-20 22:37:45 +0000 | [diff] [blame] | 919 | def _check_for_remaining_orphan_processes(self, orphans): |
| 920 | if not orphans: |
| 921 | return |
| 922 | subject = 'Unrecovered orphan autoserv processes remain' |
| 923 | message = '\n'.join(str(process) for process in orphans) |
| 924 | email_manager.manager.enqueue_notify_email(subject, message) |
mbligh | 5fa9e11 | 2009-08-03 16:46:06 +0000 | [diff] [blame] | 925 | |
| 926 | die_on_orphans = global_config.global_config.get_config_value( |
| 927 | scheduler_config.CONFIG_SECTION, 'die_on_orphans', type=bool) |
| 928 | |
| 929 | if die_on_orphans: |
| 930 | raise RuntimeError(subject + '\n' + message) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 931 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 932 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 933 | def _recover_pending_entries(self): |
| 934 | for entry in self._get_unassigned_entries( |
| 935 | models.HostQueueEntry.Status.PENDING): |
showard | 5682407 | 2009-10-12 20:30:21 +0000 | [diff] [blame] | 936 | logging.info('Recovering Pending entry %s', entry) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 937 | entry.on_pending() |
| 938 | |
| 939 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 940 | def _check_for_unrecovered_verifying_entries(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 941 | queue_entries = scheduler_models.HostQueueEntry.fetch( |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 942 | where='status = "%s"' % models.HostQueueEntry.Status.VERIFYING) |
| 943 | unrecovered_hqes = [] |
| 944 | for queue_entry in queue_entries: |
| 945 | special_tasks = models.SpecialTask.objects.filter( |
| 946 | task__in=(models.SpecialTask.Task.CLEANUP, |
| 947 | models.SpecialTask.Task.VERIFY), |
| 948 | queue_entry__id=queue_entry.id, |
| 949 | is_complete=False) |
| 950 | if special_tasks.count() == 0: |
| 951 | unrecovered_hqes.append(queue_entry) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 952 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 953 | if unrecovered_hqes: |
| 954 | message = '\n'.join(str(hqe) for hqe in unrecovered_hqes) |
showard | e8e3707 | 2009-08-20 23:31:30 +0000 | [diff] [blame] | 955 | raise SchedulerError( |
showard | 37757f3 | 2009-10-19 18:34:24 +0000 | [diff] [blame] | 956 | '%d unrecovered verifying host queue entries:\n%s' % |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 957 | (len(unrecovered_hqes), message)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 958 | |
| 959 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 960 | def _get_prioritized_special_tasks(self): |
| 961 | """ |
| 962 | Returns all queued SpecialTasks prioritized for repair first, then |
| 963 | cleanup, then verify. |
| 964 | """ |
| 965 | queued_tasks = models.SpecialTask.objects.filter(is_active=False, |
| 966 | is_complete=False, |
| 967 | host__locked=False) |
| 968 | # exclude hosts with active queue entries unless the SpecialTask is for |
| 969 | # that queue entry |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 970 | queued_tasks = models.SpecialTask.objects.add_join( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 971 | queued_tasks, 'afe_host_queue_entries', 'host_id', |
| 972 | join_condition='afe_host_queue_entries.active', |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 973 | join_from_key='host_id', force_left_join=True) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 974 | queued_tasks = queued_tasks.extra( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 975 | where=['(afe_host_queue_entries.id IS NULL OR ' |
| 976 | 'afe_host_queue_entries.id = ' |
| 977 | 'afe_special_tasks.queue_entry_id)']) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 978 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 979 | # reorder tasks by priority |
| 980 | task_priority_order = [models.SpecialTask.Task.REPAIR, |
| 981 | models.SpecialTask.Task.CLEANUP, |
| 982 | models.SpecialTask.Task.VERIFY] |
| 983 | def task_priority_key(task): |
| 984 | return task_priority_order.index(task.task) |
| 985 | return sorted(queued_tasks, key=task_priority_key) |
| 986 | |
| 987 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 988 | def _schedule_special_tasks(self): |
| 989 | """ |
| 990 | Execute queued SpecialTasks that are ready to run on idle hosts. |
| 991 | """ |
| 992 | for task in self._get_prioritized_special_tasks(): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 993 | if self.host_has_agent(task.host): |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 994 | continue |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 995 | self.add_agent_task(self._get_agent_task_for_special_task(task)) |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 996 | |
| 997 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 998 | def _reverify_remaining_hosts(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 999 | # recover active hosts that have not yet been recovered, although this |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1000 | # should never happen |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1001 | message = ('Recovering active host %s - this probably indicates a ' |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1002 | 'scheduler bug') |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1003 | self._reverify_hosts_where( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1004 | "status IN ('Repairing', 'Verifying', 'Cleaning')", |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1005 | print_message=message) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1006 | |
| 1007 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1008 | def _reverify_hosts_where(self, where, |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1009 | print_message='Reverifying host %s'): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1010 | full_where='locked = 0 AND invalid = 0 AND ' + where |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1011 | for host in scheduler_models.Host.fetch(where=full_where): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1012 | if self.host_has_agent(host): |
| 1013 | # host has already been recovered in some way |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1014 | continue |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1015 | if self._host_has_scheduled_special_task(host): |
| 1016 | # host will have a special task scheduled on the next cycle |
| 1017 | continue |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1018 | if print_message: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1019 | logging.info(print_message, host.hostname) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1020 | models.SpecialTask.objects.create( |
| 1021 | task=models.SpecialTask.Task.CLEANUP, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1022 | host=models.Host.objects.get(id=host.id)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1023 | |
| 1024 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1025 | def _recover_hosts(self): |
| 1026 | # recover "Repair Failed" hosts |
| 1027 | message = 'Reverifying dead host %s' |
| 1028 | self._reverify_hosts_where("status = 'Repair Failed'", |
| 1029 | print_message=message) |
mbligh | 62ba2ed | 2008-04-30 17:09:25 +0000 | [diff] [blame] | 1030 | |
| 1031 | |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 1032 | |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 1033 | def _get_pending_queue_entries(self): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 1034 | # prioritize by job priority, then non-metahost over metahost, then FIFO |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1035 | return list(scheduler_models.HostQueueEntry.fetch( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1036 | joins='INNER JOIN afe_jobs ON (job_id=afe_jobs.id)', |
showard | ac9ce22 | 2008-12-03 18:19:44 +0000 | [diff] [blame] | 1037 | where='NOT complete AND NOT active AND status="Queued"', |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1038 | order_by='afe_jobs.priority DESC, meta_host, job_id')) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1039 | |
| 1040 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1041 | def _refresh_pending_queue_entries(self): |
| 1042 | """ |
| 1043 | Lookup the pending HostQueueEntries and call our HostScheduler |
| 1044 | refresh() method given that list. Return the list. |
| 1045 | |
| 1046 | @returns A list of pending HostQueueEntries sorted in priority order. |
| 1047 | """ |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 1048 | queue_entries = self._get_pending_queue_entries() |
| 1049 | if not queue_entries: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1050 | return [] |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 1051 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 1052 | self._host_scheduler.refresh(queue_entries) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 1053 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1054 | return queue_entries |
| 1055 | |
| 1056 | |
| 1057 | def _schedule_atomic_group(self, queue_entry): |
| 1058 | """ |
| 1059 | Schedule the given queue_entry on an atomic group of hosts. |
| 1060 | |
| 1061 | Returns immediately if there are insufficient available hosts. |
| 1062 | |
| 1063 | Creates new HostQueueEntries based off of queue_entry for the |
| 1064 | scheduled hosts and starts them all running. |
| 1065 | """ |
| 1066 | # This is a virtual host queue entry representing an entire |
| 1067 | # atomic group, find a group and schedule their hosts. |
| 1068 | group_hosts = self._host_scheduler.find_eligible_atomic_group( |
| 1069 | queue_entry) |
| 1070 | if not group_hosts: |
| 1071 | return |
showard | cbe6f94 | 2009-06-17 19:33:49 +0000 | [diff] [blame] | 1072 | |
| 1073 | logging.info('Expanding atomic group entry %s with hosts %s', |
| 1074 | queue_entry, |
| 1075 | ', '.join(host.hostname for host in group_hosts)) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1076 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1077 | for assigned_host in group_hosts[1:]: |
| 1078 | # Create a new HQE for every additional assigned_host. |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1079 | new_hqe = scheduler_models.HostQueueEntry.clone(queue_entry) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1080 | new_hqe.save() |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1081 | new_hqe.set_host(assigned_host) |
| 1082 | self._run_queue_entry(new_hqe) |
| 1083 | |
| 1084 | # The first assigned host uses the original HostQueueEntry |
| 1085 | queue_entry.set_host(group_hosts[0]) |
| 1086 | self._run_queue_entry(queue_entry) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1087 | |
| 1088 | |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1089 | def _schedule_hostless_job(self, queue_entry): |
| 1090 | self.add_agent_task(HostlessQueueTask(queue_entry)) |
jamesren | 47bd737 | 2010-03-13 00:58:17 +0000 | [diff] [blame] | 1091 | queue_entry.set_status(models.HostQueueEntry.Status.STARTING) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1092 | |
| 1093 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1094 | def _schedule_new_jobs(self): |
| 1095 | queue_entries = self._refresh_pending_queue_entries() |
| 1096 | if not queue_entries: |
| 1097 | return |
| 1098 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 1099 | for queue_entry in queue_entries: |
showard | e55955f | 2009-10-07 20:48:58 +0000 | [diff] [blame] | 1100 | is_unassigned_atomic_group = ( |
| 1101 | queue_entry.atomic_group_id is not None |
| 1102 | and queue_entry.host_id is None) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1103 | |
| 1104 | if queue_entry.is_hostless(): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1105 | self._schedule_hostless_job(queue_entry) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1106 | elif is_unassigned_atomic_group: |
| 1107 | self._schedule_atomic_group(queue_entry) |
showard | e55955f | 2009-10-07 20:48:58 +0000 | [diff] [blame] | 1108 | else: |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1109 | assigned_host = self._host_scheduler.schedule_entry(queue_entry) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 1110 | if assigned_host and not self.host_has_agent(assigned_host): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1111 | assert assigned_host.id == queue_entry.host_id |
| 1112 | self._run_queue_entry(queue_entry) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 1113 | |
| 1114 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1115 | def _schedule_running_host_queue_entries(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1116 | for agent_task in self._get_queue_entry_agent_tasks(): |
| 1117 | self.add_agent_task(agent_task) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1118 | |
| 1119 | |
| 1120 | def _schedule_delay_tasks(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1121 | for entry in scheduler_models.HostQueueEntry.fetch( |
| 1122 | where='status = "%s"' % models.HostQueueEntry.Status.WAITING): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1123 | task = entry.job.schedule_delayed_callback_task(entry) |
| 1124 | if task: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1125 | self.add_agent_task(task) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1126 | |
| 1127 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1128 | def _run_queue_entry(self, queue_entry): |
| 1129 | queue_entry.schedule_pre_job_tasks() |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1130 | |
| 1131 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1132 | def _find_aborting(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1133 | for entry in scheduler_models.HostQueueEntry.fetch( |
| 1134 | where='aborted and not complete'): |
showard | f4a2e50 | 2009-07-28 20:06:39 +0000 | [diff] [blame] | 1135 | logging.info('Aborting %s', entry) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1136 | for agent in self.get_agents_for_entry(entry): |
| 1137 | agent.abort() |
| 1138 | entry.abort(self) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1139 | |
| 1140 | |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 1141 | def _can_start_agent(self, agent, num_started_this_cycle, |
| 1142 | have_reached_limit): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1143 | # always allow zero-process agents to run |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1144 | if agent.task.num_processes == 0: |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1145 | return True |
| 1146 | # don't allow any nonzero-process agents to run after we've reached a |
| 1147 | # limit (this avoids starvation of many-process agents) |
| 1148 | if have_reached_limit: |
| 1149 | return False |
| 1150 | # total process throttling |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1151 | max_runnable_processes = _drone_manager.max_runnable_processes( |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame^] | 1152 | agent.task.owner_username, |
| 1153 | agent.task.get_drone_hostnames_allowed()) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1154 | if agent.task.num_processes > max_runnable_processes: |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1155 | return False |
| 1156 | # if a single agent exceeds the per-cycle throttling, still allow it to |
| 1157 | # run when it's the first agent in the cycle |
| 1158 | if num_started_this_cycle == 0: |
| 1159 | return True |
| 1160 | # per-cycle throttling |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1161 | if (num_started_this_cycle + agent.task.num_processes > |
| 1162 | scheduler_config.config.max_processes_started_per_cycle): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1163 | return False |
| 1164 | return True |
| 1165 | |
| 1166 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1167 | def _handle_agents(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1168 | num_started_this_cycle = 0 |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1169 | have_reached_limit = False |
| 1170 | # iterate over copy, so we can remove agents during iteration |
| 1171 | for agent in list(self._agents): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1172 | if not agent.started: |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 1173 | if not self._can_start_agent(agent, num_started_this_cycle, |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1174 | have_reached_limit): |
| 1175 | have_reached_limit = True |
| 1176 | continue |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1177 | num_started_this_cycle += agent.task.num_processes |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1178 | agent.tick() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1179 | if agent.is_done(): |
| 1180 | logging.info("agent finished") |
| 1181 | self.remove_agent(agent) |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1182 | logging.info('%d running processes', |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1183 | _drone_manager.total_running_processes()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1184 | |
| 1185 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1186 | def _process_recurring_runs(self): |
| 1187 | recurring_runs = models.RecurringRun.objects.filter( |
| 1188 | start_date__lte=datetime.datetime.now()) |
| 1189 | for rrun in recurring_runs: |
| 1190 | # Create job from template |
| 1191 | job = rrun.job |
| 1192 | info = rpc_utils.get_job_info(job) |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1193 | options = job.get_object_dict() |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1194 | |
| 1195 | host_objects = info['hosts'] |
| 1196 | one_time_hosts = info['one_time_hosts'] |
| 1197 | metahost_objects = info['meta_hosts'] |
| 1198 | dependencies = info['dependencies'] |
| 1199 | atomic_group = info['atomic_group'] |
| 1200 | |
| 1201 | for host in one_time_hosts or []: |
| 1202 | this_host = models.Host.create_one_time_host(host.hostname) |
| 1203 | host_objects.append(this_host) |
| 1204 | |
| 1205 | try: |
| 1206 | rpc_utils.create_new_job(owner=rrun.owner.login, |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1207 | options=options, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1208 | host_objects=host_objects, |
| 1209 | metahost_objects=metahost_objects, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1210 | atomic_group=atomic_group) |
| 1211 | |
| 1212 | except Exception, ex: |
| 1213 | logging.exception(ex) |
| 1214 | #TODO send email |
| 1215 | |
| 1216 | if rrun.loop_count == 1: |
| 1217 | rrun.delete() |
| 1218 | else: |
| 1219 | if rrun.loop_count != 0: # if not infinite loop |
| 1220 | # calculate new start_date |
| 1221 | difference = datetime.timedelta(seconds=rrun.loop_period) |
| 1222 | rrun.start_date = rrun.start_date + difference |
| 1223 | rrun.loop_count -= 1 |
| 1224 | rrun.save() |
| 1225 | |
| 1226 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1227 | class PidfileRunMonitor(object): |
| 1228 | """ |
| 1229 | Client must call either run() to start a new process or |
| 1230 | attach_to_existing_process(). |
| 1231 | """ |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1232 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1233 | class _PidfileException(Exception): |
| 1234 | """ |
| 1235 | Raised when there's some unexpected behavior with the pid file, but only |
| 1236 | used internally (never allowed to escape this class). |
| 1237 | """ |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1238 | |
| 1239 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1240 | def __init__(self): |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1241 | self.lost_process = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1242 | self._start_time = None |
| 1243 | self.pidfile_id = None |
| 1244 | self._state = drone_manager.PidfileContents() |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1245 | |
| 1246 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1247 | def _add_nice_command(self, command, nice_level): |
| 1248 | if not nice_level: |
| 1249 | return command |
| 1250 | return ['nice', '-n', str(nice_level)] + command |
| 1251 | |
| 1252 | |
| 1253 | def _set_start_time(self): |
| 1254 | self._start_time = time.time() |
| 1255 | |
| 1256 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1257 | def run(self, command, working_directory, num_processes, nice_level=None, |
| 1258 | log_file=None, pidfile_name=None, paired_with_pidfile=None, |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame^] | 1259 | username=None, drone_hostnames_allowed=None): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1260 | assert command is not None |
| 1261 | if nice_level is not None: |
| 1262 | command = ['nice', '-n', str(nice_level)] + command |
| 1263 | self._set_start_time() |
| 1264 | self.pidfile_id = _drone_manager.execute_command( |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1265 | command, working_directory, pidfile_name=pidfile_name, |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1266 | num_processes=num_processes, log_file=log_file, |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame^] | 1267 | paired_with_pidfile=paired_with_pidfile, username=username, |
| 1268 | drone_hostnames_allowed=drone_hostnames_allowed) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1269 | |
| 1270 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1271 | def attach_to_existing_process(self, execution_path, |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1272 | pidfile_name=drone_manager.AUTOSERV_PID_FILE, |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1273 | num_processes=None): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1274 | self._set_start_time() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1275 | self.pidfile_id = _drone_manager.get_pidfile_id_from( |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1276 | execution_path, pidfile_name=pidfile_name) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1277 | if num_processes is not None: |
| 1278 | _drone_manager.declare_process_count(self.pidfile_id, num_processes) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1279 | |
| 1280 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1281 | def kill(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1282 | if self.has_process(): |
| 1283 | _drone_manager.kill_process(self.get_process()) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1284 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1285 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1286 | def has_process(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1287 | self._get_pidfile_info() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1288 | return self._state.process is not None |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1289 | |
| 1290 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1291 | def get_process(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1292 | self._get_pidfile_info() |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1293 | assert self._state.process is not None |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1294 | return self._state.process |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1295 | |
| 1296 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1297 | def _read_pidfile(self, use_second_read=False): |
| 1298 | assert self.pidfile_id is not None, ( |
| 1299 | 'You must call run() or attach_to_existing_process()') |
| 1300 | contents = _drone_manager.get_pidfile_contents( |
| 1301 | self.pidfile_id, use_second_read=use_second_read) |
| 1302 | if contents.is_invalid(): |
| 1303 | self._state = drone_manager.PidfileContents() |
| 1304 | raise self._PidfileException(contents) |
| 1305 | self._state = contents |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1306 | |
| 1307 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1308 | def _handle_pidfile_error(self, error, message=''): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1309 | message = error + '\nProcess: %s\nPidfile: %s\n%s' % ( |
| 1310 | self._state.process, self.pidfile_id, message) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1311 | email_manager.manager.enqueue_notify_email(error, message) |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1312 | self.on_lost_process(self._state.process) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1313 | |
| 1314 | |
| 1315 | def _get_pidfile_info_helper(self): |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1316 | if self.lost_process: |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1317 | return |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1318 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1319 | self._read_pidfile() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1320 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1321 | if self._state.process is None: |
| 1322 | self._handle_no_process() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1323 | return |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1324 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1325 | if self._state.exit_status is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1326 | # double check whether or not autoserv is running |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1327 | if _drone_manager.is_process_running(self._state.process): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1328 | return |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1329 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1330 | # pid but no running process - maybe process *just* exited |
| 1331 | self._read_pidfile(use_second_read=True) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1332 | if self._state.exit_status is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1333 | # autoserv exited without writing an exit code |
| 1334 | # to the pidfile |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1335 | self._handle_pidfile_error( |
| 1336 | 'autoserv died without writing exit code') |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1337 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1338 | |
| 1339 | def _get_pidfile_info(self): |
| 1340 | """\ |
| 1341 | After completion, self._state will contain: |
| 1342 | pid=None, exit_status=None if autoserv has not yet run |
| 1343 | pid!=None, exit_status=None if autoserv is running |
| 1344 | pid!=None, exit_status!=None if autoserv has completed |
| 1345 | """ |
| 1346 | try: |
| 1347 | self._get_pidfile_info_helper() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1348 | except self._PidfileException, exc: |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1349 | self._handle_pidfile_error('Pidfile error', traceback.format_exc()) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1350 | |
| 1351 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1352 | def _handle_no_process(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1353 | """\ |
| 1354 | Called when no pidfile is found or no pid is in the pidfile. |
| 1355 | """ |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1356 | message = 'No pid found at %s' % self.pidfile_id |
showard | ec6a3b9 | 2009-09-25 20:29:13 +0000 | [diff] [blame] | 1357 | if time.time() - self._start_time > _get_pidfile_timeout_secs(): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1358 | email_manager.manager.enqueue_notify_email( |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1359 | 'Process has failed to write pidfile', message) |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1360 | self.on_lost_process() |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1361 | |
| 1362 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1363 | def on_lost_process(self, process=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1364 | """\ |
| 1365 | Called when autoserv has exited without writing an exit status, |
| 1366 | or we've timed out waiting for autoserv to write a pid to the |
| 1367 | pidfile. In either case, we just return failure and the caller |
| 1368 | should signal some kind of warning. |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1369 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1370 | process is unimportant here, as it shouldn't be used by anyone. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1371 | """ |
| 1372 | self.lost_process = True |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1373 | self._state.process = process |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1374 | self._state.exit_status = 1 |
| 1375 | self._state.num_tests_failed = 0 |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1376 | |
| 1377 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1378 | def exit_code(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1379 | self._get_pidfile_info() |
| 1380 | return self._state.exit_status |
| 1381 | |
| 1382 | |
| 1383 | def num_tests_failed(self): |
showard | 6bba3d1 | 2009-08-20 23:31:41 +0000 | [diff] [blame] | 1384 | """@returns The number of tests that failed or -1 if unknown.""" |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1385 | self._get_pidfile_info() |
showard | 6bba3d1 | 2009-08-20 23:31:41 +0000 | [diff] [blame] | 1386 | if self._state.num_tests_failed is None: |
| 1387 | return -1 |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1388 | return self._state.num_tests_failed |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1389 | |
| 1390 | |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1391 | def try_copy_results_on_drone(self, **kwargs): |
| 1392 | if self.has_process(): |
| 1393 | # copy results logs into the normal place for job results |
| 1394 | _drone_manager.copy_results_on_drone(self.get_process(), **kwargs) |
| 1395 | |
| 1396 | |
| 1397 | def try_copy_to_results_repository(self, source, **kwargs): |
| 1398 | if self.has_process(): |
| 1399 | _drone_manager.copy_to_results_repository(self.get_process(), |
| 1400 | source, **kwargs) |
| 1401 | |
| 1402 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1403 | class Agent(object): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1404 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1405 | An agent for use by the Dispatcher class to perform a task. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1406 | |
| 1407 | The following methods are required on all task objects: |
| 1408 | poll() - Called periodically to let the task check its status and |
| 1409 | update its internal state. If the task succeeded. |
| 1410 | is_done() - Returns True if the task is finished. |
| 1411 | abort() - Called when an abort has been requested. The task must |
| 1412 | set its aborted attribute to True if it actually aborted. |
| 1413 | |
| 1414 | The following attributes are required on all task objects: |
| 1415 | aborted - bool, True if this task was aborted. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1416 | success - bool, True if this task succeeded. |
| 1417 | queue_entry_ids - A sequence of HostQueueEntry ids this task handles. |
| 1418 | host_ids - A sequence of Host ids this task represents. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1419 | """ |
| 1420 | |
| 1421 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1422 | def __init__(self, task): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1423 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1424 | @param task: A task as described in the class docstring. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1425 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1426 | self.task = task |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1427 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1428 | # This is filled in by Dispatcher.add_agent() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1429 | self.dispatcher = None |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1430 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1431 | self.queue_entry_ids = task.queue_entry_ids |
| 1432 | self.host_ids = task.host_ids |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1433 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1434 | self.started = False |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1435 | self.finished = False |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1436 | |
| 1437 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1438 | def tick(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1439 | self.started = True |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1440 | if not self.finished: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1441 | self.task.poll() |
| 1442 | if self.task.is_done(): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1443 | self.finished = True |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 1444 | |
| 1445 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1446 | def is_done(self): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1447 | return self.finished |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1448 | |
| 1449 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1450 | def abort(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1451 | if self.task: |
| 1452 | self.task.abort() |
| 1453 | if self.task.aborted: |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1454 | # tasks can choose to ignore aborts |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1455 | self.finished = True |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1456 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1457 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1458 | class AgentTask(object): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1459 | class _NullMonitor(object): |
| 1460 | pidfile_id = None |
| 1461 | |
| 1462 | def has_process(self): |
| 1463 | return True |
| 1464 | |
| 1465 | |
| 1466 | def __init__(self, log_file_name=None): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1467 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1468 | @param log_file_name: (optional) name of file to log command output to |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1469 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1470 | self.done = False |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1471 | self.started = False |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1472 | self.success = None |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1473 | self.aborted = False |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1474 | self.monitor = None |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1475 | self.queue_entry_ids = [] |
| 1476 | self.host_ids = [] |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1477 | self._log_file_name = log_file_name |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1478 | |
| 1479 | |
| 1480 | def _set_ids(self, host=None, queue_entries=None): |
| 1481 | if queue_entries and queue_entries != [None]: |
| 1482 | self.host_ids = [entry.host.id for entry in queue_entries] |
| 1483 | self.queue_entry_ids = [entry.id for entry in queue_entries] |
| 1484 | else: |
| 1485 | assert host |
| 1486 | self.host_ids = [host.id] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1487 | |
| 1488 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1489 | def poll(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1490 | if not self.started: |
| 1491 | self.start() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1492 | if not self.done: |
| 1493 | self.tick() |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1494 | |
| 1495 | |
| 1496 | def tick(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1497 | assert self.monitor |
| 1498 | exit_code = self.monitor.exit_code() |
| 1499 | if exit_code is None: |
| 1500 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1501 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1502 | success = (exit_code == 0) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1503 | self.finished(success) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1504 | |
| 1505 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1506 | def is_done(self): |
| 1507 | return self.done |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1508 | |
| 1509 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1510 | def finished(self, success): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1511 | if self.done: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1512 | assert self.started |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1513 | return |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1514 | self.started = True |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1515 | self.done = True |
| 1516 | self.success = success |
| 1517 | self.epilog() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1518 | |
| 1519 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1520 | def prolog(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1521 | """ |
| 1522 | To be overridden. |
| 1523 | """ |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1524 | assert not self.monitor |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1525 | self.register_necessary_pidfiles() |
| 1526 | |
| 1527 | |
| 1528 | def _log_file(self): |
| 1529 | if not self._log_file_name: |
| 1530 | return None |
| 1531 | return os.path.join(self._working_directory(), self._log_file_name) |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1532 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1533 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1534 | def cleanup(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1535 | log_file = self._log_file() |
| 1536 | if self.monitor and log_file: |
| 1537 | self.monitor.try_copy_to_results_repository(log_file) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1538 | |
| 1539 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1540 | def epilog(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1541 | """ |
| 1542 | To be overridden. |
| 1543 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1544 | self.cleanup() |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1545 | logging.info("%s finished with success=%s", type(self).__name__, |
| 1546 | self.success) |
| 1547 | |
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): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1551 | if not self.started: |
| 1552 | self.prolog() |
| 1553 | self.run() |
| 1554 | |
| 1555 | self.started = True |
| 1556 | |
| 1557 | |
| 1558 | def abort(self): |
| 1559 | if self.monitor: |
| 1560 | self.monitor.kill() |
| 1561 | self.done = True |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1562 | self.aborted = True |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1563 | self.cleanup() |
| 1564 | |
| 1565 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1566 | def _get_consistent_execution_path(self, execution_entries): |
| 1567 | first_execution_path = execution_entries[0].execution_path() |
| 1568 | for execution_entry in execution_entries[1:]: |
| 1569 | assert execution_entry.execution_path() == first_execution_path, ( |
| 1570 | '%s (%s) != %s (%s)' % (execution_entry.execution_path(), |
| 1571 | execution_entry, |
| 1572 | first_execution_path, |
| 1573 | execution_entries[0])) |
| 1574 | return first_execution_path |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1575 | |
| 1576 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1577 | def _copy_results(self, execution_entries, use_monitor=None): |
| 1578 | """ |
| 1579 | @param execution_entries: list of objects with execution_path() method |
| 1580 | """ |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1581 | if use_monitor is not None and not use_monitor.has_process(): |
| 1582 | return |
| 1583 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1584 | assert len(execution_entries) > 0 |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1585 | if use_monitor is None: |
| 1586 | assert self.monitor |
| 1587 | use_monitor = self.monitor |
| 1588 | assert use_monitor.has_process() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1589 | execution_path = self._get_consistent_execution_path(execution_entries) |
| 1590 | results_path = execution_path + '/' |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1591 | use_monitor.try_copy_to_results_repository(results_path) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1592 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1593 | |
| 1594 | def _parse_results(self, queue_entries): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1595 | for queue_entry in queue_entries: |
| 1596 | queue_entry.set_status(models.HostQueueEntry.Status.PARSING) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1597 | |
| 1598 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1599 | def _archive_results(self, queue_entries): |
| 1600 | for queue_entry in queue_entries: |
| 1601 | queue_entry.set_status(models.HostQueueEntry.Status.ARCHIVING) |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1602 | |
| 1603 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1604 | def _command_line(self): |
| 1605 | """ |
| 1606 | Return the command line to run. Must be overridden. |
| 1607 | """ |
| 1608 | raise NotImplementedError |
| 1609 | |
| 1610 | |
| 1611 | @property |
| 1612 | def num_processes(self): |
| 1613 | """ |
| 1614 | Return the number of processes forked by this AgentTask's process. It |
| 1615 | may only be approximate. To be overridden if necessary. |
| 1616 | """ |
| 1617 | return 1 |
| 1618 | |
| 1619 | |
| 1620 | def _paired_with_monitor(self): |
| 1621 | """ |
| 1622 | If this AgentTask's process must run on the same machine as some |
| 1623 | previous process, this method should be overridden to return a |
| 1624 | PidfileRunMonitor for that process. |
| 1625 | """ |
| 1626 | return self._NullMonitor() |
| 1627 | |
| 1628 | |
| 1629 | @property |
| 1630 | def owner_username(self): |
| 1631 | """ |
| 1632 | Return login of user responsible for this task. May be None. Must be |
| 1633 | overridden. |
| 1634 | """ |
| 1635 | raise NotImplementedError |
| 1636 | |
| 1637 | |
| 1638 | def _working_directory(self): |
| 1639 | """ |
| 1640 | Return the directory where this AgentTask's process executes. Must be |
| 1641 | overridden. |
| 1642 | """ |
| 1643 | raise NotImplementedError |
| 1644 | |
| 1645 | |
| 1646 | def _pidfile_name(self): |
| 1647 | """ |
| 1648 | Return the name of the pidfile this AgentTask's process uses. To be |
| 1649 | overridden if necessary. |
| 1650 | """ |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1651 | return drone_manager.AUTOSERV_PID_FILE |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1652 | |
| 1653 | |
| 1654 | def _check_paired_results_exist(self): |
| 1655 | if not self._paired_with_monitor().has_process(): |
| 1656 | email_manager.manager.enqueue_notify_email( |
| 1657 | 'No paired results in task', |
| 1658 | 'No paired results in task %s at %s' |
| 1659 | % (self, self._paired_with_monitor().pidfile_id)) |
| 1660 | self.finished(False) |
| 1661 | return False |
| 1662 | return True |
| 1663 | |
| 1664 | |
| 1665 | def _create_monitor(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1666 | assert not self.monitor |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1667 | self.monitor = PidfileRunMonitor() |
| 1668 | |
| 1669 | |
| 1670 | def run(self): |
| 1671 | if not self._check_paired_results_exist(): |
| 1672 | return |
| 1673 | |
| 1674 | self._create_monitor() |
| 1675 | self.monitor.run( |
| 1676 | self._command_line(), self._working_directory(), |
| 1677 | num_processes=self.num_processes, |
| 1678 | nice_level=AUTOSERV_NICE_LEVEL, log_file=self._log_file(), |
| 1679 | pidfile_name=self._pidfile_name(), |
| 1680 | paired_with_pidfile=self._paired_with_monitor().pidfile_id, |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame^] | 1681 | username=self.owner_username, |
| 1682 | drone_hostnames_allowed=self.get_drone_hostnames_allowed()) |
| 1683 | |
| 1684 | |
| 1685 | def get_drone_hostnames_allowed(self): |
| 1686 | if not models.DroneSet.drone_sets_enabled(): |
| 1687 | return None |
| 1688 | |
| 1689 | hqes = models.HostQueueEntry.objects.filter(id__in=self.queue_entry_ids) |
| 1690 | if not hqes: |
| 1691 | # Only special tasks could be missing host queue entries |
| 1692 | assert isinstance(self, SpecialAgentTask) |
| 1693 | return self._user_or_global_default_drone_set( |
| 1694 | self.task, self.task.requested_by) |
| 1695 | |
| 1696 | job_ids = hqes.values_list('job', flat=True).distinct() |
| 1697 | assert job_ids.count() == 1, ("AgentTask's queue entries " |
| 1698 | "span multiple jobs") |
| 1699 | |
| 1700 | job = models.Job.objects.get(id=job_ids[0]) |
| 1701 | drone_set = job.drone_set |
| 1702 | if not drone_set: |
| 1703 | return self_user_or_global_default_drone_set(job, job.user()) |
| 1704 | |
| 1705 | return drone_set.get_drone_hostnames() |
| 1706 | |
| 1707 | |
| 1708 | def _user_or_global_default_drone_set(self, obj_with_owner, user): |
| 1709 | """ |
| 1710 | Returns the user's default drone set, if present. |
| 1711 | |
| 1712 | Otherwise, returns the global default drone set. |
| 1713 | """ |
| 1714 | default_hostnames = models.DroneSet.get_default().get_drone_hostnames() |
| 1715 | if not user: |
| 1716 | logging.warn('%s had no owner; using default drone set', |
| 1717 | obj_with_owner) |
| 1718 | return default_hostnames |
| 1719 | if not user.drone_set: |
| 1720 | logging.warn('User %s has no default drone set, using global ' |
| 1721 | 'default', user.login) |
| 1722 | return default_hostnames |
| 1723 | return user.drone_set.get_drone_hostnames() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1724 | |
| 1725 | |
| 1726 | def register_necessary_pidfiles(self): |
| 1727 | pidfile_id = _drone_manager.get_pidfile_id_from( |
| 1728 | self._working_directory(), self._pidfile_name()) |
| 1729 | _drone_manager.register_pidfile(pidfile_id) |
| 1730 | |
| 1731 | paired_pidfile_id = self._paired_with_monitor().pidfile_id |
| 1732 | if paired_pidfile_id: |
| 1733 | _drone_manager.register_pidfile(paired_pidfile_id) |
| 1734 | |
| 1735 | |
| 1736 | def recover(self): |
| 1737 | if not self._check_paired_results_exist(): |
| 1738 | return |
| 1739 | |
| 1740 | self._create_monitor() |
| 1741 | self.monitor.attach_to_existing_process( |
| 1742 | self._working_directory(), pidfile_name=self._pidfile_name(), |
| 1743 | num_processes=self.num_processes) |
| 1744 | if not self.monitor.has_process(): |
| 1745 | # no process to recover; wait to be started normally |
| 1746 | self.monitor = None |
| 1747 | return |
| 1748 | |
| 1749 | self.started = True |
| 1750 | logging.info('Recovering process %s for %s at %s' |
| 1751 | % (self.monitor.get_process(), type(self).__name__, |
| 1752 | self._working_directory())) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1753 | |
| 1754 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1755 | def _check_queue_entry_statuses(self, queue_entries, allowed_hqe_statuses, |
| 1756 | allowed_host_statuses=None): |
| 1757 | for entry in queue_entries: |
| 1758 | if entry.status not in allowed_hqe_statuses: |
| 1759 | raise SchedulerError('Queue task attempting to start ' |
| 1760 | 'entry with invalid status %s: %s' |
| 1761 | % (entry.status, entry)) |
| 1762 | invalid_host_status = ( |
| 1763 | allowed_host_statuses is not None |
| 1764 | and entry.host.status not in allowed_host_statuses) |
| 1765 | if invalid_host_status: |
| 1766 | raise SchedulerError('Queue task attempting to start on queue ' |
| 1767 | 'entry with invalid host status %s: %s' |
| 1768 | % (entry.host.status, entry)) |
| 1769 | |
| 1770 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1771 | class TaskWithJobKeyvals(object): |
| 1772 | """AgentTask mixin providing functionality to help with job keyval files.""" |
| 1773 | _KEYVAL_FILE = 'keyval' |
| 1774 | def _format_keyval(self, key, value): |
| 1775 | return '%s=%s' % (key, value) |
| 1776 | |
| 1777 | |
| 1778 | def _keyval_path(self): |
| 1779 | """Subclasses must override this""" |
lmr | b7c5d27 | 2010-04-16 06:34:04 +0000 | [diff] [blame] | 1780 | raise NotImplementedError |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1781 | |
| 1782 | |
| 1783 | def _write_keyval_after_job(self, field, value): |
| 1784 | assert self.monitor |
| 1785 | if not self.monitor.has_process(): |
| 1786 | return |
| 1787 | _drone_manager.write_lines_to_file( |
| 1788 | self._keyval_path(), [self._format_keyval(field, value)], |
| 1789 | paired_with_process=self.monitor.get_process()) |
| 1790 | |
| 1791 | |
| 1792 | def _job_queued_keyval(self, job): |
| 1793 | return 'job_queued', int(time.mktime(job.created_on.timetuple())) |
| 1794 | |
| 1795 | |
| 1796 | def _write_job_finished(self): |
| 1797 | self._write_keyval_after_job("job_finished", int(time.time())) |
| 1798 | |
| 1799 | |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1800 | def _write_keyvals_before_job_helper(self, keyval_dict, keyval_path): |
| 1801 | keyval_contents = '\n'.join(self._format_keyval(key, value) |
| 1802 | for key, value in keyval_dict.iteritems()) |
| 1803 | # always end with a newline to allow additional keyvals to be written |
| 1804 | keyval_contents += '\n' |
showard | 493beaa | 2009-12-18 22:44:45 +0000 | [diff] [blame] | 1805 | _drone_manager.attach_file_to_execution(self._working_directory(), |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1806 | keyval_contents, |
| 1807 | file_path=keyval_path) |
| 1808 | |
| 1809 | |
| 1810 | def _write_keyvals_before_job(self, keyval_dict): |
| 1811 | self._write_keyvals_before_job_helper(keyval_dict, self._keyval_path()) |
| 1812 | |
| 1813 | |
| 1814 | def _write_host_keyvals(self, host): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1815 | keyval_path = os.path.join(self._working_directory(), 'host_keyvals', |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1816 | host.hostname) |
| 1817 | platform, all_labels = host.platform_and_labels() |
| 1818 | keyval_dict = dict(platform=platform, labels=','.join(all_labels)) |
| 1819 | self._write_keyvals_before_job_helper(keyval_dict, keyval_path) |
| 1820 | |
| 1821 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1822 | class SpecialAgentTask(AgentTask, TaskWithJobKeyvals): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1823 | """ |
| 1824 | Subclass for AgentTasks that correspond to a SpecialTask entry in the DB. |
| 1825 | """ |
| 1826 | |
| 1827 | TASK_TYPE = None |
| 1828 | host = None |
| 1829 | queue_entry = None |
| 1830 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1831 | def __init__(self, task, extra_command_args): |
| 1832 | super(SpecialAgentTask, self).__init__() |
| 1833 | |
lmr | b7c5d27 | 2010-04-16 06:34:04 +0000 | [diff] [blame] | 1834 | assert self.TASK_TYPE is not None, 'self.TASK_TYPE must be overridden' |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1835 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1836 | self.host = scheduler_models.Host(id=task.host.id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1837 | self.queue_entry = None |
| 1838 | if task.queue_entry: |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1839 | self.queue_entry = scheduler_models.HostQueueEntry( |
| 1840 | id=task.queue_entry.id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1841 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1842 | self.task = task |
| 1843 | self._extra_command_args = extra_command_args |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1844 | |
| 1845 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1846 | def _keyval_path(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1847 | return os.path.join(self._working_directory(), self._KEYVAL_FILE) |
| 1848 | |
| 1849 | |
| 1850 | def _command_line(self): |
| 1851 | return _autoserv_command_line(self.host.hostname, |
| 1852 | self._extra_command_args, |
| 1853 | queue_entry=self.queue_entry) |
| 1854 | |
| 1855 | |
| 1856 | def _working_directory(self): |
| 1857 | return self.task.execution_path() |
| 1858 | |
| 1859 | |
| 1860 | @property |
| 1861 | def owner_username(self): |
| 1862 | if self.task.requested_by: |
| 1863 | return self.task.requested_by.login |
| 1864 | return None |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1865 | |
| 1866 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1867 | def prolog(self): |
| 1868 | super(SpecialAgentTask, self).prolog() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1869 | self.task.activate() |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1870 | self._write_host_keyvals(self.host) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1871 | |
| 1872 | |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1873 | def _fail_queue_entry(self): |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1874 | assert self.queue_entry |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1875 | |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1876 | if self.queue_entry.meta_host: |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1877 | return # don't fail metahost entries, they'll be reassigned |
| 1878 | |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1879 | self.queue_entry.update_from_database() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1880 | if self.queue_entry.status != models.HostQueueEntry.Status.QUEUED: |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1881 | return # entry has been aborted |
| 1882 | |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1883 | self.queue_entry.set_execution_subdir() |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1884 | queued_key, queued_time = self._job_queued_keyval( |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1885 | self.queue_entry.job) |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1886 | self._write_keyval_after_job(queued_key, queued_time) |
| 1887 | self._write_job_finished() |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1888 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1889 | # copy results logs into the normal place for job results |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1890 | self.monitor.try_copy_results_on_drone( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1891 | source_path=self._working_directory() + '/', |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1892 | destination_path=self.queue_entry.execution_path() + '/') |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1893 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1894 | pidfile_id = _drone_manager.get_pidfile_id_from( |
| 1895 | self.queue_entry.execution_path(), |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1896 | pidfile_name=drone_manager.AUTOSERV_PID_FILE) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1897 | _drone_manager.register_pidfile(pidfile_id) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1898 | |
| 1899 | if self.queue_entry.job.parse_failed_repair: |
| 1900 | self._parse_results([self.queue_entry]) |
| 1901 | else: |
| 1902 | self._archive_results([self.queue_entry]) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1903 | |
| 1904 | |
| 1905 | def cleanup(self): |
| 1906 | super(SpecialAgentTask, self).cleanup() |
showard | e60e44e | 2009-11-13 20:45:38 +0000 | [diff] [blame] | 1907 | |
| 1908 | # We will consider an aborted task to be "Failed" |
| 1909 | self.task.finish(bool(self.success)) |
| 1910 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 1911 | if self.monitor: |
| 1912 | if self.monitor.has_process(): |
| 1913 | self._copy_results([self.task]) |
| 1914 | if self.monitor.pidfile_id is not None: |
| 1915 | _drone_manager.unregister_pidfile(self.monitor.pidfile_id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1916 | |
| 1917 | |
| 1918 | class RepairTask(SpecialAgentTask): |
| 1919 | TASK_TYPE = models.SpecialTask.Task.REPAIR |
| 1920 | |
| 1921 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1922 | def __init__(self, task): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1923 | """\ |
| 1924 | queue_entry: queue entry to mark failed if this repair fails. |
| 1925 | """ |
| 1926 | protection = host_protections.Protection.get_string( |
| 1927 | task.host.protection) |
| 1928 | # normalize the protection name |
| 1929 | protection = host_protections.Protection.get_attr_name(protection) |
| 1930 | |
| 1931 | super(RepairTask, self).__init__( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1932 | task, ['-R', '--host-protection', protection]) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1933 | |
| 1934 | # *don't* include the queue entry in IDs -- if the queue entry is |
| 1935 | # aborted, we want to leave the repair task running |
| 1936 | self._set_ids(host=self.host) |
| 1937 | |
| 1938 | |
| 1939 | def prolog(self): |
| 1940 | super(RepairTask, self).prolog() |
| 1941 | logging.info("repair_task starting") |
| 1942 | self.host.set_status(models.Host.Status.REPAIRING) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1943 | |
| 1944 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1945 | def epilog(self): |
| 1946 | super(RepairTask, self).epilog() |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 1947 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1948 | if self.success: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1949 | self.host.set_status(models.Host.Status.READY) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1950 | else: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1951 | self.host.set_status(models.Host.Status.REPAIR_FAILED) |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1952 | if self.queue_entry: |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1953 | self._fail_queue_entry() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1954 | |
| 1955 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1956 | class PreJobTask(SpecialAgentTask): |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1957 | def _copy_to_results_repository(self): |
| 1958 | if not self.queue_entry or self.queue_entry.meta_host: |
| 1959 | return |
| 1960 | |
| 1961 | self.queue_entry.set_execution_subdir() |
| 1962 | log_name = os.path.basename(self.task.execution_path()) |
| 1963 | source = os.path.join(self.task.execution_path(), 'debug', |
| 1964 | 'autoserv.DEBUG') |
| 1965 | destination = os.path.join( |
| 1966 | self.queue_entry.execution_path(), log_name) |
| 1967 | |
| 1968 | self.monitor.try_copy_to_results_repository( |
| 1969 | source, destination_path=destination) |
| 1970 | |
| 1971 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1972 | def epilog(self): |
| 1973 | super(PreJobTask, self).epilog() |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1974 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1975 | if self.success: |
| 1976 | return |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1977 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1978 | self._copy_to_results_repository() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1979 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1980 | if self.host.protection == host_protections.Protection.DO_NOT_VERIFY: |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 1981 | # effectively ignore failure for these hosts |
| 1982 | self.success = True |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1983 | return |
| 1984 | |
| 1985 | if self.queue_entry: |
| 1986 | self.queue_entry.requeue() |
| 1987 | |
| 1988 | if models.SpecialTask.objects.filter( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1989 | task=models.SpecialTask.Task.REPAIR, |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1990 | queue_entry__id=self.queue_entry.id): |
| 1991 | self.host.set_status(models.Host.Status.REPAIR_FAILED) |
| 1992 | self._fail_queue_entry() |
| 1993 | return |
| 1994 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1995 | queue_entry = models.HostQueueEntry.objects.get( |
| 1996 | id=self.queue_entry.id) |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1997 | else: |
| 1998 | queue_entry = None |
| 1999 | |
| 2000 | models.SpecialTask.objects.create( |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2001 | host=models.Host.objects.get(id=self.host.id), |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 2002 | task=models.SpecialTask.Task.REPAIR, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2003 | queue_entry=queue_entry, |
| 2004 | requested_by=self.task.requested_by) |
showard | 58721a8 | 2009-08-20 23:32:40 +0000 | [diff] [blame] | 2005 | |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 2006 | |
| 2007 | class VerifyTask(PreJobTask): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2008 | TASK_TYPE = models.SpecialTask.Task.VERIFY |
| 2009 | |
| 2010 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2011 | def __init__(self, task): |
| 2012 | super(VerifyTask, self).__init__(task, ['-v']) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2013 | self._set_ids(host=self.host, queue_entries=[self.queue_entry]) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2014 | |
| 2015 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2016 | def prolog(self): |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 2017 | super(VerifyTask, self).prolog() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2018 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2019 | logging.info("starting verify on %s", self.host.hostname) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2020 | if self.queue_entry: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2021 | self.queue_entry.set_status(models.HostQueueEntry.Status.VERIFYING) |
| 2022 | self.host.set_status(models.Host.Status.VERIFYING) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2023 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2024 | # Delete any other queued verifies for this host. One verify will do |
| 2025 | # and there's no need to keep records of other requests. |
| 2026 | queued_verifies = models.SpecialTask.objects.filter( |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2027 | host__id=self.host.id, |
| 2028 | task=models.SpecialTask.Task.VERIFY, |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2029 | is_active=False, is_complete=False) |
| 2030 | queued_verifies = queued_verifies.exclude(id=self.task.id) |
| 2031 | queued_verifies.delete() |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2032 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2033 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2034 | def epilog(self): |
| 2035 | super(VerifyTask, self).epilog() |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2036 | if self.success: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2037 | if self.queue_entry: |
| 2038 | self.queue_entry.on_pending() |
| 2039 | else: |
| 2040 | self.host.set_status(models.Host.Status.READY) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2041 | |
| 2042 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2043 | class CleanupTask(PreJobTask): |
| 2044 | # note this can also run post-job, but when it does, it's running standalone |
| 2045 | # against the host (not related to the job), so it's not considered a |
| 2046 | # PostJobTask |
| 2047 | |
| 2048 | TASK_TYPE = models.SpecialTask.Task.CLEANUP |
| 2049 | |
| 2050 | |
| 2051 | def __init__(self, task, recover_run_monitor=None): |
| 2052 | super(CleanupTask, self).__init__(task, ['--cleanup']) |
| 2053 | self._set_ids(host=self.host, queue_entries=[self.queue_entry]) |
| 2054 | |
| 2055 | |
| 2056 | def prolog(self): |
| 2057 | super(CleanupTask, self).prolog() |
| 2058 | logging.info("starting cleanup task for host: %s", self.host.hostname) |
| 2059 | self.host.set_status(models.Host.Status.CLEANING) |
| 2060 | if self.queue_entry: |
| 2061 | self.queue_entry.set_status(models.HostQueueEntry.Status.VERIFYING) |
| 2062 | |
| 2063 | |
| 2064 | def _finish_epilog(self): |
| 2065 | if not self.queue_entry or not self.success: |
| 2066 | return |
| 2067 | |
| 2068 | do_not_verify_protection = host_protections.Protection.DO_NOT_VERIFY |
| 2069 | should_run_verify = ( |
| 2070 | self.queue_entry.job.run_verify |
| 2071 | and self.host.protection != do_not_verify_protection) |
| 2072 | if should_run_verify: |
| 2073 | entry = models.HostQueueEntry.objects.get(id=self.queue_entry.id) |
| 2074 | models.SpecialTask.objects.create( |
| 2075 | host=models.Host.objects.get(id=self.host.id), |
| 2076 | queue_entry=entry, |
| 2077 | task=models.SpecialTask.Task.VERIFY) |
| 2078 | else: |
| 2079 | self.queue_entry.on_pending() |
| 2080 | |
| 2081 | |
| 2082 | def epilog(self): |
| 2083 | super(CleanupTask, self).epilog() |
| 2084 | |
| 2085 | if self.success: |
| 2086 | self.host.update_field('dirty', 0) |
| 2087 | self.host.set_status(models.Host.Status.READY) |
| 2088 | |
| 2089 | self._finish_epilog() |
| 2090 | |
| 2091 | |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2092 | class AbstractQueueTask(AgentTask, TaskWithJobKeyvals): |
| 2093 | """ |
| 2094 | Common functionality for QueueTask and HostlessQueueTask |
| 2095 | """ |
| 2096 | def __init__(self, queue_entries): |
| 2097 | super(AbstractQueueTask, self).__init__() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2098 | self.job = queue_entries[0].job |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2099 | self.queue_entries = queue_entries |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2100 | |
| 2101 | |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 2102 | def _keyval_path(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2103 | return os.path.join(self._working_directory(), self._KEYVAL_FILE) |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 2104 | |
| 2105 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 2106 | def _write_control_file(self, execution_path): |
| 2107 | control_path = _drone_manager.attach_file_to_execution( |
| 2108 | execution_path, self.job.control_file) |
| 2109 | return control_path |
| 2110 | |
| 2111 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2112 | def _command_line(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 2113 | execution_path = self.queue_entries[0].execution_path() |
| 2114 | control_path = self._write_control_file(execution_path) |
| 2115 | hostnames = ','.join(entry.host.hostname |
| 2116 | for entry in self.queue_entries |
| 2117 | if not entry.is_hostless()) |
| 2118 | |
| 2119 | execution_tag = self.queue_entries[0].execution_tag() |
| 2120 | params = _autoserv_command_line( |
| 2121 | hostnames, |
| 2122 | ['-P', execution_tag, '-n', |
| 2123 | _drone_manager.absolute_path(control_path)], |
| 2124 | job=self.job, verbose=False) |
| 2125 | |
| 2126 | if not self.job.is_server_job(): |
| 2127 | params.append('-c') |
| 2128 | |
| 2129 | return params |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2130 | |
| 2131 | |
| 2132 | @property |
| 2133 | def num_processes(self): |
| 2134 | return len(self.queue_entries) |
| 2135 | |
| 2136 | |
| 2137 | @property |
| 2138 | def owner_username(self): |
| 2139 | return self.job.owner |
| 2140 | |
| 2141 | |
| 2142 | def _working_directory(self): |
| 2143 | return self._get_consistent_execution_path(self.queue_entries) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 2144 | |
| 2145 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2146 | def prolog(self): |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 2147 | queued_key, queued_time = self._job_queued_keyval(self.job) |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 2148 | keyval_dict = self.job.keyval_dict() |
| 2149 | keyval_dict[queued_key] = queued_time |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2150 | group_name = self.queue_entries[0].get_group_name() |
| 2151 | if group_name: |
| 2152 | keyval_dict['host_group_name'] = group_name |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2153 | self._write_keyvals_before_job(keyval_dict) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2154 | for queue_entry in self.queue_entries: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2155 | queue_entry.set_status(models.HostQueueEntry.Status.RUNNING) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2156 | queue_entry.set_started_on_now() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2157 | |
| 2158 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 2159 | def _write_lost_process_error_file(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2160 | error_file_path = os.path.join(self._working_directory(), 'job_failure') |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 2161 | _drone_manager.write_lines_to_file(error_file_path, |
| 2162 | [_LOST_PROCESS_ERROR]) |
| 2163 | |
| 2164 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2165 | def _finish_task(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 2166 | if not self.monitor: |
| 2167 | return |
| 2168 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 2169 | self._write_job_finished() |
| 2170 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 2171 | if self.monitor.lost_process: |
| 2172 | self._write_lost_process_error_file() |
showard | 4ac4754 | 2009-08-31 18:32:19 +0000 | [diff] [blame] | 2173 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2174 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 2175 | def _write_status_comment(self, comment): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2176 | _drone_manager.write_lines_to_file( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2177 | os.path.join(self._working_directory(), 'status.log'), |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2178 | ['INFO\t----\t----\t' + comment], |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 2179 | paired_with_process=self.monitor.get_process()) |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 2180 | |
| 2181 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2182 | def _log_abort(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2183 | if not self.monitor or not self.monitor.has_process(): |
| 2184 | return |
| 2185 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2186 | # build up sets of all the aborted_by and aborted_on values |
| 2187 | aborted_by, aborted_on = set(), set() |
| 2188 | for queue_entry in self.queue_entries: |
| 2189 | if queue_entry.aborted_by: |
| 2190 | aborted_by.add(queue_entry.aborted_by) |
| 2191 | t = int(time.mktime(queue_entry.aborted_on.timetuple())) |
| 2192 | aborted_on.add(t) |
| 2193 | |
| 2194 | # extract some actual, unique aborted by value and write it out |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 2195 | # TODO(showard): this conditional is now obsolete, we just need to leave |
| 2196 | # it in temporarily for backwards compatibility over upgrades. delete |
| 2197 | # soon. |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2198 | assert len(aborted_by) <= 1 |
| 2199 | if len(aborted_by) == 1: |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 2200 | aborted_by_value = aborted_by.pop() |
| 2201 | aborted_on_value = max(aborted_on) |
| 2202 | else: |
| 2203 | aborted_by_value = 'autotest_system' |
| 2204 | aborted_on_value = int(time.time()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2205 | |
showard | a038235 | 2009-02-11 23:36:43 +0000 | [diff] [blame] | 2206 | self._write_keyval_after_job("aborted_by", aborted_by_value) |
| 2207 | self._write_keyval_after_job("aborted_on", aborted_on_value) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2208 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 2209 | aborted_on_string = str(datetime.datetime.fromtimestamp( |
| 2210 | aborted_on_value)) |
| 2211 | self._write_status_comment('Job aborted by %s on %s' % |
| 2212 | (aborted_by_value, aborted_on_string)) |
jadmanski | c2ac77f | 2008-05-16 21:44:04 +0000 | [diff] [blame] | 2213 | |
| 2214 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2215 | def abort(self): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2216 | super(AbstractQueueTask, self).abort() |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2217 | self._log_abort() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2218 | self._finish_task() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2219 | |
| 2220 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2221 | def epilog(self): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2222 | super(AbstractQueueTask, self).epilog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2223 | self._finish_task() |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2224 | |
| 2225 | |
| 2226 | class QueueTask(AbstractQueueTask): |
| 2227 | def __init__(self, queue_entries): |
| 2228 | super(QueueTask, self).__init__(queue_entries) |
| 2229 | self._set_ids(queue_entries=queue_entries) |
| 2230 | |
| 2231 | |
| 2232 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2233 | self._check_queue_entry_statuses( |
| 2234 | self.queue_entries, |
| 2235 | allowed_hqe_statuses=(models.HostQueueEntry.Status.STARTING, |
| 2236 | models.HostQueueEntry.Status.RUNNING), |
| 2237 | allowed_host_statuses=(models.Host.Status.PENDING, |
| 2238 | models.Host.Status.RUNNING)) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2239 | |
| 2240 | super(QueueTask, self).prolog() |
| 2241 | |
| 2242 | for queue_entry in self.queue_entries: |
| 2243 | self._write_host_keyvals(queue_entry.host) |
| 2244 | queue_entry.host.set_status(models.Host.Status.RUNNING) |
| 2245 | queue_entry.host.update_field('dirty', 1) |
| 2246 | if self.job.synch_count == 1 and len(self.queue_entries) == 1: |
| 2247 | # TODO(gps): Remove this if nothing needs it anymore. |
| 2248 | # A potential user is: tko/parser |
| 2249 | self.job.write_to_machines_file(self.queue_entries[0]) |
| 2250 | |
| 2251 | |
| 2252 | def _finish_task(self): |
| 2253 | super(QueueTask, self)._finish_task() |
| 2254 | |
| 2255 | for queue_entry in self.queue_entries: |
| 2256 | queue_entry.set_status(models.HostQueueEntry.Status.GATHERING) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2257 | |
| 2258 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2259 | class HostlessQueueTask(AbstractQueueTask): |
| 2260 | def __init__(self, queue_entry): |
| 2261 | super(HostlessQueueTask, self).__init__([queue_entry]) |
| 2262 | self.queue_entry_ids = [queue_entry.id] |
| 2263 | |
| 2264 | |
| 2265 | def prolog(self): |
| 2266 | self.queue_entries[0].update_field('execution_subdir', 'hostless') |
| 2267 | super(HostlessQueueTask, self).prolog() |
| 2268 | |
| 2269 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2270 | def _finish_task(self): |
| 2271 | super(HostlessQueueTask, self)._finish_task() |
showard | cc92936 | 2010-01-25 21:20:41 +0000 | [diff] [blame] | 2272 | self.queue_entries[0].set_status(models.HostQueueEntry.Status.PARSING) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2273 | |
| 2274 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2275 | class PostJobTask(AgentTask): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2276 | def __init__(self, queue_entries, log_file_name): |
| 2277 | super(PostJobTask, self).__init__(log_file_name=log_file_name) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2278 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2279 | self.queue_entries = queue_entries |
| 2280 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2281 | self._autoserv_monitor = PidfileRunMonitor() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2282 | self._autoserv_monitor.attach_to_existing_process( |
| 2283 | self._working_directory()) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2284 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2285 | |
| 2286 | def _command_line(self): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2287 | if _testing_mode: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2288 | return 'true' |
| 2289 | return self._generate_command( |
| 2290 | _drone_manager.absolute_path(self._working_directory())) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2291 | |
| 2292 | |
| 2293 | def _generate_command(self, results_dir): |
| 2294 | raise NotImplementedError('Subclasses must override this') |
| 2295 | |
| 2296 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2297 | @property |
| 2298 | def owner_username(self): |
| 2299 | return self.queue_entries[0].job.owner |
| 2300 | |
| 2301 | |
| 2302 | def _working_directory(self): |
| 2303 | return self._get_consistent_execution_path(self.queue_entries) |
| 2304 | |
| 2305 | |
| 2306 | def _paired_with_monitor(self): |
| 2307 | return self._autoserv_monitor |
| 2308 | |
| 2309 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2310 | def _job_was_aborted(self): |
| 2311 | was_aborted = None |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2312 | for queue_entry in self.queue_entries: |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2313 | queue_entry.update_from_database() |
| 2314 | if was_aborted is None: # first queue entry |
| 2315 | was_aborted = bool(queue_entry.aborted) |
| 2316 | elif was_aborted != bool(queue_entry.aborted): # subsequent entries |
| 2317 | email_manager.manager.enqueue_notify_email( |
| 2318 | 'Inconsistent abort state', |
| 2319 | 'Queue entries have inconsistent abort state: ' + |
| 2320 | ', '.join('%s (%s)' % (queue_entry, queue_entry.aborted))) |
| 2321 | # don't crash here, just assume true |
| 2322 | return True |
| 2323 | return was_aborted |
| 2324 | |
| 2325 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2326 | def _final_status(self): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2327 | if self._job_was_aborted(): |
| 2328 | return models.HostQueueEntry.Status.ABORTED |
| 2329 | |
| 2330 | # we'll use a PidfileRunMonitor to read the autoserv exit status |
| 2331 | if self._autoserv_monitor.exit_code() == 0: |
| 2332 | return models.HostQueueEntry.Status.COMPLETED |
| 2333 | return models.HostQueueEntry.Status.FAILED |
| 2334 | |
| 2335 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2336 | def _set_all_statuses(self, status): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2337 | for queue_entry in self.queue_entries: |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2338 | queue_entry.set_status(status) |
| 2339 | |
| 2340 | |
| 2341 | def abort(self): |
| 2342 | # override AgentTask.abort() to avoid killing the process and ending |
| 2343 | # the task. post-job tasks continue when the job is aborted. |
| 2344 | pass |
| 2345 | |
| 2346 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2347 | def _pidfile_label(self): |
| 2348 | # '.autoserv_execute' -> 'autoserv' |
| 2349 | return self._pidfile_name()[1:-len('_execute')] |
| 2350 | |
| 2351 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2352 | class GatherLogsTask(PostJobTask): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2353 | """ |
| 2354 | Task responsible for |
| 2355 | * gathering uncollected logs (if Autoserv crashed hard or was killed) |
| 2356 | * copying logs to the results repository |
| 2357 | * spawning CleanupTasks for hosts, if necessary |
| 2358 | * spawning a FinalReparseTask for the job |
| 2359 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2360 | def __init__(self, queue_entries, recover_run_monitor=None): |
| 2361 | self._job = queue_entries[0].job |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2362 | super(GatherLogsTask, self).__init__( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2363 | queue_entries, log_file_name='.collect_crashinfo.log') |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2364 | self._set_ids(queue_entries=queue_entries) |
| 2365 | |
| 2366 | |
| 2367 | def _generate_command(self, results_dir): |
| 2368 | host_list = ','.join(queue_entry.host.hostname |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2369 | for queue_entry in self.queue_entries) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2370 | return [_autoserv_path , '-p', |
| 2371 | '--pidfile-label=%s' % self._pidfile_label(), |
| 2372 | '--use-existing-results', '--collect-crashinfo', |
| 2373 | '-m', host_list, '-r', results_dir] |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2374 | |
| 2375 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2376 | @property |
| 2377 | def num_processes(self): |
| 2378 | return len(self.queue_entries) |
| 2379 | |
| 2380 | |
| 2381 | def _pidfile_name(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 2382 | return drone_manager.CRASHINFO_PID_FILE |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2383 | |
| 2384 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2385 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2386 | self._check_queue_entry_statuses( |
| 2387 | self.queue_entries, |
| 2388 | allowed_hqe_statuses=(models.HostQueueEntry.Status.GATHERING,), |
| 2389 | allowed_host_statuses=(models.Host.Status.RUNNING,)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2390 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2391 | super(GatherLogsTask, self).prolog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2392 | |
| 2393 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2394 | def epilog(self): |
| 2395 | super(GatherLogsTask, self).epilog() |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2396 | self._parse_results(self.queue_entries) |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2397 | self._reboot_hosts() |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 2398 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2399 | |
| 2400 | def _reboot_hosts(self): |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 2401 | if self._autoserv_monitor.has_process(): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2402 | final_success = (self._final_status() == |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 2403 | models.HostQueueEntry.Status.COMPLETED) |
| 2404 | num_tests_failed = self._autoserv_monitor.num_tests_failed() |
| 2405 | else: |
| 2406 | final_success = False |
| 2407 | num_tests_failed = 0 |
| 2408 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2409 | reboot_after = self._job.reboot_after |
| 2410 | do_reboot = ( |
| 2411 | # always reboot after aborted jobs |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2412 | self._final_status() == models.HostQueueEntry.Status.ABORTED |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 2413 | or reboot_after == model_attributes.RebootAfter.ALWAYS |
| 2414 | or (reboot_after == model_attributes.RebootAfter.IF_ALL_TESTS_PASSED |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2415 | and final_success and num_tests_failed == 0)) |
| 2416 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2417 | for queue_entry in self.queue_entries: |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2418 | if do_reboot: |
| 2419 | # don't pass the queue entry to the CleanupTask. if the cleanup |
| 2420 | # fails, the job doesn't care -- it's over. |
| 2421 | models.SpecialTask.objects.create( |
| 2422 | host=models.Host.objects.get(id=queue_entry.host.id), |
| 2423 | task=models.SpecialTask.Task.CLEANUP, |
| 2424 | requested_by=self._job.owner_model()) |
| 2425 | else: |
| 2426 | queue_entry.host.set_status(models.Host.Status.READY) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2427 | |
| 2428 | |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 2429 | def run(self): |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 2430 | autoserv_exit_code = self._autoserv_monitor.exit_code() |
| 2431 | # only run if Autoserv exited due to some signal. if we have no exit |
| 2432 | # code, assume something bad (and signal-like) happened. |
| 2433 | if autoserv_exit_code is None or os.WIFSIGNALED(autoserv_exit_code): |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 2434 | super(GatherLogsTask, self).run() |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 2435 | else: |
| 2436 | self.finished(True) |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 2437 | |
| 2438 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2439 | class SelfThrottledPostJobTask(PostJobTask): |
| 2440 | """ |
| 2441 | Special AgentTask subclass that maintains its own global process limit. |
| 2442 | """ |
| 2443 | _num_running_processes = 0 |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2444 | |
| 2445 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2446 | @classmethod |
| 2447 | def _increment_running_processes(cls): |
| 2448 | cls._num_running_processes += 1 |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 2449 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2450 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2451 | @classmethod |
| 2452 | def _decrement_running_processes(cls): |
| 2453 | cls._num_running_processes -= 1 |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2454 | |
| 2455 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2456 | @classmethod |
| 2457 | def _max_processes(cls): |
| 2458 | raise NotImplementedError |
| 2459 | |
| 2460 | |
| 2461 | @classmethod |
| 2462 | def _can_run_new_process(cls): |
| 2463 | return cls._num_running_processes < cls._max_processes() |
| 2464 | |
| 2465 | |
| 2466 | def _process_started(self): |
| 2467 | return bool(self.monitor) |
| 2468 | |
| 2469 | |
| 2470 | def tick(self): |
| 2471 | # override tick to keep trying to start until the process count goes |
| 2472 | # down and we can, at which point we revert to default behavior |
| 2473 | if self._process_started(): |
| 2474 | super(SelfThrottledPostJobTask, self).tick() |
| 2475 | else: |
| 2476 | self._try_starting_process() |
| 2477 | |
| 2478 | |
| 2479 | def run(self): |
| 2480 | # override run() to not actually run unless we can |
| 2481 | self._try_starting_process() |
| 2482 | |
| 2483 | |
| 2484 | def _try_starting_process(self): |
| 2485 | if not self._can_run_new_process(): |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 2486 | return |
| 2487 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2488 | # actually run the command |
| 2489 | super(SelfThrottledPostJobTask, self).run() |
| 2490 | self._increment_running_processes() |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2491 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2492 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2493 | def finished(self, success): |
| 2494 | super(SelfThrottledPostJobTask, self).finished(success) |
| 2495 | if self._process_started(): |
| 2496 | self._decrement_running_processes() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2497 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2498 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2499 | class FinalReparseTask(SelfThrottledPostJobTask): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2500 | def __init__(self, queue_entries): |
| 2501 | super(FinalReparseTask, self).__init__(queue_entries, |
| 2502 | log_file_name='.parse.log') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2503 | # don't use _set_ids, since we don't want to set the host_ids |
| 2504 | self.queue_entry_ids = [entry.id for entry in queue_entries] |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2505 | |
| 2506 | |
| 2507 | def _generate_command(self, results_dir): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2508 | return [_parser_path, '--write-pidfile', '-l', '2', '-r', '-o', |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2509 | results_dir] |
| 2510 | |
| 2511 | |
| 2512 | @property |
| 2513 | def num_processes(self): |
| 2514 | return 0 # don't include parser processes in accounting |
| 2515 | |
| 2516 | |
| 2517 | def _pidfile_name(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 2518 | return drone_manager.PARSER_PID_FILE |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2519 | |
| 2520 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2521 | @classmethod |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2522 | def _max_processes(cls): |
| 2523 | return scheduler_config.config.max_parse_processes |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2524 | |
| 2525 | |
| 2526 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2527 | self._check_queue_entry_statuses( |
| 2528 | self.queue_entries, |
| 2529 | allowed_hqe_statuses=(models.HostQueueEntry.Status.PARSING,)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2530 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2531 | super(FinalReparseTask, self).prolog() |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2532 | |
| 2533 | |
| 2534 | def epilog(self): |
| 2535 | super(FinalReparseTask, self).epilog() |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2536 | self._archive_results(self.queue_entries) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2537 | |
| 2538 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2539 | class ArchiveResultsTask(SelfThrottledPostJobTask): |
showard | e1575b5 | 2010-01-15 00:21:12 +0000 | [diff] [blame] | 2540 | _ARCHIVING_FAILED_FILE = '.archiver_failed' |
| 2541 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2542 | def __init__(self, queue_entries): |
| 2543 | super(ArchiveResultsTask, self).__init__(queue_entries, |
| 2544 | log_file_name='.archiving.log') |
| 2545 | # don't use _set_ids, since we don't want to set the host_ids |
| 2546 | self.queue_entry_ids = [entry.id for entry in queue_entries] |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2547 | |
| 2548 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2549 | def _pidfile_name(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 2550 | return drone_manager.ARCHIVER_PID_FILE |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2551 | |
| 2552 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2553 | def _generate_command(self, results_dir): |
| 2554 | return [_autoserv_path , '-p', |
| 2555 | '--pidfile-label=%s' % self._pidfile_label(), '-r', results_dir, |
mbligh | e0cbc91 | 2010-03-11 18:03:07 +0000 | [diff] [blame] | 2556 | '--use-existing-results', '--control-filename=control.archive', |
showard | 948eb30 | 2010-01-15 00:16:20 +0000 | [diff] [blame] | 2557 | os.path.join(drones.AUTOTEST_INSTALL_DIR, 'scheduler', |
| 2558 | 'archive_results.control.srv')] |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2559 | |
| 2560 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2561 | @classmethod |
| 2562 | def _max_processes(cls): |
| 2563 | return scheduler_config.config.max_transfer_processes |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2564 | |
| 2565 | |
| 2566 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2567 | self._check_queue_entry_statuses( |
| 2568 | self.queue_entries, |
| 2569 | allowed_hqe_statuses=(models.HostQueueEntry.Status.ARCHIVING,)) |
| 2570 | |
| 2571 | super(ArchiveResultsTask, self).prolog() |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2572 | |
| 2573 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2574 | def epilog(self): |
| 2575 | super(ArchiveResultsTask, self).epilog() |
showard | 4076c63 | 2010-01-15 20:28:49 +0000 | [diff] [blame] | 2576 | if not self.success and self._paired_with_monitor().has_process(): |
showard | e1575b5 | 2010-01-15 00:21:12 +0000 | [diff] [blame] | 2577 | failed_file = os.path.join(self._working_directory(), |
| 2578 | self._ARCHIVING_FAILED_FILE) |
| 2579 | paired_process = self._paired_with_monitor().get_process() |
| 2580 | _drone_manager.write_lines_to_file( |
| 2581 | failed_file, ['Archiving failed with exit code %s' |
| 2582 | % self.monitor.exit_code()], |
| 2583 | paired_with_process=paired_process) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2584 | self._set_all_statuses(self._final_status()) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2585 | |
| 2586 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2587 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2588 | main() |