Skip to content

Commit

Permalink
[Fixes #10303] automatic periodic TaskResult removal (#10306) (#10319)
Browse files Browse the repository at this point in the history
* [Fixes #10303] automatic periodic TaskResult removal

* [Fixes #10303] automatic periodic TaskResult removal

* [Fixes #10303] automatic periodic TaskResult removal

* [Fixes #10303] fix flake8 formatting

Co-authored-by: Alessio Fabiani <[email protected]>

Co-authored-by: mattiagiupponi <[email protected]>
Co-authored-by: Alessio Fabiani <[email protected]>
  • Loading branch information
3 people authored Nov 21, 2022
1 parent ba07953 commit 1f25962
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions geonode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,7 @@ def get_geonode_catalogue_service():
Queue('email', GEONODE_EXCHANGE, routing_key='email', priority=0),
Queue('security', GEONODE_EXCHANGE, routing_key='security', priority=0),
Queue('management_commands_http', GEONODE_EXCHANGE, routing_key='management_commands_http', priority=0),
Queue('clery_cleanup', GEONODE_EXCHANGE, routing_key='clery_cleanup', priority=0)
)

if USE_GEOSERVER:
Expand Down
17 changes: 17 additions & 0 deletions geonode/upload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ def run_setup_hooks(sender, **kwargs):
start_time=timezone.now()
)
)
daily_interval, _ = IntervalSchedule.objects.get_or_create(
every=1,
period="days"
)
PeriodicTask.objects.update_or_create(
name="clean-up-old-task-result",
defaults=dict(
task="geonode.upload.tasks.cleanup_celery_task_entries",
interval=daily_interval,
args='',
start_time=timezone.now()
)
)


class UploadAppConfig(AppConfig):
Expand All @@ -67,6 +80,10 @@ def ready(self):
'task': 'geonode.upload.tasks.finalize_incomplete_session_uploads',
'schedule': 10.0,
}
settings.CELERY_BEAT_SCHEDULE['clean-up-old-task-result'] = {
'task': 'geonode.upload.tasks.cleanup_celery_task_entries',
'schedule': 86400.0,
}


default_app_config = "geonode.upload.UploadAppConfig"
13 changes: 13 additions & 0 deletions geonode/upload/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
from datetime import datetime
import json
import logging

Expand Down Expand Up @@ -238,3 +239,15 @@ def _upload_session_cleanup(self, upload_session_id: int):
logger.debug(f"Upload {upload_session_id} deleted with state {_upload.state}.")
except Exception as e:
logger.error(f"Upload {upload_session_id} errored with exception {e}.")


@app.task(
bind=False,
acks_late=False,
queue="clery_cleanup",
ignore_result=True)
def cleanup_celery_task_entries():
from django_celery_results.models import TaskResult
result_obj = TaskResult.objects.filter(date_done__lte=(datetime.today() - timedelta(days=7)))
logger.error(f"Total celery task to be deleted: {result_obj.count()}")
result_obj.delete()

0 comments on commit 1f25962

Please sign in to comment.