data.queue: limiting before order by rand

This commit is contained in:
Jimmy Zelinskie 2016-05-27 14:41:44 -04:00
parent fdf49d65ec
commit 8a5aa65d74

View file

@ -155,12 +155,11 @@ class WorkQueue(object):
avail = self._available_jobs_not_running(now, name_match_query, running)
db_item = avail.order_by(QueueItem.id).get()
else:
# If we don't require ordering, we can forego this whole subquery and
# instead try and grab any available item. We ORDER BY randomly in order
# to prevent races.
db_item = (self
._available_jobs(now, name_match_query)
.limit(50)
# If we don't require ordering, we grab a random item from any of the first 50 available.
subquery = self._available_jobs(now, name_match_query).limit(50).alias('j1')
db_item = (QueueItem
.select()
.join(subquery, on=QueueItem.id == subquery.c.id)
.order_by(db_random_func())
.get())