From 9052efd8a8b3676bac279ea8fa7d52eb463d2162 Mon Sep 17 00:00:00 2001 From: Kai Schlamp Date: Tue, 14 Nov 2023 11:36:57 +0000 Subject: [PATCH] Debounce autoreload of non web servers during development --- TODO.md | 2 +- adit/core/management/base/server_command.py | 4 ++++ adit/{selective_transfer => core}/utils/debounce.py | 1 + adit/selective_transfer/consumers.py | 2 +- 4 files changed, 7 insertions(+), 2 deletions(-) rename adit/{selective_transfer => core}/utils/debounce.py (99%) diff --git a/TODO.md b/TODO.md index 32a22900..9993ec49 100644 --- a/TODO.md +++ b/TODO.md @@ -3,7 +3,6 @@ ## Top - Before new release - -- Fix order of forms that source / destination fields are at the top again -- test job_utils -- exclude autoreload when tests are saved (Custom Filter in server command watched files) -- Test canceled task/job in test_workers.py @@ -112,6 +111,7 @@ ## Maybe +- Switch from Daphne to Uvicorn (maybe it has faster restart times during development) - Switch from Celery to Huey - Upgrade postgres server to v15, but we have to migrate the data then as the database files are incompatible a newer version -- diff --git a/adit/core/management/base/server_command.py b/adit/core/management/base/server_command.py index 8becbbc1..01af9a7e 100644 --- a/adit/core/management/base/server_command.py +++ b/adit/core/management/base/server_command.py @@ -12,6 +12,8 @@ from django.core.management.base import BaseCommand from watchfiles import PythonFilter, watch +from ...utils.debounce import debounce + class ServerCommand(BaseCommand, ABC): """See Django's runserver.py command. @@ -57,6 +59,8 @@ def run(self, **options): if options["autoreload"]: self.stdout.write(f"Autoreload enabled for {self.server_name}.") + # We debounce to give the Django webserver time to restart first + @debounce(wait_time=2) def inner_run(): if self._popen is not None: self._popen.terminate() diff --git a/adit/selective_transfer/utils/debounce.py b/adit/core/utils/debounce.py similarity index 99% rename from adit/selective_transfer/utils/debounce.py rename to adit/core/utils/debounce.py index 5efa5308..2f1f0b86 100644 --- a/adit/selective_transfer/utils/debounce.py +++ b/adit/core/utils/debounce.py @@ -5,6 +5,7 @@ def debounce(wait_time=1): """Decorator that will debounce a function. + # Function is called after wait_time in seconds. If it is called multiple times, it will wait for the last call to be debounced and run only this one. """ diff --git a/adit/selective_transfer/consumers.py b/adit/selective_transfer/consumers.py index 64b3cf9e..fabbf8d5 100644 --- a/adit/selective_transfer/consumers.py +++ b/adit/selective_transfer/consumers.py @@ -15,13 +15,13 @@ from adit.accounts.models import User from adit.core.models import DicomNode +from adit.core.utils.debounce import debounce from adit.core.utils.dicom_dataset import QueryDataset, ResultDataset from adit.core.utils.dicom_operator import DicomOperator from adit.core.utils.job_utils import queue_pending_tasks from .forms import SelectiveTransferJobForm from .models import SelectiveTransferJob, SelectiveTransferTask -from .utils.debounce import debounce from .views import SELECTIVE_TRANSFER_ADVANCED_OPTIONS_COLLAPSED logger = logging.getLogger(__name__)