Add scheduled task to delete stale document stubs.

This commit is contained in:
Roberto Rosario
2015-09-08 18:38:24 -04:00
parent 02353927db
commit 43e1ffbc24
3 changed files with 26 additions and 2 deletions

View File

@@ -56,7 +56,10 @@ from .links import (
link_document_version_download, link_document_version_list, link_document_version_download, link_document_version_list,
link_document_version_revert, link_trash_can_empty link_document_version_revert, link_trash_can_empty
) )
from .literals import CHECK_DELETE_PERIOD_INTERVAL, CHECK_TRASH_PERIOD_INTERVAL from .literals import (
CHECK_DELETE_PERIOD_INTERVAL, CHECK_TRASH_PERIOD_INTERVAL,
DELETE_STALE_STUBS_INTERVAL
)
from .models import ( from .models import (
DeletedDocument, Document, DocumentPage, DocumentType, DeletedDocument, Document, DocumentPage, DocumentType,
DocumentTypeFilename, DocumentVersion DocumentTypeFilename, DocumentVersion
@@ -183,6 +186,10 @@ class DocumentsApp(MayanAppConfig):
'task': 'documents.tasks.task_check_trash_periods', 'task': 'documents.tasks.task_check_trash_periods',
'schedule': timedelta(seconds=CHECK_TRASH_PERIOD_INTERVAL), 'schedule': timedelta(seconds=CHECK_TRASH_PERIOD_INTERVAL),
}, },
'task_delete_stubs': {
'task': 'documents.tasks.task_delete_stubs',
'schedule': timedelta(seconds=DELETE_STALE_STUBS_INTERVAL),
},
} }
) )
@@ -208,6 +215,9 @@ class DocumentsApp(MayanAppConfig):
'documents.tasks.task_check_trash_periods': { 'documents.tasks.task_check_trash_periods': {
'queue': 'documents_periodic' 'queue': 'documents_periodic'
}, },
'documents.tasks.task_delete_stubs': {
'queue': 'documents_periodic'
},
'documents.tasks.task_clear_image_cache': { 'documents.tasks.task_clear_image_cache': {
'queue': 'tools' 'queue': 'tools'
}, },

View File

@@ -5,10 +5,12 @@ from django.utils.translation import ugettext_lazy as _
CACHE_PATH = 'document_cache/' CACHE_PATH = 'document_cache/'
CHECK_DELETE_PERIOD_INTERVAL = 60 CHECK_DELETE_PERIOD_INTERVAL = 60
CHECK_TRASH_PERIOD_INTERVAL = 60 CHECK_TRASH_PERIOD_INTERVAL = 60
DELETE_STALE_STUBS_INTERVAL = 60
DEFAULT_DELETE_PERIOD = 30 DEFAULT_DELETE_PERIOD = 30
DEFAULT_DELETE_TIME_UNIT = 'days' DEFAULT_DELETE_TIME_UNIT = 'days'
DEFAULT_ZIP_FILENAME = 'document_bundle.zip' DEFAULT_ZIP_FILENAME = 'document_bundle.zip'
DOCUMENT_IMAGE_TASK_TIMEOUT = 20 DOCUMENT_IMAGE_TASK_TIMEOUT = 20
STUB_EXPIRATION_INTERVAL = 60 * 60 * 24 # 24 hours
UPDATE_PAGE_COUNT_RETRY_DELAY = 10 UPDATE_PAGE_COUNT_RETRY_DELAY = 10
UPLOAD_NEW_VERSION_RETRY_DELAY = 10 UPLOAD_NEW_VERSION_RETRY_DELAY = 10
NEW_DOCUMENT_RETRY_DELAY = 10 NEW_DOCUMENT_RETRY_DELAY = 10

View File

@@ -13,7 +13,7 @@ from common.models import SharedUploadedFile
from .literals import ( from .literals import (
UPDATE_PAGE_COUNT_RETRY_DELAY, UPLOAD_NEW_VERSION_RETRY_DELAY, UPDATE_PAGE_COUNT_RETRY_DELAY, UPLOAD_NEW_VERSION_RETRY_DELAY,
NEW_DOCUMENT_RETRY_DELAY NEW_DOCUMENT_RETRY_DELAY, STUB_EXPIRATION_INTERVAL
) )
from .models import ( from .models import (
DeletedDocument, Document, DocumentPage, DocumentType, DocumentVersion DeletedDocument, Document, DocumentPage, DocumentType, DocumentVersion
@@ -41,6 +41,7 @@ def task_check_delete_periods():
document_type, delta document_type, delta
) )
for document in DeletedDocument.objects.filter(document_type=document_type): for document in DeletedDocument.objects.filter(document_type=document_type):
# TODO: Don't iterate, filter documents by expiration
if now() > document.deleted_date_time + delta: if now() > document.deleted_date_time + delta:
logger.info( logger.info(
'Document "%s" with id: %d, trashed on: %s, exceded ' 'Document "%s" with id: %d, trashed on: %s, exceded '
@@ -75,6 +76,7 @@ def task_check_trash_periods():
document_type, delta document_type, delta
) )
for document in Document.objects.filter(document_type=document_type): for document in Document.objects.filter(document_type=document_type):
# TODO: Don't iterate, filter documents by expiration
if now() > document.date_added + delta: if now() > document.date_added + delta:
logger.info( logger.info(
'Document "%s" with id: %d, added on: %s, exceded ' 'Document "%s" with id: %d, added on: %s, exceded '
@@ -98,6 +100,16 @@ def task_clear_image_cache():
logger.info('Finished document cache invalidation') logger.info('Finished document cache invalidation')
@app.task(ignore_result=True)
def task_delete_stubs():
logger.info('Executing')
for stale_stub_document in Document.objects.filter(is_stub=True, date_added__lt=now() - timedelta(seconds=STUB_EXPIRATION_INTERVAL)):
stale_stub_document.delete(trash=False)
logger.info('Finshed')
@app.task(compression='zlib') @app.task(compression='zlib')
def task_get_document_page_image(document_page_id, *args, **kwargs): def task_get_document_page_image(document_page_id, *args, **kwargs):
document_page = DocumentPage.objects.get(pk=document_page_id) document_page = DocumentPage.objects.get(pk=document_page_id)