From 2a34de5cd98ab260170c239a6416087990349ee3 Mon Sep 17 00:00:00 2001 From: afabiani Date: Fri, 15 Oct 2021 18:08:23 +0200 Subject: [PATCH] - Just making the tasks initialization coherent with the other modules --- geonode/harvesting/apps.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/geonode/harvesting/apps.py b/geonode/harvesting/apps.py index 4d6cde9a3b6..8dfc2c3f2c1 100644 --- a/geonode/harvesting/apps.py +++ b/geonode/harvesting/apps.py @@ -20,10 +20,43 @@ from django.apps import AppConfig from django.conf import settings from django.conf.urls import url, include +from django.db.models.signals import post_migrate from . import config +def run_setup_hooks(sender, **kwargs): + from django.utils import timezone + + # Initialize periodic tasks + if 'django_celery_beat' in settings.INSTALLED_APPS and \ + getattr(settings, 'CELERY_BEAT_SCHEDULER', None) == 'django_celery_beat.schedulers:DatabaseScheduler': + from django_celery_beat.models import ( + IntervalSchedule, + PeriodicTask, + ) + + secs = config.get_setting("HARVESTER_SCHEDULER_FREQUENCY_MINUTES") * 60 + check_intervals = IntervalSchedule.objects.filter(every=secs, period="seconds") + if not check_intervals.exists(): + check_interval, _ = IntervalSchedule.objects.get_or_create( + every=secs, + period="seconds" + ) + else: + check_interval = check_intervals.first() + + PeriodicTask.objects.update_or_create( + name="harvesting-scheduler", + defaults=dict( + task="geonode.harvesting.tasks.harvesting_scheduler", + interval=check_interval, + args='', + start_time=timezone.now() + ) + ) + + class HarvestingAppConfig(AppConfig): name = "geonode.harvesting" @@ -34,6 +67,7 @@ def ready(self): urlpatterns += [ url(r'^api/v2/', include('geonode.harvesting.api.urls')) ] + post_migrate.connect(run_setup_hooks, sender=self) settings.CELERY_BEAT_SCHEDULE['harvesting-scheduler'] = { "task": "geonode.harvesting.tasks.harvesting_scheduler", "schedule": config.get_setting("HARVESTER_SCHEDULER_FREQUENCY_MINUTES") * 60,