blob: 2b0fa83f51d55b9abf47ffb5b68ec44416e4ec64 [file] [log] [blame]
mblighe8819cd2008-02-15 16:48:40 +00001"""\
2Utility functions for rpc_interface.py. We keep them in a separate file so that
3only RPC interface functions go into that file.
4"""
5
6__author__ = 'showard@google.com (Steve Howard)'
7
showard14374b12009-01-31 00:11:54 +00008import datetime, os
showard3d6ae112009-05-02 00:45:48 +00009import django.http
showarda5288b42009-07-28 20:06:08 +000010from autotest_lib.frontend.afe import models, model_logic
mblighe8819cd2008-02-15 16:48:40 +000011
showarda62866b2008-07-28 21:27:41 +000012NULL_DATETIME = datetime.datetime.max
13NULL_DATE = datetime.date.max
14
mblighe8819cd2008-02-15 16:48:40 +000015def prepare_for_serialization(objects):
jadmanski0afbb632008-06-06 21:10:57 +000016 """
17 Prepare Python objects to be returned via RPC.
18 """
19 if (isinstance(objects, list) and len(objects) and
20 isinstance(objects[0], dict) and 'id' in objects[0]):
21 objects = gather_unique_dicts(objects)
22 return _prepare_data(objects)
showardb8d34242008-04-25 18:11:16 +000023
24
showardc92da832009-04-07 18:14:34 +000025def prepare_rows_as_nested_dicts(query, nested_dict_column_names):
26 """
27 Prepare a Django query to be returned via RPC as a sequence of nested
28 dictionaries.
29
30 @param query - A Django model query object with a select_related() method.
31 @param nested_dict_column_names - A list of column/attribute names for the
32 rows returned by query to expand into nested dictionaries using
33 their get_object_dict() method when not None.
34
35 @returns An list suitable to returned in an RPC.
36 """
37 all_dicts = []
38 for row in query.select_related():
39 row_dict = row.get_object_dict()
40 for column in nested_dict_column_names:
41 if row_dict[column] is not None:
42 row_dict[column] = getattr(row, column).get_object_dict()
43 all_dicts.append(row_dict)
44 return prepare_for_serialization(all_dicts)
45
46
showardb8d34242008-04-25 18:11:16 +000047def _prepare_data(data):
jadmanski0afbb632008-06-06 21:10:57 +000048 """
49 Recursively process data structures, performing necessary type
50 conversions to values in data to allow for RPC serialization:
51 -convert datetimes to strings
showard2b9a88b2008-06-13 20:55:03 +000052 -convert tuples and sets to lists
jadmanski0afbb632008-06-06 21:10:57 +000053 """
54 if isinstance(data, dict):
55 new_data = {}
56 for key, value in data.iteritems():
57 new_data[key] = _prepare_data(value)
58 return new_data
showard2b9a88b2008-06-13 20:55:03 +000059 elif (isinstance(data, list) or isinstance(data, tuple) or
60 isinstance(data, set)):
jadmanski0afbb632008-06-06 21:10:57 +000061 return [_prepare_data(item) for item in data]
showard98659972008-07-17 17:00:07 +000062 elif isinstance(data, datetime.date):
showarda62866b2008-07-28 21:27:41 +000063 if data is NULL_DATETIME or data is NULL_DATE:
64 return None
jadmanski0afbb632008-06-06 21:10:57 +000065 return str(data)
66 else:
67 return data
mblighe8819cd2008-02-15 16:48:40 +000068
69
showard3d6ae112009-05-02 00:45:48 +000070def raw_http_response(response_data, content_type=None):
71 response = django.http.HttpResponse(response_data, mimetype=content_type)
72 response['Content-length'] = str(len(response.content))
73 return response
74
75
showardb0dfb9f2008-06-06 18:08:02 +000076def gather_unique_dicts(dict_iterable):
jadmanski0afbb632008-06-06 21:10:57 +000077 """\
78 Pick out unique objects (by ID) from an iterable of object dicts.
79 """
80 id_set = set()
81 result = []
82 for obj in dict_iterable:
83 if obj['id'] not in id_set:
84 id_set.add(obj['id'])
85 result.append(obj)
86 return result
showardb0dfb9f2008-06-06 18:08:02 +000087
88
mblighe8819cd2008-02-15 16:48:40 +000089def extra_job_filters(not_yet_run=False, running=False, finished=False):
jadmanski0afbb632008-06-06 21:10:57 +000090 """\
91 Generate a SQL WHERE clause for job status filtering, and return it in
92 a dict of keyword args to pass to query.extra(). No more than one of
93 the parameters should be passed as True.
94 """
95 assert not ((not_yet_run and running) or
96 (not_yet_run and finished) or
97 (running and finished)), ('Cannot specify more than one '
98 'filter to this function')
99 if not_yet_run:
100 where = ['id NOT IN (SELECT job_id FROM host_queue_entries '
101 'WHERE active OR complete)']
102 elif running:
103 where = ['(id IN (SELECT job_id FROM host_queue_entries '
104 'WHERE active OR complete)) AND '
105 '(id IN (SELECT job_id FROM host_queue_entries '
106 'WHERE not complete OR active))']
107 elif finished:
108 where = ['id NOT IN (SELECT job_id FROM host_queue_entries '
109 'WHERE not complete OR active)']
110 else:
showard10f41672009-05-13 21:28:25 +0000111 return {}
jadmanski0afbb632008-06-06 21:10:57 +0000112 return {'where': where}
mblighe8819cd2008-02-15 16:48:40 +0000113
114
showard87cc38f2009-08-20 23:37:04 +0000115def extra_host_filters(multiple_labels=()):
jadmanski0afbb632008-06-06 21:10:57 +0000116 """\
117 Generate SQL WHERE clauses for matching hosts in an intersection of
118 labels.
119 """
120 extra_args = {}
121 where_str = ('hosts.id in (select host_id from hosts_labels '
122 'where label_id=%s)')
123 extra_args['where'] = [where_str] * len(multiple_labels)
124 extra_args['params'] = [models.Label.smart_get(label).id
125 for label in multiple_labels]
126 return extra_args
showard8e3aa5e2008-04-08 19:42:32 +0000127
128
showard87cc38f2009-08-20 23:37:04 +0000129def get_host_query(multiple_labels, exclude_only_if_needed_labels,
130 exclude_atomic_group_hosts, filter_data):
showard43a3d262008-11-12 18:17:05 +0000131 query = models.Host.valid_objects.all()
132 if exclude_only_if_needed_labels:
133 only_if_needed_labels = models.Label.valid_objects.filter(
134 only_if_needed=True)
showardf7eac6f2008-11-13 21:18:01 +0000135 if only_if_needed_labels.count() > 0:
showard87cc38f2009-08-20 23:37:04 +0000136 only_if_needed_ids = ','.join(
137 str(label['id'])
138 for label in only_if_needed_labels.values('id'))
showardf7eac6f2008-11-13 21:18:01 +0000139 query = models.Host.objects.add_join(
140 query, 'hosts_labels', join_key='host_id',
showard87cc38f2009-08-20 23:37:04 +0000141 join_condition=('hosts_labels_exclude_OIN.label_id IN (%s)'
142 % only_if_needed_ids),
143 suffix='_exclude_OIN', exclude=True)
144 if exclude_atomic_group_hosts:
145 atomic_group_labels = models.Label.valid_objects.filter(
146 atomic_group__isnull=False)
147 if atomic_group_labels.count() > 0:
148 atomic_group_label_ids = ','.join(
149 str(atomic_group['id'])
150 for atomic_group in atomic_group_labels.values('id'))
151 query = models.Host.objects.add_join(
152 query, 'hosts_labels', join_key='host_id',
153 join_condition=('hosts_labels_exclude_AG.label_id IN (%s)'
154 % atomic_group_label_ids),
155 suffix='_exclude_AG', exclude=True)
showard43a3d262008-11-12 18:17:05 +0000156 filter_data['extra_args'] = (extra_host_filters(multiple_labels))
157 return models.Host.query_objects(filter_data, initial_query=query)
158
159
showard8fd58242008-03-10 21:29:07 +0000160class InconsistencyException(Exception):
jadmanski0afbb632008-06-06 21:10:57 +0000161 'Raised when a list of objects does not have a consistent value'
showard8fd58242008-03-10 21:29:07 +0000162
163
164def get_consistent_value(objects, field):
mblighc5ddfd12008-08-04 17:15:00 +0000165 if not objects:
166 # well a list of nothing is consistent
167 return None
168
jadmanski0afbb632008-06-06 21:10:57 +0000169 value = getattr(objects[0], field)
170 for obj in objects:
171 this_value = getattr(obj, field)
172 if this_value != value:
173 raise InconsistencyException(objects[0], obj)
174 return value
showard8fd58242008-03-10 21:29:07 +0000175
176
showard2b9a88b2008-06-13 20:55:03 +0000177def prepare_generate_control_file(tests, kernel, label, profilers):
jadmanski0afbb632008-06-06 21:10:57 +0000178 test_objects = [models.Test.smart_get(test) for test in tests]
showard2b9a88b2008-06-13 20:55:03 +0000179 profiler_objects = [models.Profiler.smart_get(profiler)
180 for profiler in profilers]
jadmanski0afbb632008-06-06 21:10:57 +0000181 # ensure tests are all the same type
182 try:
183 test_type = get_consistent_value(test_objects, 'test_type')
184 except InconsistencyException, exc:
185 test1, test2 = exc.args
mblighec5546d2008-06-16 16:51:28 +0000186 raise model_logic.ValidationError(
jadmanski0afbb632008-06-06 21:10:57 +0000187 {'tests' : 'You cannot run both server- and client-side '
188 'tests together (tests %s and %s differ' % (
189 test1.name, test2.name)})
showard8fd58242008-03-10 21:29:07 +0000190
jadmanski0afbb632008-06-06 21:10:57 +0000191 is_server = (test_type == models.Test.Types.SERVER)
showard14374b12009-01-31 00:11:54 +0000192 if test_objects:
193 synch_count = max(test.sync_count for test in test_objects)
194 else:
195 synch_count = 1
jadmanski0afbb632008-06-06 21:10:57 +0000196 if label:
197 label = models.Label.smart_get(label)
mblighe8819cd2008-02-15 16:48:40 +0000198
showard989f25d2008-10-01 11:38:11 +0000199 dependencies = set(label.name for label
200 in models.Label.objects.filter(test__in=test_objects))
201
showard2bab8f42008-11-12 18:15:22 +0000202 cf_info = dict(is_server=is_server, synch_count=synch_count,
203 dependencies=list(dependencies))
204 return cf_info, test_objects, profiler_objects, label
showard989f25d2008-10-01 11:38:11 +0000205
206
207def check_job_dependencies(host_objects, job_dependencies):
208 """
209 Check that a set of machines satisfies a job's dependencies.
210 host_objects: list of models.Host objects
211 job_dependencies: list of names of labels
212 """
213 # check that hosts satisfy dependencies
214 host_ids = [host.id for host in host_objects]
215 hosts_in_job = models.Host.objects.filter(id__in=host_ids)
216 ok_hosts = hosts_in_job
217 for index, dependency in enumerate(job_dependencies):
showarda5288b42009-07-28 20:06:08 +0000218 ok_hosts = ok_hosts.filter(labels__name=dependency)
showard989f25d2008-10-01 11:38:11 +0000219 failing_hosts = (set(host.hostname for host in host_objects) -
220 set(host.hostname for host in ok_hosts))
221 if failing_hosts:
222 raise model_logic.ValidationError(
223 {'hosts' : 'Host(s) failed to meet job dependencies: ' +
224 ', '.join(failing_hosts)})
225
showard2bab8f42008-11-12 18:15:22 +0000226
227def _execution_key_for(host_queue_entry):
228 return (host_queue_entry.job.id, host_queue_entry.execution_subdir)
229
230
231def check_abort_synchronous_jobs(host_queue_entries):
232 # ensure user isn't aborting part of a synchronous autoserv execution
233 count_per_execution = {}
234 for queue_entry in host_queue_entries:
235 key = _execution_key_for(queue_entry)
236 count_per_execution.setdefault(key, 0)
237 count_per_execution[key] += 1
238
239 for queue_entry in host_queue_entries:
240 if not queue_entry.execution_subdir:
241 continue
242 execution_count = count_per_execution[_execution_key_for(queue_entry)]
243 if execution_count < queue_entry.job.synch_count:
mbligh1ef218d2009-08-03 16:57:56 +0000244 raise model_logic.ValidationError(
245 {'' : 'You cannot abort part of a synchronous job execution '
246 '(%d/%s), %d included, %d expected'
247 % (queue_entry.job.id, queue_entry.execution_subdir,
248 execution_count, queue_entry.job.synch_count)})
showard8fbae652009-01-20 23:23:10 +0000249
250
showardc92da832009-04-07 18:14:34 +0000251def check_atomic_group_create_job(synch_count, host_objects, metahost_objects,
252 dependencies, atomic_group, labels_by_name):
253 """
254 Attempt to reject create_job requests with an atomic group that
255 will be impossible to schedule. The checks are not perfect but
256 should catch the most obvious issues.
257
258 @param synch_count - The job's minimum synch count.
259 @param host_objects - A list of models.Host instances.
260 @param metahost_objects - A list of models.Label instances.
261 @param dependencies - A list of job dependency label names.
262 @param atomic_group - The models.AtomicGroup instance.
263 @param labels_by_name - A dictionary mapping label names to models.Label
264 instance. Used to look up instances for dependencies.
265
266 @raises model_logic.ValidationError - When an issue is found.
267 """
268 # If specific host objects were supplied with an atomic group, verify
269 # that there are enough to satisfy the synch_count.
270 minimum_required = synch_count or 1
271 if (host_objects and not metahost_objects and
272 len(host_objects) < minimum_required):
273 raise model_logic.ValidationError(
274 {'hosts':
275 'only %d hosts provided for job with synch_count = %d' %
276 (len(host_objects), synch_count)})
277
278 # Check that the atomic group has a hope of running this job
279 # given any supplied metahosts and dependancies that may limit.
280
281 # Get a set of hostnames in the atomic group.
282 possible_hosts = set()
283 for label in atomic_group.label_set.all():
284 possible_hosts.update(h.hostname for h in label.host_set.all())
285
286 # Filter out hosts that don't match all of the job dependency labels.
287 for label_name in set(dependencies):
288 label = labels_by_name[label_name]
289 hosts_in_label = (h.hostname for h in label.host_set.all())
290 possible_hosts.intersection_update(hosts_in_label)
291
showard225bdc12009-04-13 16:09:21 +0000292 if not host_objects and not metahost_objects:
293 # No hosts or metahosts are required to queue an atomic group Job.
294 # However, if they are given, we respect them below.
295 host_set = possible_hosts
296 else:
297 host_set = set(host.hostname for host in host_objects)
298 unusable_host_set = host_set.difference(possible_hosts)
299 if unusable_host_set:
300 raise model_logic.ValidationError(
301 {'hosts': 'Hosts "%s" are not in Atomic Group "%s"' %
302 (', '.join(sorted(unusable_host_set)), atomic_group.name)})
showardc92da832009-04-07 18:14:34 +0000303
304 # Lookup hosts provided by each meta host and merge them into the
305 # host_set for final counting.
306 for meta_host in metahost_objects:
307 meta_possible = possible_hosts.copy()
308 hosts_in_meta_host = (h.hostname for h in meta_host.host_set.all())
309 meta_possible.intersection_update(hosts_in_meta_host)
310
311 # Count all hosts that this meta_host will provide.
312 host_set.update(meta_possible)
313
314 if len(host_set) < minimum_required:
315 raise model_logic.ValidationError(
316 {'atomic_group_name':
317 'Insufficient hosts in Atomic Group "%s" with the'
318 ' supplied dependencies and meta_hosts.' %
319 (atomic_group.name,)})
320
321
showard8fbae652009-01-20 23:23:10 +0000322def get_motd():
323 dirname = os.path.dirname(__file__)
324 filename = os.path.join(dirname, "..", "..", "motd.txt")
325 text = ''
326 try:
327 fp = open(filename, "r")
328 try:
329 text = fp.read()
330 finally:
331 fp.close()
332 except:
333 pass
334
335 return text
showard29f7cd22009-04-29 21:16:24 +0000336
337
338def _get_metahost_counts(metahost_objects):
339 metahost_counts = {}
340 for metahost in metahost_objects:
341 metahost_counts.setdefault(metahost, 0)
342 metahost_counts[metahost] += 1
343 return metahost_counts
344
345
showarda965cef2009-05-15 23:17:41 +0000346def get_job_info(job, preserve_metahosts=False, queue_entry_filter_data=None):
showard29f7cd22009-04-29 21:16:24 +0000347 hosts = []
348 one_time_hosts = []
349 meta_hosts = []
350 atomic_group = None
351
showard4d077562009-05-08 18:24:36 +0000352 queue_entries = job.hostqueueentry_set.all()
showarda965cef2009-05-15 23:17:41 +0000353 if queue_entry_filter_data:
354 queue_entries = models.HostQueueEntry.query_objects(
355 queue_entry_filter_data, initial_query=queue_entries)
showard4d077562009-05-08 18:24:36 +0000356
357 for queue_entry in queue_entries:
showard29f7cd22009-04-29 21:16:24 +0000358 if (queue_entry.host and (preserve_metahosts or
359 not queue_entry.meta_host)):
360 if queue_entry.deleted:
361 continue
362 if queue_entry.host.invalid:
363 one_time_hosts.append(queue_entry.host)
364 else:
365 hosts.append(queue_entry.host)
366 else:
367 meta_hosts.append(queue_entry.meta_host)
368 if atomic_group is None:
369 if queue_entry.atomic_group is not None:
370 atomic_group = queue_entry.atomic_group
371 else:
372 assert atomic_group.name == queue_entry.atomic_group.name, (
373 'DB inconsistency. HostQueueEntries with multiple atomic'
374 ' groups on job %s: %s != %s' % (
375 id, atomic_group.name, queue_entry.atomic_group.name))
376
377 meta_host_counts = _get_metahost_counts(meta_hosts)
378
379 info = dict(dependencies=[label.name for label
380 in job.dependency_labels.all()],
381 hosts=hosts,
382 meta_hosts=meta_hosts,
383 meta_host_counts=meta_host_counts,
384 one_time_hosts=one_time_hosts,
385 atomic_group=atomic_group)
386 return info
387
388
showarda1e74b32009-05-12 17:32:04 +0000389def create_new_job(owner, options, host_objects, metahost_objects,
390 atomic_group=None):
showard29f7cd22009-04-29 21:16:24 +0000391 labels_by_name = dict((label.name, label)
showarda1e74b32009-05-12 17:32:04 +0000392 for label in models.Label.objects.all())
showard29f7cd22009-04-29 21:16:24 +0000393 all_host_objects = host_objects + metahost_objects
394 metahost_counts = _get_metahost_counts(metahost_objects)
showarda1e74b32009-05-12 17:32:04 +0000395 dependencies = options.get('dependencies', [])
396 synch_count = options.get('synch_count')
showard29f7cd22009-04-29 21:16:24 +0000397
398 # check that each metahost request has enough hosts under the label
399 for label, requested_count in metahost_counts.iteritems():
400 available_count = label.host_set.count()
401 if requested_count > available_count:
402 error = ("You have requested %d %s's, but there are only %d."
403 % (requested_count, label.name, available_count))
404 raise model_logic.ValidationError({'meta_hosts' : error})
405
406 if atomic_group:
407 check_atomic_group_create_job(
408 synch_count, host_objects, metahost_objects,
409 dependencies, atomic_group, labels_by_name)
410 else:
411 if synch_count is not None and synch_count > len(all_host_objects):
412 raise model_logic.ValidationError(
413 {'hosts':
414 'only %d hosts provided for job with synch_count = %d' %
415 (len(all_host_objects), synch_count)})
416 atomic_hosts = models.Host.objects.filter(
417 id__in=[host.id for host in host_objects],
418 labels__atomic_group=True)
419 unusable_host_names = [host.hostname for host in atomic_hosts]
420 if unusable_host_names:
421 raise model_logic.ValidationError(
422 {'hosts':
423 'Host(s) "%s" are atomic group hosts but no '
424 'atomic group was specified for this job.' %
425 (', '.join(unusable_host_names),)})
426
427
428 check_job_dependencies(host_objects, dependencies)
showarda1e74b32009-05-12 17:32:04 +0000429 options['dependencies'] = [labels_by_name[label_name]
430 for label_name in dependencies]
showard29f7cd22009-04-29 21:16:24 +0000431
showarda1e74b32009-05-12 17:32:04 +0000432 for label in metahost_objects + options['dependencies']:
showard29f7cd22009-04-29 21:16:24 +0000433 if label.atomic_group and not atomic_group:
434 raise model_logic.ValidationError(
435 {'atomic_group_name':
showardc8730322009-06-30 01:56:38 +0000436 'Dependency %r requires an atomic group but no '
437 'atomic_group_name or meta_host in an atomic group was '
438 'specified for this job.' % label.name})
showard29f7cd22009-04-29 21:16:24 +0000439 elif (label.atomic_group and
440 label.atomic_group.name != atomic_group.name):
441 raise model_logic.ValidationError(
442 {'atomic_group_name':
showardc8730322009-06-30 01:56:38 +0000443 'meta_hosts or dependency %r requires atomic group '
444 '%r instead of the supplied atomic_group_name=%r.' %
445 (label.name, label.atomic_group.name, atomic_group.name)})
showard29f7cd22009-04-29 21:16:24 +0000446
showarda1e74b32009-05-12 17:32:04 +0000447 job = models.Job.create(owner=owner, options=options,
448 hosts=all_host_objects)
showard29f7cd22009-04-29 21:16:24 +0000449 job.queue(all_host_objects, atomic_group=atomic_group,
showarda1e74b32009-05-12 17:32:04 +0000450 is_template=options.get('is_template', False))
showard29f7cd22009-04-29 21:16:24 +0000451 return job.id
showard0957a842009-05-11 19:25:08 +0000452
453
showard909c9142009-07-07 20:54:42 +0000454def find_platform_and_atomic_group(host):
455 """
456 Figure out the platform name and atomic group name for the given host
457 object. If none, the return value for either will be None.
458
459 @returns (platform name, atomic group name) for the given host.
460 """
showard0957a842009-05-11 19:25:08 +0000461 platforms = [label.name for label in host.label_list if label.platform]
462 if not platforms:
showard909c9142009-07-07 20:54:42 +0000463 platform = None
464 else:
465 platform = platforms[0]
showard0957a842009-05-11 19:25:08 +0000466 if len(platforms) > 1:
467 raise ValueError('Host %s has more than one platform: %s' %
468 (host.hostname, ', '.join(platforms)))
showard909c9142009-07-07 20:54:42 +0000469 for label in host.label_list:
470 if label.atomic_group:
471 atomic_group_name = label.atomic_group.name
472 break
473 else:
474 atomic_group_name = None
475 # Don't check for multiple atomic groups on a host here. That is an
476 # error but should not trip up the RPC interface. monitor_db_cleanup
477 # deals with it. This just returns the first one found.
478 return platform, atomic_group_name
showardc0ac3a72009-07-08 21:14:45 +0000479
480
481# support for get_host_queue_entries_and_special_tasks()
482
483def _common_entry_to_dict(entry, type, job_dict):
484 return dict(type=type,
485 host=entry.host.get_object_dict(),
486 job=job_dict,
487 execution_path=entry.execution_path(),
488 status=entry.status,
489 started_on=entry.started_on,
showard8fb1fde2009-07-11 01:47:16 +0000490 id=str(entry.id) + type)
showardc0ac3a72009-07-08 21:14:45 +0000491
492
493def _special_task_to_dict(special_task):
494 job_dict = None
495 if special_task.queue_entry:
496 job_dict = special_task.queue_entry.job.get_object_dict()
497 return _common_entry_to_dict(special_task, special_task.task, job_dict)
498
499
500def _queue_entry_to_dict(queue_entry):
501 return _common_entry_to_dict(queue_entry, 'Job',
502 queue_entry.job.get_object_dict())
503
504
505def _compute_next_job_for_tasks(queue_entries, special_tasks):
506 """
507 For each task, try to figure out the next job that ran after that task.
508 This is done using two pieces of information:
509 * if the task has a queue entry, we can use that entry's job ID.
510 * if the task has a time_started, we can try to compare that against the
511 started_on field of queue_entries. this isn't guaranteed to work perfectly
512 since queue_entries may also have null started_on values.
513 * if the task has neither, or if use of time_started fails, just use the
514 last computed job ID.
515 """
516 next_job_id = None # most recently computed next job
517 hqe_index = 0 # index for scanning by started_on times
518 for task in special_tasks:
519 if task.queue_entry:
520 next_job_id = task.queue_entry.job.id
521 elif task.time_started is not None:
522 for queue_entry in queue_entries[hqe_index:]:
523 if queue_entry.started_on is None:
524 continue
525 if queue_entry.started_on < task.time_started:
526 break
527 next_job_id = queue_entry.job.id
528
529 task.next_job_id = next_job_id
530
531 # advance hqe_index to just after next_job_id
532 if next_job_id is not None:
533 for queue_entry in queue_entries[hqe_index:]:
534 if queue_entry.job.id < next_job_id:
535 break
536 hqe_index += 1
537
538
539def interleave_entries(queue_entries, special_tasks):
540 """
541 Both lists should be ordered by descending ID.
542 """
543 _compute_next_job_for_tasks(queue_entries, special_tasks)
544
545 # start with all special tasks that've run since the last job
546 interleaved_entries = []
547 for task in special_tasks:
548 if task.next_job_id is not None:
549 break
550 interleaved_entries.append(_special_task_to_dict(task))
551
552 # now interleave queue entries with the remaining special tasks
553 special_task_index = len(interleaved_entries)
554 for queue_entry in queue_entries:
555 interleaved_entries.append(_queue_entry_to_dict(queue_entry))
556 # add all tasks that ran between this job and the previous one
557 for task in special_tasks[special_task_index:]:
558 if task.next_job_id < queue_entry.job.id:
559 break
560 interleaved_entries.append(_special_task_to_dict(task))
561 special_task_index += 1
562
563 return interleaved_entries