def test_deps_load(name): """Test load().""" with mock.patch('importlib.import_module', autospec=True) \ as import_module, \ mock.patch.object(autotest, '_setup_done', True): autotest.deps_load(name) import_module.assert_called_once_with(name)
def _retry_db_errors(func): """Call func, retrying multiple times if database errors are raised. crbug.com/863504 """ django = autotest.deps_load('django') MySQLdb = autotest.deps_load('MySQLdb') max_retries = 10 # n ... 0 means n + 1 tries, or 1 try plus n retries for i in xrange(max_retries, -1, -1): try: func() except (django.db.utils.DatabaseError, MySQLdb.OperationalError) as e: if i == 0: raise logger.debug('Got database error %s, retrying', e) django.db.close_connection() time.sleep(5) else: break
def _clean_up_hosts(host_ids): models = autotest.load('frontend.afe.models') transaction = autotest.deps_load('django.db.transaction') with transaction.commit_on_success(): active_hosts = { id for id in (models.HostQueueEntry.objects.filter( active=True, complete=False).values_list('host_id', flat=True)) if id is not None } logger.debug('Found active Hosts: %r', active_hosts) (models.Host.objects.filter(id__in=host_ids).exclude( id__in=active_hosts).update(status=models.Host.Status.READY))
def _main_loop(jobdir): transaction = autotest.deps_load('django.db.transaction') @transaction.commit_manually def flush_transaction(): """Flush transaction https://stackoverflow.com/questions/3346124/""" transaction.commit() metrics = _Metrics() metrics.send_starting() while True: logger.debug('Tick') metrics.send_tick() _main_loop_body(metrics, jobdir) flush_transaction() time.sleep(20)
def send_hqe_duration(self, hqe): """Send CloudTrace metrics for HQE duration.""" if not (hqe.started_on and hqe.finished_on): return scheduler_models = autotest.load('scheduler.scheduler_models') cloud_trace = autotest.chromite_load('cloud_trace') types = autotest.deps_load('google.protobuf.internal.well_known_types') hqe_trace_id = scheduler_models.hqe_trace_id span = cloud_trace.Span( 'HQE', spanId='0', traceId=hqe_trace_id(hqe.id)) span.startTime = types.Timestamp() span.startTime.FromDatetime(hqe.started_on) span.endTime = types.Timestamp() span.endTime.FromDatetime(hqe.finished_on) cloud_trace.LogSpan(span)
def incomplete(): """Return a QuerySet of incomplete JobHandoffs. JobHandoff created within a cutoff period are exempt to allow the job the chance to acquire its lease file; otherwise, incomplete jobs without an active lease are considered dead. @returns: Django QuerySet """ models = autotest.load('frontend.afe.models') Q = autotest.deps_load('django.db.models').Q # Time ---*---------|---------*-------|---> # incomplete cutoff newborn now cutoff = (datetime.datetime.now() - datetime.timedelta(seconds=_JOB_GRACE_SECS)) return (models.JobHandoff.objects .filter(completed=False, created__lt=cutoff) .filter(Q(drone=socket.gethostname()) | Q(drone=None)))
def _mark_hqe_aborted(hqe): """Perform Autotest operations needed for HQE abortion. This also operates on the HQE's host, so prefetch it when possible. This logic is from scheduler_models.HostQueueEntry.abort(). """ models = autotest.load('frontend.afe.models') transaction = autotest.deps_load('django.db.transaction') Status = models.HostQueueEntry.Status with transaction.commit_on_success(): if hqe.status in (Status.GATHERING, Status.PARSING): return if hqe.status in (Status.STARTING, Status.PENDING, Status.RUNNING): if hqe.host is None: return hqe.host.status = models.Host.Status.READY hqe.host.save(update_fields=['status']) hqe.status = Status.ABORTED hqe.save(update_fields=['status'])