Skip to content

Commit

Permalink
Move telemetry setup to pulpcore-worker startup
Browse files Browse the repository at this point in the history
fixes pulp#3122
  • Loading branch information
mdellweg authored and bmbouter committed Aug 24, 2022
1 parent bc001ea commit f85381e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
2 changes: 2 additions & 0 deletions 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.
23 changes: 0 additions & 23 deletions 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
Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions pulpcore/app/util.py
Expand Up @@ -3,6 +3,7 @@
import tempfile

from contextlib import ExitStack
from datetime import timedelta
import gnupg

from django.conf import settings
Expand Down Expand Up @@ -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}
)
7 changes: 7 additions & 0 deletions pulpcore/tasking/pulpcore_worker.py
Expand Up @@ -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,
)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit f85381e

Please sign in to comment.