Skip to content

Commit 37b5bdf

Browse files
fix: queue membership count refreshes for cohort segments (#8438)
Co-authored-by: flagsmith-engineering[bot] <flagsmith-engineering[bot]@users.noreply.github.com>
1 parent 76da16c commit 37b5bdf

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

api/cohorts/services.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from uuid import UUID
55

66
import structlog
7+
from django.conf import settings
78
from django.db import transaction
89
from django.db.models import F, QuerySet
910
from django.utils import timezone
@@ -158,6 +159,13 @@ def create_cohort(
158159
project__id=environment.project_id,
159160
organisation__id=environment.project.organisation_id,
160161
)
162+
if settings.CLICKHOUSE_ENABLED:
163+
from segment_membership.services import enqueue_membership_refresh
164+
165+
# The segment is created directly rather than through the segment
166+
# serializer, which is where membership count refreshes are normally
167+
# queued from.
168+
enqueue_membership_refresh(environment.project)
161169
return cohort
162170

163171

api/tests/unit/cohorts/test_services.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
from flag_engine.segments.constants import IS_SET
5+
from pytest_django.fixtures import SettingsWrapper
56
from pytest_mock import MockerFixture
67
from pytest_structlog import StructuredLogCapture
78
from rest_framework.exceptions import ValidationError
@@ -546,3 +547,21 @@ def test_sync_cohort_memberships_from_csv__edge_cohort__applies_traits(
546547
assert document["system_traits"] == {edge_cohort.system_trait_key: True}
547548
membership = CohortMembership.objects.get(cohort=edge_cohort)
548549
assert membership.state == CohortMembershipState.APPLIED
550+
551+
552+
def test_create_cohort__clickhouse_enabled__queues_membership_refresh(
553+
environment: Environment,
554+
settings: SettingsWrapper,
555+
mocker: MockerFixture,
556+
) -> None:
557+
# Given
558+
settings.CLICKHOUSE_ENABLED = True
559+
enqueue_mock = mocker.patch(
560+
"segment_membership.services.enqueue_membership_refresh"
561+
)
562+
563+
# When
564+
create_cohort(environment=environment, name="Beta users")
565+
566+
# Then
567+
enqueue_mock.assert_called_once_with(environment.project)

docs/docs/deployment-self-hosting/observability/_events-catalogue.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Attributes:
7474
### `cohorts.cohort.created`
7575

7676
Logged at `info` from:
77-
- `api/cohorts/services.py:153`
77+
- `api/cohorts/services.py:154`
7878

7979
Attributes:
8080
- `cohort.id`
@@ -86,7 +86,7 @@ Attributes:
8686
### `cohorts.cohort.deleted`
8787

8888
Logged at `info` from:
89-
- `api/cohorts/services.py:459`
89+
- `api/cohorts/services.py:467`
9090

9191
Attributes:
9292
- `cohort.id`
@@ -95,7 +95,7 @@ Attributes:
9595
### `cohorts.cohort.deletion_requested`
9696

9797
Logged at `info` from:
98-
- `api/cohorts/services.py:443`
98+
- `api/cohorts/services.py:451`
9999

100100
Attributes:
101101
- `cohort.id`
@@ -104,7 +104,7 @@ Attributes:
104104
### `cohorts.csv.synced`
105105

106106
Logged at `info` from:
107-
- `api/cohorts/services.py:415`
107+
- `api/cohorts/services.py:423`
108108

109109
Attributes:
110110
- `adds.count`
@@ -117,7 +117,7 @@ Attributes:
117117
### `cohorts.membership.adds_received`
118118

119119
Logged at `info` from:
120-
- `api/cohorts/services.py:266`
120+
- `api/cohorts/services.py:274`
121121

122122
Attributes:
123123
- `cohort.id`
@@ -127,7 +127,7 @@ Attributes:
127127
### `cohorts.membership.applied`
128128

129129
Logged at `info` from:
130-
- `api/cohorts/services.py:104`
130+
- `api/cohorts/services.py:105`
131131

132132
Attributes:
133133
- `adds.count`
@@ -155,7 +155,7 @@ Attributes:
155155
### `cohorts.membership.removals_received`
156156

157157
Logged at `info` from:
158-
- `api/cohorts/services.py:291`
158+
- `api/cohorts/services.py:299`
159159

160160
Attributes:
161161
- `cohort.id`

0 commit comments

Comments
 (0)