blob: ffab5a1fe524630cf8e4d3cfe35fe57978d35cd9 [file] [log] [blame]
Yu-Ju Hong52ce11d2012-08-01 17:55:48 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Dale Curtise5436f32011-03-31 14:10:37 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
Simran Basia858a232012-08-21 11:04:37 -07006import logging
Dale Curtise5436f32011-03-31 14:10:37 -07007
Yu-Ju Hong52ce11d2012-08-01 17:55:48 -07008from autotest_lib.client.common_lib import global_config
9from autotest_lib.frontend.afe import models
Simran Basibf2e21f2012-10-16 11:36:52 -070010from autotest_lib.scheduler import email_manager
Simran Basia858a232012-08-21 11:04:37 -070011from autotest_lib.scheduler import scheduler_config, scheduler_models
Dale Curtise5436f32011-03-31 14:10:37 -070012
13# Override default parser with our site parser.
14def parser_path(install_dir):
15 return os.path.join(install_dir, 'tko', 'site_parse')
Yu-Ju Hong52ce11d2012-08-01 17:55:48 -070016
17
18class SiteAgentTask(object):
19 """
20 SiteAgentTask subclasses BaseAgentTask in monitor_db.
21 """
22
23
24 def _archive_results(self, queue_entries):
25 """
26 Set the status of queue_entries to ARCHIVING.
27
28 This method sets the status of the queue_entries to ARCHIVING
29 if the enable_archiving flag is true in global_config.ini.
30 Otherwise, it bypasses the archiving step and sets the queue entries
31 to the final status of current step.
32 """
33 enable_archiving = global_config.global_config.get_config_value(
34 scheduler_config.CONFIG_SECTION, 'enable_archiving', type=bool)
35 # Set the status of the queue entries to archiving or self final status
36 if enable_archiving:
37 status = models.HostQueueEntry.Status.ARCHIVING
38 else:
39 status = self._final_status()
40
41 for queue_entry in self.queue_entries:
42 queue_entry.set_status(status)
Simran Basia858a232012-08-21 11:04:37 -070043
44
Simran Basibf2e21f2012-10-16 11:36:52 -070045 def _check_queue_entry_statuses(self, queue_entries, allowed_hqe_statuses,
46 allowed_host_statuses=None):
47 """
48 Forked from monitor_db.py
49 """
50 class_name = self.__class__.__name__
51 for entry in queue_entries:
52 if entry.status not in allowed_hqe_statuses:
53 # In the orignal code, here we raise an exception. In an
54 # effort to prevent downtime we will instead abort the job and
55 # send out an email notifying us this has occured.
56 error_message = ('%s attempting to start entry with invalid '
57 'status %s: %s. Aborting Job: %s.'
58 % (class_name, entry.status, entry,
59 entry.job))
60 logging.error(error_message)
61 email_manager.manager.enqueue_notify_email(
62 'Job Aborted - Invalid Host Queue Entry Status',
63 error_message)
64 entry.job.request_abort()
65 invalid_host_status = (
66 allowed_host_statuses is not None
67 and entry.host.status not in allowed_host_statuses)
68 if invalid_host_status:
69 # In the orignal code, here we raise an exception. In an
70 # effort to prevent downtime we will instead abort the job and
71 # send out an email notifying us this has occured.
72 error_message = ('%s attempting to start on queue entry with '
73 'invalid host status %s: %s. Aborting Job: %s'
74 % (class_name, entry.host.status, entry,
75 entry.job))
76 logging.error(error_message)
77 email_manager.manager.enqueue_notify_email(
78 'Job Aborted - Invalid Host Status', error_message)
79 entry.job.request_abort()
80
81
Simran Basia858a232012-08-21 11:04:37 -070082class SiteDispatcher(object):
83 """
84 SiteDispatcher subclasses BaseDispatcher in monitor_db.
85 """
86 DEFAULT_REQUESTED_BY_USER_ID = 1
87
88
89 def _reverify_hosts_where(self, where,
90 print_message='Reverifying host %s'):
91 """
92 This is an altered version of _reverify_hosts_where the class to
93 models.SpecialTask.objects.create passes in an argument for
94 requested_by, in order to allow the Cleanup task to be created
95 properly.
96 """
97 full_where='locked = 0 AND invalid = 0 AND ' + where
98 for host in scheduler_models.Host.fetch(where=full_where):
99 if self.host_has_agent(host):
100 # host has already been recovered in some way
101 continue
102 if self._host_has_scheduled_special_task(host):
103 # host will have a special task scheduled on the next cycle
104 continue
105 if print_message:
106 logging.error(print_message, host.hostname)
107 try:
108 user = models.User.objects.get(login='autotest_system')
109 except models.User.DoesNotExist:
110 user = models.User.objects.get(
111 id=SiteDispatcher.DEFAULT_REQUESTED_BY_USER_ID)
112 models.SpecialTask.objects.create(
113 task=models.SpecialTask.Task.CLEANUP,
114 host=models.Host.objects.get(id=host.id),
Simran Basi3d899732012-09-07 15:46:35 -0700115 requested_by=user)
116
117
118 def _check_for_unrecovered_verifying_entries(self):
119 queue_entries = scheduler_models.HostQueueEntry.fetch(
120 where='status = "%s"' % models.HostQueueEntry.Status.VERIFYING)
121 for queue_entry in queue_entries:
122 special_tasks = models.SpecialTask.objects.filter(
123 task__in=(models.SpecialTask.Task.CLEANUP,
124 models.SpecialTask.Task.VERIFY),
125 queue_entry__id=queue_entry.id,
126 is_complete=False)
127 if special_tasks.count() == 0:
128 logging.error('Unrecovered Verifying host queue entry: %s. '
129 'Setting status to Queued.', str(queue_entry))
130 # Essentially this host queue entry was set to be Verifying
131 # however no special task exists for entry. This occurs if the
132 # scheduler dies between changing the status and creating the
133 # special task. By setting it to queued, the job can restart
134 # from the beginning and proceed correctly. This is much more
135 # preferable than having monitor_db not launching.
136 queue_entry.set_status('Queued')