diff --git a/app/YtManagerApp/management/jobs/synchronize.py b/app/YtManagerApp/management/jobs/synchronize.py index 376d980..5d2be4d 100644 --- a/app/YtManagerApp/management/jobs/synchronize.py +++ b/app/YtManagerApp/management/jobs/synchronize.py @@ -148,10 +148,22 @@ def synchronize_subscription(subscription: Subscription): __lock.release() +__global_sync_job = None + + def schedule_synchronize_global(): + global __global_sync_job + trigger = CronTrigger.from_crontab(appconfig.sync_schedule) - job = scheduler.add_job(synchronize, trigger, max_instances=1, coalesce=True) - log.info('Scheduled synchronize job job=%s', job.id) + + if __global_sync_job is None: + trigger = CronTrigger.from_crontab(appconfig.sync_schedule) + __global_sync_job = scheduler.add_job(synchronize, trigger, max_instances=1, coalesce=True) + + else: + __global_sync_job.reschedule(trigger, max_instances=1, coalesce=True) + + log.info('Scheduled synchronize job job=%s', __global_sync_job.id) def schedule_synchronize_now(): diff --git a/app/YtManagerApp/scheduler.py b/app/YtManagerApp/scheduler.py index 74a8fdb..c103700 100644 --- a/app/YtManagerApp/scheduler.py +++ b/app/YtManagerApp/scheduler.py @@ -9,6 +9,9 @@ scheduler = BackgroundScheduler() def initialize_scheduler(): + if scheduler.running: + return + logger = logging.getLogger('scheduler') executors = { 'default': { diff --git a/app/YtManagerApp/views/first_time.py b/app/YtManagerApp/views/first_time.py index 6d243b4..6e3ce0c 100644 --- a/app/YtManagerApp/views/first_time.py +++ b/app/YtManagerApp/views/first_time.py @@ -9,6 +9,8 @@ from django.urls import reverse_lazy from django.views.generic import FormView from YtManagerApp.management.appconfig import appconfig +from YtManagerApp.management.jobs.synchronize import schedule_synchronize_global +from YtManagerApp.scheduler import initialize_scheduler from YtManagerApp.views.forms.auth import ExtendedAuthenticationForm from YtManagerApp.views.forms.first_time import WelcomeForm, ApiKeyForm, PickAdminUserForm, ServerConfigForm, DoneForm, UserCreationForm @@ -174,7 +176,11 @@ class Step3ConfigureView(WizardStepMixin, FormView): # Set initialized to true appconfig.initialized = True - + + # Start scheduler if not started + initialize_scheduler() + schedule_synchronize_global() + return super().form_valid(form) diff --git a/app/YtManagerApp/views/settings.py b/app/YtManagerApp/views/settings.py index a284d41..a1329d7 100644 --- a/app/YtManagerApp/views/settings.py +++ b/app/YtManagerApp/views/settings.py @@ -3,6 +3,7 @@ from django.http import HttpResponseForbidden from django.urls import reverse_lazy from django.views.generic import FormView +from YtManagerApp.management.jobs.synchronize import schedule_synchronize_global from YtManagerApp.views.forms.settings import SettingsForm, AdminSettingsForm @@ -44,4 +45,5 @@ class AdminSettingsView(LoginRequiredMixin, FormView): def form_valid(self, form): form.save() + schedule_synchronize_global() return super().form_valid(form)