Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sse_recurring_task): reload sse/tasks #3108

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions api/task_processor/management/commands/runprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from django.core.management import BaseCommand
from django.utils import timezone

from task_processor import tasks
from sse import tasks as sse_tasks
from task_processor import tasks as processor_tasks
from task_processor.task_registry import registered_tasks
from task_processor.thread_monitoring import (
clear_unhealthy_threads,
Expand All @@ -20,6 +21,8 @@

logger = logging.getLogger(__name__)

TASKS_MODULES_TO_RELOAD = [processor_tasks, sse_tasks]


class Command(BaseCommand):
def __init__(self, *args, **kwargs):
Expand All @@ -33,10 +36,14 @@ def __init__(self, *args, **kwargs):
# environment variable.
os.environ["RUN_BY_PROCESSOR"] = "True"

# Since the tasks module is loaded by the ready method in TaskProcessorConfig
# which is run before the command is initialised, we need to reload the internal
# tasks module here to make sure recurring tasks are registered correctly.
reload(tasks)
# Since all the apps are loaded before the command is initialised,
# we need to reload some of those modules(that contains recurring tasks)
# to ensure the tasks are registered correctly
# e.g the tasks module is loaded by the ready method in TaskProcessorConfig
# which is run before the command is initialised

for module in TASKS_MODULES_TO_RELOAD:
reload(module)

signal.signal(signal.SIGINT, self._exit_gracefully)
signal.signal(signal.SIGTERM, self._exit_gracefully)
Expand Down
27 changes: 27 additions & 0 deletions api/tests/unit/sse/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
from datetime import datetime
from typing import Callable
from unittest import mock
from unittest.mock import call

import pytest
Expand All @@ -15,6 +17,10 @@
send_environment_update_message_for_project,
update_sse_usage,
)
from task_processor.management.commands.runprocessor import (
Command as RunProcessorCommand,
)
from task_processor.models import RecurringTask


def test_send_environment_update_message_for_project_make_correct_request(
Expand Down Expand Up @@ -138,3 +144,24 @@ def test_track_sse_usage(
bucket=influxdb_bucket,
record=mocked_influx_point().field().tag().tag().tag().time(),
)


@mock.patch.dict(os.environ, {})
@pytest.mark.django_db
def test_track_sse_usage_is_installed_correctly(
settings: SettingsWrapper,
):
# Given
settings.AWS_SSE_LOGS_BUCKET_NAME = "test_bucket"

# When
# Initialising the command should save the task to the database
RunProcessorCommand()

# Then
assert (
RecurringTask.objects.filter(
task_identifier=f"tasks.{update_sse_usage.__name__}"
).exists()
is True
)