Add a cleanup worker for blob uploads

This commit is contained in:
Joseph Schorr 2017-03-16 13:36:59 -04:00
parent ce7a9d550d
commit e25c989fef
4 changed files with 102 additions and 1 deletions

View file

@ -1,9 +1,10 @@
from datetime import datetime
from uuid import uuid4
from data.model import (tag, _basequery, BlobDoesNotExist, InvalidBlobUpload, db_transaction,
storage as storage_model, InvalidImageException)
from data.database import (Repository, Namespace, ImageStorage, Image, ImageStoragePlacement,
BlobUpload, ImageStorageLocation)
BlobUpload, ImageStorageLocation, db_random_func)
def get_repo_blob_by_digest(namespace, repo_name, blob_digest):
@ -58,6 +59,35 @@ def store_blob_record_and_temp_link(namespace, repo_name, blob_digest, location_
return storage
def get_stale_blob_upload(stale_timespan):
""" Returns a random blob upload which was created before the stale timespan. """
stale_threshold = datetime.now() - stale_timespan
try:
candidates = (BlobUpload
.select()
.where(BlobUpload.created <= stale_threshold)
.limit(500)
.distinct()
.alias('candidates'))
found = (BlobUpload
.select(candidates.c.id)
.from_(candidates)
.order_by(db_random_func())
.get())
if not found:
return None
return (BlobUpload
.select(BlobUpload, ImageStorageLocation)
.join(ImageStorageLocation)
.where(BlobUpload.id == found.id)
.get())
except BlobUpload.DoesNotExist:
return None
def get_blob_upload(namespace, repo_name, upload_uuid):
""" Load the upload which is already in progress.
"""