diff --git a/CHANGES/3122.bugfix b/CHANGES/3122.bugfix new file mode 100644 index 0000000000..1b5eb41b35 --- /dev/null +++ b/CHANGES/3122.bugfix @@ -0,0 +1,2 @@ +Moved telemetry setup to the pulpcore-worker startup sequence. This will prevent orm calls before +all apps are ready. diff --git a/pulpcore/app/apps.py b/pulpcore/app/apps.py index 6b6dba17ba..c651e234f5 100644 --- a/pulpcore/app/apps.py +++ b/pulpcore/app/apps.py @@ -1,7 +1,6 @@ from collections import defaultdict from gettext import gettext as _ from importlib import import_module -from datetime import timedelta from django import apps from django.core.exceptions import ImproperlyConfigured @@ -212,8 +211,6 @@ def ready(self): super().ready() from . import checks # noqa - _configure_telemetry(self.apps) - post_migrate.connect( _populate_system_id, sender=self, dispatch_uid="populate_system_id_identifier" ) @@ -262,26 +259,6 @@ def _populate_system_id(sender, apps, verbosity, **kwargs): SystemID().save() -def _configure_telemetry(apps): - from django.db import connection - from pulpcore.app.util import get_telemetry_posting_url, PRODUCTION_URL - - if "core_taskschedule" in connection.introspection.table_names(): - url = get_telemetry_posting_url() - TaskSchedule = apps.get_model("core", "TaskSchedule") - task_name = "pulpcore.app.tasks.telemetry.post_telemetry" - dispatch_interval = timedelta(days=1) - name = "Post Anonymous Telemetry Periodically" - # Initially only dev systems receive posted data. - if url == PRODUCTION_URL: - TaskSchedule.objects.filter(task_name=task_name).delete() - else: - TaskSchedule.objects.update_or_create( - name=name, defaults={"task_name": task_name, "dispatch_interval": dispatch_interval} - ) - connection.close() - - def _populate_roles(sender, apps, verbosity, **kwargs): role_prefix = f"{sender.label}." # collect all plugin defined roles diff --git a/pulpcore/app/util.py b/pulpcore/app/util.py index 5243bf4eab..dd8f6f4dd5 100644 --- a/pulpcore/app/util.py +++ b/pulpcore/app/util.py @@ -3,6 +3,7 @@ import tempfile from contextlib import ExitStack +from datetime import timedelta import gnupg from django.conf import settings @@ -254,3 +255,17 @@ def get_telemetry_posting_url(): return DEV_URL return PRODUCTION_URL + + +def configure_telemetry(): + url = get_telemetry_posting_url() + task_name = "pulpcore.app.tasks.telemetry.post_telemetry" + dispatch_interval = timedelta(days=1) + name = "Post Anonymous Telemetry Periodically" + # Initially only dev systems send data. + if url == PRODUCTION_URL: + models.TaskSchedule.objects.filter(task_name=task_name).delete() + else: + models.TaskSchedule.objects.update_or_create( + name=name, defaults={"task_name": task_name, "dispatch_interval": dispatch_interval} + ) diff --git a/pulpcore/tasking/pulpcore_worker.py b/pulpcore/tasking/pulpcore_worker.py index 8a5f5e8404..8f94668210 100644 --- a/pulpcore/tasking/pulpcore_worker.py +++ b/pulpcore/tasking/pulpcore_worker.py @@ -33,6 +33,7 @@ from pulpcore.app.models import Worker, Task # noqa: E402: module level not at top of file +from pulpcore.app.util import configure_telemetry # noqa: E402: module level not at top of file from pulpcore.app.role_util import ( # noqa: E402: module level not at top of file get_users_with_perms, ) @@ -58,6 +59,10 @@ TASK_SCHEDULING_LOCK = 42 +def startup_hook(): + configure_telemetry() + + class PGAdvisoryLock: """ A context manager that will hold a postgres advisory lock non-blocking. @@ -132,6 +137,8 @@ def __init__(self): os.set_blocking(sentinel_w, False) signal.set_wakeup_fd(sentinel_w) + startup_hook() + def _signal_handler(self, thesignal, frame): # Reset signal handlers to default # If you kill the process a second time it's not graceful anymore.