2017-03-16 17:36:59 +00:00
|
|
|
import logging
|
|
|
|
import logging.config
|
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from app import app, storage
|
2017-07-11 13:58:09 +00:00
|
|
|
from data.database import UseThenDisconnect
|
|
|
|
from workers.blobuploadcleanupworker.models_pre_oci import pre_oci_model as model
|
2017-03-16 17:36:59 +00:00
|
|
|
from workers.worker import Worker
|
2017-02-01 23:17:25 +00:00
|
|
|
from util.log import logfile_path
|
2017-03-16 17:36:59 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
DELETION_DATE_THRESHOLD = timedelta(days=2)
|
|
|
|
BLOBUPLOAD_CLEANUP_FREQUENCY = app.config.get('BLOBUPLOAD_CLEANUP_FREQUENCY', 60*60)
|
|
|
|
|
|
|
|
|
|
|
|
class BlobUploadCleanupWorker(Worker):
|
|
|
|
def __init__(self):
|
|
|
|
super(BlobUploadCleanupWorker, self).__init__()
|
|
|
|
self.add_operation(self._cleanup_uploads, BLOBUPLOAD_CLEANUP_FREQUENCY)
|
|
|
|
|
|
|
|
def _cleanup_uploads(self):
|
|
|
|
""" Performs garbage collection on the blobupload table. """
|
|
|
|
while True:
|
|
|
|
# Find all blob uploads older than the threshold (typically a week) and delete them.
|
|
|
|
with UseThenDisconnect(app.config):
|
2017-07-11 13:58:09 +00:00
|
|
|
stale_upload = model.get_stale_blob_upload(DELETION_DATE_THRESHOLD)
|
2017-03-16 17:36:59 +00:00
|
|
|
if stale_upload is None:
|
|
|
|
logger.debug('No additional stale blob uploads found')
|
|
|
|
return
|
|
|
|
|
|
|
|
# Remove the stale upload from storage.
|
|
|
|
logger.debug('Removing stale blob upload %s', stale_upload.uuid)
|
|
|
|
try:
|
2017-07-11 13:58:09 +00:00
|
|
|
storage.cancel_chunked_upload([stale_upload.location_name], stale_upload.uuid,
|
2017-03-16 17:36:59 +00:00
|
|
|
stale_upload.storage_metadata)
|
|
|
|
except Exception as ex:
|
|
|
|
logger.debug('Got error when trying to cancel chunked upload %s: %s', stale_upload.uuid,
|
|
|
|
ex.message)
|
|
|
|
|
|
|
|
# Delete the stale upload's row.
|
|
|
|
with UseThenDisconnect(app.config):
|
2017-07-11 13:58:09 +00:00
|
|
|
model.delete_blob_upload(stale_upload)
|
2017-03-16 17:36:59 +00:00
|
|
|
|
|
|
|
logger.debug('Removed stale blob upload %s', stale_upload.uuid)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-02-01 23:17:25 +00:00
|
|
|
logging.config.fileConfig(logfile_path(debug=True), disable_existing_loggers=False)
|
2017-03-16 17:36:59 +00:00
|
|
|
worker = BlobUploadCleanupWorker()
|
|
|
|
worker.start()
|