2013-10-17 22:25:19 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2015-09-21 17:34:12 +00:00
|
|
|
from data.database import QueueItem, db, db_for_update
|
2014-10-01 18:23:15 +00:00
|
|
|
from util.morecollections import AttrDict
|
2013-10-17 22:25:19 +00:00
|
|
|
|
|
|
|
|
2014-05-06 22:46:19 +00:00
|
|
|
MINIMUM_EXTENSION = timedelta(seconds=20)
|
|
|
|
|
2015-02-18 00:15:54 +00:00
|
|
|
class NoopWith:
|
|
|
|
def __enter__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
pass
|
2014-05-06 22:46:19 +00:00
|
|
|
|
2015-08-17 21:22:46 +00:00
|
|
|
class MetricQueueReporter(object):
|
|
|
|
def __init__(self, metric_queue):
|
|
|
|
self._metric_queue = metric_queue
|
|
|
|
|
|
|
|
def __call__(self, currently_processing, running_count, total_count):
|
|
|
|
need_capacity_count = total_count - running_count
|
|
|
|
self._metric_queue.put('BuildCapacityShortage', need_capacity_count, unit='Count')
|
|
|
|
|
|
|
|
building_percent = 100 if currently_processing else 0
|
|
|
|
self._metric_queue.put('PercentBuilding', building_percent, unit='Percent')
|
|
|
|
|
2013-10-17 22:25:19 +00:00
|
|
|
class WorkQueue(object):
|
2014-05-22 16:13:41 +00:00
|
|
|
def __init__(self, queue_name, transaction_factory,
|
2015-09-14 19:57:08 +00:00
|
|
|
canonical_name_match_list=None, reporter=None, metric_queue=None):
|
2014-05-21 23:50:37 +00:00
|
|
|
self._queue_name = queue_name
|
2015-08-17 21:22:46 +00:00
|
|
|
self._reporter = reporter
|
2015-09-14 19:57:08 +00:00
|
|
|
self._metric_queue = metric_queue
|
2014-05-22 16:13:41 +00:00
|
|
|
self._transaction_factory = transaction_factory
|
2014-05-23 18:16:26 +00:00
|
|
|
self._currently_processing = False
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2014-04-11 22:34:47 +00:00
|
|
|
if canonical_name_match_list is None:
|
2014-05-21 23:50:37 +00:00
|
|
|
self._canonical_name_match_list = []
|
2014-04-11 22:34:47 +00:00
|
|
|
else:
|
2014-05-21 23:50:37 +00:00
|
|
|
self._canonical_name_match_list = canonical_name_match_list
|
2014-04-11 22:34:47 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _canonical_name(name_list):
|
|
|
|
return '/'.join(name_list) + '/'
|
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
def _running_jobs(self, now, name_match_query):
|
|
|
|
return (QueueItem
|
2014-10-01 18:23:15 +00:00
|
|
|
.select(QueueItem.queue_name)
|
|
|
|
.where(QueueItem.available == False,
|
|
|
|
QueueItem.processing_expires > now,
|
|
|
|
QueueItem.queue_name ** name_match_query))
|
2014-05-21 23:50:37 +00:00
|
|
|
|
2015-02-03 19:50:01 +00:00
|
|
|
def _available_jobs(self, now, name_match_query):
|
2014-05-22 17:50:06 +00:00
|
|
|
return (QueueItem
|
2014-10-01 18:23:15 +00:00
|
|
|
.select()
|
|
|
|
.where(QueueItem.queue_name ** name_match_query, QueueItem.available_after <= now,
|
|
|
|
((QueueItem.available == True) | (QueueItem.processing_expires <= now)),
|
2015-02-03 19:50:01 +00:00
|
|
|
QueueItem.retries_remaining > 0))
|
|
|
|
|
|
|
|
def _available_jobs_not_running(self, now, name_match_query, running_query):
|
|
|
|
return (self
|
|
|
|
._available_jobs(now, name_match_query)
|
|
|
|
.where(~(QueueItem.queue_name << running_query)))
|
2014-05-22 17:50:06 +00:00
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
def _name_match_query(self):
|
|
|
|
return '%s%%' % self._canonical_name([self._queue_name] + self._canonical_name_match_list)
|
|
|
|
|
2015-01-30 21:32:13 +00:00
|
|
|
def _item_by_id_for_update(self, queue_id):
|
|
|
|
return db_for_update(QueueItem.select().where(QueueItem.id == queue_id)).get()
|
|
|
|
|
2015-02-18 00:15:54 +00:00
|
|
|
def get_metrics(self, require_transaction=True):
|
2015-09-21 17:34:12 +00:00
|
|
|
guard = self._transaction_factory(db) if require_transaction else NoopWith()
|
|
|
|
with guard:
|
|
|
|
now = datetime.utcnow()
|
|
|
|
name_match_query = self._name_match_query()
|
2014-05-22 17:50:06 +00:00
|
|
|
|
2015-09-21 17:34:12 +00:00
|
|
|
running_query = self._running_jobs(now, name_match_query)
|
|
|
|
running_count = running_query.distinct().count()
|
2014-05-21 23:50:37 +00:00
|
|
|
|
2015-09-21 17:34:12 +00:00
|
|
|
available_query = self._available_jobs(now, name_match_query)
|
|
|
|
available_count = available_query.select(QueueItem.queue_name).distinct().count()
|
2014-05-21 23:50:37 +00:00
|
|
|
|
2015-09-21 17:34:12 +00:00
|
|
|
available_not_running_query = self._available_jobs_not_running(now, name_match_query,
|
|
|
|
running_query)
|
|
|
|
available_not_running_count = (available_not_running_query.select(QueueItem.queue_name)
|
|
|
|
.distinct().count())
|
2015-02-18 00:15:54 +00:00
|
|
|
|
2015-09-21 17:34:12 +00:00
|
|
|
return (running_count, available_not_running_count, available_count)
|
2015-02-18 00:15:54 +00:00
|
|
|
|
|
|
|
def update_metrics(self):
|
2015-09-14 19:57:08 +00:00
|
|
|
if self._reporter is None and self._metric_queue is None:
|
2015-02-18 00:15:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
(running_count, available_not_running_count, available_count) = self.get_metrics()
|
2015-09-14 19:57:08 +00:00
|
|
|
|
|
|
|
if self._metric_queue:
|
|
|
|
dim = {'queue': self._queue_name}
|
|
|
|
self._metric_queue.put('Running', running_count, dimensions=dim)
|
|
|
|
self._metric_queue.put('AvailableNotRunning', available_not_running_count, dimensions=dim)
|
|
|
|
self._metric_queue.put('Available', available_count, dimensions=dim)
|
|
|
|
|
|
|
|
if self._reporter:
|
|
|
|
self._reporter(self._currently_processing, running_count,
|
|
|
|
running_count + available_not_running_count)
|
2014-05-21 23:50:37 +00:00
|
|
|
|
2015-02-23 18:38:01 +00:00
|
|
|
def has_retries_remaining(self, item_id):
|
|
|
|
""" Returns whether the queue item with the given id has any retries remaining. If the
|
|
|
|
queue item does not exist, returns False. """
|
|
|
|
with self._transaction_factory(db):
|
|
|
|
try:
|
|
|
|
return QueueItem.get(id=item_id).retries_remaining > 0
|
|
|
|
except QueueItem.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
2014-04-11 22:34:47 +00:00
|
|
|
def put(self, canonical_name_list, message, available_after=0, retries_remaining=5):
|
2013-10-17 22:25:19 +00:00
|
|
|
"""
|
|
|
|
Put an item, if it shouldn't be processed for some number of seconds,
|
2015-02-23 18:38:01 +00:00
|
|
|
specify that amount as available_after. Returns the ID of the queue item added.
|
2013-10-17 22:25:19 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
params = {
|
2014-05-21 23:50:37 +00:00
|
|
|
'queue_name': self._canonical_name([self._queue_name] + canonical_name_list),
|
2013-10-17 22:25:19 +00:00
|
|
|
'body': message,
|
2013-11-15 20:49:26 +00:00
|
|
|
'retries_remaining': retries_remaining,
|
2013-10-17 22:25:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 16:52:57 +00:00
|
|
|
available_date = datetime.utcnow() + timedelta(seconds=available_after or 0)
|
|
|
|
params['available_after'] = available_date
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2014-05-22 16:13:41 +00:00
|
|
|
with self._transaction_factory(db):
|
2015-09-18 20:21:16 +00:00
|
|
|
r = str(QueueItem.create(**params).id)
|
2015-09-21 17:37:49 +00:00
|
|
|
if self._metric_queue:
|
|
|
|
self._metric_queue.put('Added', 1, dimensions={'queue': self._queue_name})
|
|
|
|
return r
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2013-10-18 19:28:16 +00:00
|
|
|
def get(self, processing_time=300):
|
2013-10-17 22:25:19 +00:00
|
|
|
"""
|
|
|
|
Get an available item and mark it as unavailable for the default of five
|
2014-12-31 16:33:56 +00:00
|
|
|
minutes. The result of this method must always be composed of simple
|
|
|
|
python objects which are JSON serializable for network portability reasons.
|
2013-10-17 22:25:19 +00:00
|
|
|
"""
|
2014-05-23 18:16:26 +00:00
|
|
|
now = datetime.utcnow()
|
2014-04-11 22:34:47 +00:00
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
name_match_query = self._name_match_query()
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2015-02-03 19:50:01 +00:00
|
|
|
running = self._running_jobs(now, name_match_query)
|
2015-08-05 19:27:33 +00:00
|
|
|
avail = self._available_jobs_not_running(now, name_match_query, running)
|
2015-02-03 19:50:01 +00:00
|
|
|
|
|
|
|
item = None
|
|
|
|
try:
|
2015-08-05 19:27:33 +00:00
|
|
|
db_item_candidate = avail.order_by(QueueItem.id).get()
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2015-02-03 19:50:01 +00:00
|
|
|
with self._transaction_factory(db):
|
|
|
|
still_available_query = (db_for_update(self
|
|
|
|
._available_jobs(now, name_match_query)
|
2015-08-05 19:27:33 +00:00
|
|
|
.where(QueueItem.id == db_item_candidate.id)))
|
2015-02-03 19:50:01 +00:00
|
|
|
|
|
|
|
db_item = still_available_query.get()
|
2014-10-01 18:23:15 +00:00
|
|
|
db_item.available = False
|
|
|
|
db_item.processing_expires = now + timedelta(seconds=processing_time)
|
|
|
|
db_item.retries_remaining -= 1
|
|
|
|
db_item.save()
|
|
|
|
|
|
|
|
item = AttrDict({
|
|
|
|
'id': db_item.id,
|
|
|
|
'body': db_item.body,
|
2015-02-12 21:19:44 +00:00
|
|
|
'retries_remaining': db_item.retries_remaining
|
2014-10-01 18:23:15 +00:00
|
|
|
})
|
2014-05-23 18:16:26 +00:00
|
|
|
|
|
|
|
self._currently_processing = True
|
2015-02-03 19:50:01 +00:00
|
|
|
except QueueItem.DoesNotExist:
|
|
|
|
self._currently_processing = False
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2015-02-03 19:50:01 +00:00
|
|
|
# Return a view of the queue item rather than an active db object
|
|
|
|
return item
|
2013-10-17 22:25:19 +00:00
|
|
|
|
2015-02-23 18:38:01 +00:00
|
|
|
def cancel(self, item_id):
|
|
|
|
""" Attempts to cancel the queue item with the given ID from the queue. Returns true on success
|
|
|
|
and false if the queue item could not be canceled. A queue item can only be canceled if
|
|
|
|
if is available and has retries remaining.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with self._transaction_factory(db):
|
|
|
|
# Load the build queue item for update.
|
|
|
|
try:
|
|
|
|
queue_item = db_for_update(QueueItem.select()
|
|
|
|
.where(QueueItem.id == item_id)).get()
|
|
|
|
except QueueItem.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Check the queue item.
|
|
|
|
if not queue_item.available or queue_item.retries_remaining == 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Delete the queue item.
|
|
|
|
queue_item.delete_instance(recursive=True)
|
|
|
|
return True
|
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
def complete(self, completed_item):
|
2014-05-22 16:13:41 +00:00
|
|
|
with self._transaction_factory(db):
|
2015-09-10 18:12:16 +00:00
|
|
|
try:
|
|
|
|
completed_item_obj = self._item_by_id_for_update(completed_item.id)
|
|
|
|
except QueueItem.DoesNotExist:
|
|
|
|
self._currently_processing = False
|
|
|
|
return
|
|
|
|
|
2015-02-23 18:38:01 +00:00
|
|
|
completed_item_obj.delete_instance(recursive=True)
|
2014-05-23 18:16:26 +00:00
|
|
|
self._currently_processing = False
|
2013-10-18 21:27:09 +00:00
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
def incomplete(self, incomplete_item, retry_after=300, restore_retry=False):
|
2014-05-22 16:13:41 +00:00
|
|
|
with self._transaction_factory(db):
|
2014-05-23 18:16:26 +00:00
|
|
|
retry_date = datetime.utcnow() + timedelta(seconds=retry_after)
|
2015-01-30 21:32:13 +00:00
|
|
|
incomplete_item_obj = self._item_by_id_for_update(incomplete_item.id)
|
2014-10-01 18:23:15 +00:00
|
|
|
incomplete_item_obj.available_after = retry_date
|
|
|
|
incomplete_item_obj.available = True
|
2014-05-06 22:46:19 +00:00
|
|
|
|
2014-05-21 23:50:37 +00:00
|
|
|
if restore_retry:
|
2014-10-01 18:23:15 +00:00
|
|
|
incomplete_item_obj.retries_remaining += 1
|
2014-05-06 22:46:19 +00:00
|
|
|
|
2014-10-01 18:23:15 +00:00
|
|
|
incomplete_item_obj.save()
|
2014-05-23 18:16:26 +00:00
|
|
|
self._currently_processing = False
|
2015-02-12 21:19:44 +00:00
|
|
|
return incomplete_item_obj.retries_remaining > 0
|
2013-10-29 19:42:19 +00:00
|
|
|
|
2015-01-30 21:32:13 +00:00
|
|
|
def extend_processing(self, item, seconds_from_now, minimum_extension=MINIMUM_EXTENSION):
|
2015-01-29 23:40:41 +00:00
|
|
|
with self._transaction_factory(db):
|
2015-01-30 21:32:13 +00:00
|
|
|
queue_item = self._item_by_id_for_update(item.id)
|
2015-01-29 23:40:41 +00:00
|
|
|
new_expiration = datetime.utcnow() + timedelta(seconds=seconds_from_now)
|
|
|
|
|
|
|
|
# Only actually write the new expiration to the db if it moves the expiration some minimum
|
|
|
|
if new_expiration - queue_item.processing_expires > minimum_extension:
|
|
|
|
queue_item.processing_expires = new_expiration
|
2015-01-30 21:32:13 +00:00
|
|
|
queue_item.save()
|