First few changes for the image diffs feature.

This commit is contained in:
yackob03 2013-10-17 18:25:19 -04:00
parent b22a4aa24c
commit 93b856bdb3
9 changed files with 189 additions and 2 deletions

View file

@ -150,10 +150,19 @@ class RepositoryTag(BaseModel):
)
class QueueItem(BaseMode):
queue_name = CharField(index=True)
body = TextField()
available_after = DateTimeField(default=datetime.now, index=True)
available = BooleanField(default=True, index=True)
processing_expires = DateTimeField(null=True, index=True)
def initialize_db():
create_model_tables([User, Repository, Image, AccessToken, Role,
RepositoryPermission, Visibility, RepositoryTag,
EmailConfirmation, FederatedLogin, LoginService])
EmailConfirmation, FederatedLogin, LoginService,
QueueItem])
Role.create(name='admin')
Role.create(name='write')
Role.create(name='read')

55
data/queue.py Normal file
View file

@ -0,0 +1,55 @@
from datetime import datetime, timedelta
from database import QueueItem
class WorkQueue(object):
def __init__(self, queue_name):
self.queue_name = queue_name
def put(message, available_after=0):
"""
Put an item, if it shouldn't be processed for some number of seconds,
specify that amount as available_after.
"""
params = {
'queue_name': self.queue_name,
'body': message,
}
if available_after:
available_date = datetime.now() + timedelta(seconds=available_after)
params['available_after'] = available_date
QueueItem.create(**params)
def get(processing_time=300):
"""
Get an available item and mark it as unavailable for the default of five
minutes.
"""
now = datetime.now()
available_or_expired = (QueueItem.available == True |
QueueItem.processing_expires <= now)
# TODO the query and the update should be atomic, but for now we only
# have one worker.
avaial = QueueItem.select().where(QueueItem.queue_name = self.queue_name,
QueueItem.available_after <= now,
available_or_expired)
found = list(avail.limit(1).order_by(QueueItem.available_after))
if found:
item = found[0]
item.available = False
item.processing_expires = now + timedelta(seconds=processing_time)
item.save()
return item
return None
def complete(completed_item):
item.delete_instance()