Add garbage collection task

This commit is contained in:
Jessica Tallon 2014-07-15 21:24:25 +01:00
parent 0e283215bd
commit 0679545f19
5 changed files with 98 additions and 17 deletions

View File

@ -23,6 +23,10 @@ allow_registration = true
# Set to false to disable the ability for users to report offensive content
allow_reporting = true
# Frequency garbage collection will run (setting to 0 or false to disable)
# Setting units are minutes.
garbage_collection = 60
## Uncomment this to put some user-overriding templates here
# local_templates = %(here)s/user_dev/templates/

49
mediagoblin/federation/task.py Executable file
View File

@ -0,0 +1,49 @@
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import celery
import datetime
import logging
import pytz
from mediagoblin.db.models import MediaEntry
_log = logging.getLogger(__name__)
logging.basicConfig()
_log.setLevel(logging.DEBUG)
@celery.task()
def collect_garbage():
"""
Garbage collection to clean up media
This will look for all critera on models to clean
up. This is primerally written to clean up media that's
entered a erroneous state.
"""
_log.info("Garbage collection is running.")
now = datetime.datetime.now(pytz.UTC) - datetime.timedelta(days=1)
garbage = MediaEntry.query.filter(MediaEntry.created > now)
garbage = garbage.filter(MediaEntry.state == "unprocessed")
for entry in garbage.all():
_log.info("Garbage media found with ID '{0}'".format(entry.id))
entry.delete()

View File

@ -1,3 +1,19 @@
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import io
import mimetypes
@ -135,7 +151,7 @@ def feed(request):
media.license = obj["license"]
media.save()
manager = media.media_manager.api_add_to_feed(request, media)
media.media_manager.api_add_to_feed(request, media)
return json_response({
"verb": "post",
@ -319,7 +335,6 @@ def object_comments(request):
return response
##
# Well known
##

View File

@ -16,6 +16,7 @@
import os
import sys
import datetime
import logging
from celery import Celery
@ -58,6 +59,18 @@ def get_celery_settings_dict(app_config, global_config,
celery_settings['CELERY_ALWAYS_EAGER'] = True
celery_settings['CELERY_EAGER_PROPAGATES_EXCEPTIONS'] = True
# Garbage collection periodic task
frequency = app_config.get('garbage_collection', 60)
if frequency:
frequency = int(app_config['garbage_collection'])
celery_settings['CELERYBEAT_SCHEDULE'] = {
'garbage-collection': {
'task': 'mediagoblin.federation.task.garbage_collection',
'schedule': datetime.timedelta(minutes=frequency),
}
}
celery_settings['BROKER_HEARTBEAT'] = 1
return celery_settings