Skip to content

Commit 8f64f62

Browse files
authored
Always flush entity manager challenges (#12018)
1 parent 69e076b commit 8f64f62

File tree

4 files changed

+399
-398
lines changed

4 files changed

+399
-398
lines changed

packages/discovery-provider/src/challenges/challenge_event_bus.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import logging
32
from collections import defaultdict
43
from contextlib import contextmanager
54
from datetime import datetime
@@ -49,8 +48,9 @@
4948
trending_underground_track_challenge_manager,
5049
)
5150
from src.utils.redis_connection import get_redis
51+
from src.utils.structured_logger import StructuredLogger
5252

53-
logger = logging.getLogger(__name__)
53+
logger = StructuredLogger(__name__)
5454
REDIS_QUEUE_PREFIX = "challenges-event-queue"
5555

5656

packages/discovery-provider/src/tasks/entity_manager/entities/comment.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,13 @@ def create_comment(params: ManageEntityParameters):
175175

176176
params.add_record(comment_id, comment_record, EntityType.COMMENT)
177177

178-
with challenge_bus.use_scoped_dispatch_queue():
179-
challenge_bus.dispatch(
180-
ChallengeEvent.first_weekly_comment,
181-
params.block_number,
182-
params.block_datetime,
183-
user_id,
184-
{"created_at": params.block_datetime.timestamp()},
185-
)
178+
challenge_bus.dispatch(
179+
ChallengeEvent.first_weekly_comment,
180+
params.block_number,
181+
params.block_datetime,
182+
user_id,
183+
{"created_at": params.block_datetime.timestamp()},
184+
)
186185

187186
if (
188187
not is_reply
@@ -746,18 +745,17 @@ def pin_comment(params: ManageEntityParameters):
746745
)
747746

748747
if artist_is_verified:
749-
with params.challenge_bus.use_scoped_dispatch_queue():
750-
params.challenge_bus.dispatch(
751-
ChallengeEvent.pinned_comment,
752-
params.block_number,
753-
params.block_datetime,
754-
comment_user_id,
755-
{
756-
"track_id": track_id,
757-
"comment_id": comment_id,
758-
"track_owner_id": track_owner_id,
759-
},
760-
)
748+
params.challenge_bus.dispatch(
749+
ChallengeEvent.pinned_comment,
750+
params.block_number,
751+
params.block_datetime,
752+
comment_user_id,
753+
{
754+
"track_id": track_id,
755+
"comment_id": comment_id,
756+
"track_owner_id": track_owner_id,
757+
},
758+
)
761759

762760
params.add_record(track_id, track, EntityType.TRACK)
763761

packages/discovery-provider/src/tasks/entity_manager/entities/social_features.py

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -94,50 +94,47 @@ def create_social_record(params: ManageEntityParameters):
9494
record_type=record_type,
9595
)
9696

97-
with params.challenge_bus.use_scoped_dispatch_queue():
98-
# dispatch repost, favorite, follow challenges
99-
if create_record and params.action in action_to_challenge_event:
100-
challenge_event = action_to_challenge_event[params.action]
101-
params.challenge_bus.dispatch(
102-
challenge_event,
103-
params.block_number,
104-
params.block_datetime,
105-
params.user_id,
106-
)
97+
# dispatch repost, favorite, follow challenges
98+
if create_record and params.action in action_to_challenge_event:
99+
challenge_event = action_to_challenge_event[params.action]
100+
params.challenge_bus.dispatch(
101+
challenge_event,
102+
params.block_number,
103+
params.block_datetime,
104+
params.user_id,
105+
)
106+
107+
# dispatch verified cosign challenge
108+
if (
109+
params.action in [Action.SAVE, Action.REPOST]
110+
and params.entity_type == EntityType.TRACK
111+
and params.existing_records[EntityType.TRACK][params.entity_id].remix_of
112+
):
113+
remixed_track = (
114+
params.session.query(Track)
115+
.join(Remix, Remix.parent_track_id == Track.track_id)
116+
.filter(Remix.child_track_id == params.entity_id)
117+
.first()
118+
)
119+
remixer = params.existing_records[EntityType.TRACK][params.entity_id].owner_id
120+
cosigner = params.existing_records[EntityType.USER][params.user_id]
107121

108-
# dispatch verified cosign challenge
109122
if (
110-
params.action in [Action.SAVE, Action.REPOST]
111-
and params.entity_type == EntityType.TRACK
112-
and params.existing_records[EntityType.TRACK][params.entity_id].remix_of
123+
remixed_track
124+
and remixed_track.owner_id == params.user_id
125+
and cosigner.is_verified
113126
):
114-
remixed_track = (
115-
params.session.query(Track)
116-
.join(Remix, Remix.parent_track_id == Track.track_id)
117-
.filter(Remix.child_track_id == params.entity_id)
118-
.first()
127+
params.challenge_bus.dispatch(
128+
ChallengeEvent.cosign,
129+
params.block_number,
130+
params.block_datetime,
131+
remixer,
132+
{
133+
"original_track_owner_id": params.user_id,
134+
"remix_track_id": params.entity_id,
135+
"cosign_date": params.block_datetime.timestamp(),
136+
},
119137
)
120-
remixer = params.existing_records[EntityType.TRACK][
121-
params.entity_id
122-
].owner_id
123-
cosigner = params.existing_records[EntityType.USER][params.user_id]
124-
125-
if (
126-
remixed_track
127-
and remixed_track.owner_id == params.user_id
128-
and cosigner.is_verified
129-
):
130-
params.challenge_bus.dispatch(
131-
ChallengeEvent.cosign,
132-
params.block_number,
133-
params.block_datetime,
134-
remixer,
135-
{
136-
"original_track_owner_id": params.user_id,
137-
"remix_track_id": params.entity_id,
138-
"cosign_date": params.block_datetime.timestamp(),
139-
},
140-
)
141138

142139

143140
def get_attribute_from_record_metadata(params, attribute):

0 commit comments

Comments
 (0)