Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 629a951

Browse files
authored
Move additional tasks to the background worker, part 4 (#8513)
1 parent b2486f6 commit 629a951

File tree

11 files changed

+196
-221
lines changed

11 files changed

+196
-221
lines changed

changelog.d/8513.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow running background tasks in a separate worker process.

synapse/handlers/account_validity.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def send_emails():
7070
"send_renewals", self._send_renewal_emails
7171
)
7272

73-
self.clock.looping_call(send_emails, 30 * 60 * 1000)
73+
if hs.config.run_background_tasks:
74+
self.clock.looping_call(send_emails, 30 * 60 * 1000)
7475

7576
async def _send_renewal_emails(self):
7677
"""Gets the list of users whose account is expiring in the amount of time

synapse/handlers/deactivate_account.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, hs: "HomeServer"):
4545

4646
# Start the user parter loop so it can resume parting users from rooms where
4747
# it left off (if it has work left to do).
48-
if hs.config.worker_app is None:
48+
if hs.config.run_background_tasks:
4949
hs.get_reactor().callWhenRunning(self._start_user_parting)
5050

5151
self._account_validity_enabled = hs.config.account_validity.enabled

synapse/handlers/message.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -402,21 +402,23 @@ def __init__(self, hs: "HomeServer"):
402402
self.config.block_events_without_consent_error
403403
)
404404

405+
# we need to construct a ConsentURIBuilder here, as it checks that the necessary
406+
# config options, but *only* if we have a configuration for which we are
407+
# going to need it.
408+
if self._block_events_without_consent_error:
409+
self._consent_uri_builder = ConsentURIBuilder(self.config)
410+
405411
# Rooms which should be excluded from dummy insertion. (For instance,
406412
# those without local users who can send events into the room).
407413
#
408414
# map from room id to time-of-last-attempt.
409415
#
410416
self._rooms_to_exclude_from_dummy_event_insertion = {} # type: Dict[str, int]
411-
412-
# we need to construct a ConsentURIBuilder here, as it checks that the necessary
413-
# config options, but *only* if we have a configuration for which we are
414-
# going to need it.
415-
if self._block_events_without_consent_error:
416-
self._consent_uri_builder = ConsentURIBuilder(self.config)
417+
# The number of forward extremeities before a dummy event is sent.
418+
self._dummy_events_threshold = hs.config.dummy_events_threshold
417419

418420
if (
419-
not self.config.worker_app
421+
self.config.run_background_tasks
420422
and self.config.cleanup_extremities_with_dummy_events
421423
):
422424
self.clock.looping_call(
@@ -431,8 +433,6 @@ def __init__(self, hs: "HomeServer"):
431433

432434
self._ephemeral_events_enabled = hs.config.enable_ephemeral_messages
433435

434-
self._dummy_events_threshold = hs.config.dummy_events_threshold
435-
436436
async def create_event(
437437
self,
438438
requester: Requester,

synapse/handlers/pagination.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def __init__(self, hs: "HomeServer"):
9292
self._retention_allowed_lifetime_min = hs.config.retention_allowed_lifetime_min
9393
self._retention_allowed_lifetime_max = hs.config.retention_allowed_lifetime_max
9494

95-
if hs.config.retention_enabled:
95+
if hs.config.run_background_tasks and hs.config.retention_enabled:
9696
# Run the purge jobs described in the configuration file.
9797
for job in hs.config.retention_purge_jobs:
9898
logger.info("Setting up purge job with config: %s", job)

synapse/handlers/profile.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@
3535
MAX_AVATAR_URL_LEN = 1000
3636

3737

38-
class BaseProfileHandler(BaseHandler):
38+
class ProfileHandler(BaseHandler):
3939
"""Handles fetching and updating user profile information.
4040
41-
BaseProfileHandler can be instantiated directly on workers and will
42-
delegate to master when necessary. The master process should use the
43-
subclass MasterProfileHandler
41+
ProfileHandler can be instantiated directly on workers and will
42+
delegate to master when necessary.
4443
"""
4544

45+
PROFILE_UPDATE_MS = 60 * 1000
46+
PROFILE_UPDATE_EVERY_MS = 24 * 60 * 60 * 1000
47+
4648
def __init__(self, hs):
4749
super().__init__(hs)
4850

@@ -53,6 +55,11 @@ def __init__(self, hs):
5355

5456
self.user_directory_handler = hs.get_user_directory_handler()
5557

58+
if hs.config.run_background_tasks:
59+
self.clock.looping_call(
60+
self._start_update_remote_profile_cache, self.PROFILE_UPDATE_MS
61+
)
62+
5663
async def get_profile(self, user_id):
5764
target_user = UserID.from_string(user_id)
5865

@@ -363,20 +370,6 @@ async def check_profile_query_allowed(self, target_user, requester=None):
363370
raise SynapseError(403, "Profile isn't available", Codes.FORBIDDEN)
364371
raise
365372

366-
367-
class MasterProfileHandler(BaseProfileHandler):
368-
PROFILE_UPDATE_MS = 60 * 1000
369-
PROFILE_UPDATE_EVERY_MS = 24 * 60 * 60 * 1000
370-
371-
def __init__(self, hs):
372-
super().__init__(hs)
373-
374-
assert hs.config.worker_app is None
375-
376-
self.clock.looping_call(
377-
self._start_update_remote_profile_cache, self.PROFILE_UPDATE_MS
378-
)
379-
380373
def _start_update_remote_profile_cache(self):
381374
return run_as_background_process(
382375
"Update remote profile", self._update_remote_profile_cache

synapse/server.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
from synapse.handlers.pagination import PaginationHandler
7676
from synapse.handlers.password_policy import PasswordPolicyHandler
7777
from synapse.handlers.presence import PresenceHandler
78-
from synapse.handlers.profile import BaseProfileHandler, MasterProfileHandler
78+
from synapse.handlers.profile import ProfileHandler
7979
from synapse.handlers.read_marker import ReadMarkerHandler
8080
from synapse.handlers.receipts import ReceiptsHandler
8181
from synapse.handlers.register import RegistrationHandler
@@ -191,7 +191,12 @@ class HomeServer(metaclass=abc.ABCMeta):
191191
"""
192192

193193
REQUIRED_ON_BACKGROUND_TASK_STARTUP = [
194+
"account_validity",
194195
"auth",
196+
"deactivate_account",
197+
"message",
198+
"pagination",
199+
"profile",
195200
"stats",
196201
]
197202

@@ -462,10 +467,7 @@ def get_initial_sync_handler(self) -> InitialSyncHandler:
462467

463468
@cache_in_self
464469
def get_profile_handler(self):
465-
if self.config.worker_app:
466-
return BaseProfileHandler(self)
467-
else:
468-
return MasterProfileHandler(self)
470+
return ProfileHandler(self)
469471

470472
@cache_in_self
471473
def get_event_creation_handler(self) -> EventCreationHandler:

synapse/storage/databases/main/profile.py

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -91,27 +91,6 @@ async def set_profile_avatar_url(
9191
desc="set_profile_avatar_url",
9292
)
9393

94-
95-
class ProfileStore(ProfileWorkerStore):
96-
async def add_remote_profile_cache(
97-
self, user_id: str, displayname: str, avatar_url: str
98-
) -> None:
99-
"""Ensure we are caching the remote user's profiles.
100-
101-
This should only be called when `is_subscribed_remote_profile_for_user`
102-
would return true for the user.
103-
"""
104-
await self.db_pool.simple_upsert(
105-
table="remote_profile_cache",
106-
keyvalues={"user_id": user_id},
107-
values={
108-
"displayname": displayname,
109-
"avatar_url": avatar_url,
110-
"last_check": self._clock.time_msec(),
111-
},
112-
desc="add_remote_profile_cache",
113-
)
114-
11594
async def update_remote_profile_cache(
11695
self, user_id: str, displayname: str, avatar_url: str
11796
) -> int:
@@ -138,6 +117,31 @@ async def maybe_delete_remote_profile_cache(self, user_id):
138117
desc="delete_remote_profile_cache",
139118
)
140119

120+
async def is_subscribed_remote_profile_for_user(self, user_id):
121+
"""Check whether we are interested in a remote user's profile.
122+
"""
123+
res = await self.db_pool.simple_select_one_onecol(
124+
table="group_users",
125+
keyvalues={"user_id": user_id},
126+
retcol="user_id",
127+
allow_none=True,
128+
desc="should_update_remote_profile_cache_for_user",
129+
)
130+
131+
if res:
132+
return True
133+
134+
res = await self.db_pool.simple_select_one_onecol(
135+
table="group_invites",
136+
keyvalues={"user_id": user_id},
137+
retcol="user_id",
138+
allow_none=True,
139+
desc="should_update_remote_profile_cache_for_user",
140+
)
141+
142+
if res:
143+
return True
144+
141145
async def get_remote_profile_cache_entries_that_expire(
142146
self, last_checked: int
143147
) -> Dict[str, str]:
@@ -160,27 +164,23 @@ def _get_remote_profile_cache_entries_that_expire_txn(txn):
160164
_get_remote_profile_cache_entries_that_expire_txn,
161165
)
162166

163-
async def is_subscribed_remote_profile_for_user(self, user_id):
164-
"""Check whether we are interested in a remote user's profile.
165-
"""
166-
res = await self.db_pool.simple_select_one_onecol(
167-
table="group_users",
168-
keyvalues={"user_id": user_id},
169-
retcol="user_id",
170-
allow_none=True,
171-
desc="should_update_remote_profile_cache_for_user",
172-
)
173167

174-
if res:
175-
return True
168+
class ProfileStore(ProfileWorkerStore):
169+
async def add_remote_profile_cache(
170+
self, user_id: str, displayname: str, avatar_url: str
171+
) -> None:
172+
"""Ensure we are caching the remote user's profiles.
176173
177-
res = await self.db_pool.simple_select_one_onecol(
178-
table="group_invites",
174+
This should only be called when `is_subscribed_remote_profile_for_user`
175+
would return true for the user.
176+
"""
177+
await self.db_pool.simple_upsert(
178+
table="remote_profile_cache",
179179
keyvalues={"user_id": user_id},
180-
retcol="user_id",
181-
allow_none=True,
182-
desc="should_update_remote_profile_cache_for_user",
180+
values={
181+
"displayname": displayname,
182+
"avatar_url": avatar_url,
183+
"last_check": self._clock.time_msec(),
184+
},
185+
desc="add_remote_profile_cache",
183186
)
184-
185-
if res:
186-
return True

synapse/storage/databases/main/registration.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,32 @@ def set_expiration_date_for_user_txn(self, txn, user_id, use_delta=False):
862862
values={"expiration_ts_ms": expiration_ts, "email_sent": False},
863863
)
864864

865+
async def get_user_pending_deactivation(self) -> Optional[str]:
866+
"""
867+
Gets one user from the table of users waiting to be parted from all the rooms
868+
they're in.
869+
"""
870+
return await self.db_pool.simple_select_one_onecol(
871+
"users_pending_deactivation",
872+
keyvalues={},
873+
retcol="user_id",
874+
allow_none=True,
875+
desc="get_users_pending_deactivation",
876+
)
877+
878+
async def del_user_pending_deactivation(self, user_id: str) -> None:
879+
"""
880+
Removes the given user to the table of users who need to be parted from all the
881+
rooms they're in, effectively marking that user as fully deactivated.
882+
"""
883+
# XXX: This should be simple_delete_one but we failed to put a unique index on
884+
# the table, so somehow duplicate entries have ended up in it.
885+
await self.db_pool.simple_delete(
886+
"users_pending_deactivation",
887+
keyvalues={"user_id": user_id},
888+
desc="del_user_pending_deactivation",
889+
)
890+
865891

866892
class RegistrationBackgroundUpdateStore(RegistrationWorkerStore):
867893
def __init__(self, database: DatabasePool, db_conn, hs):
@@ -1371,32 +1397,6 @@ async def add_user_pending_deactivation(self, user_id: str) -> None:
13711397
desc="add_user_pending_deactivation",
13721398
)
13731399

1374-
async def del_user_pending_deactivation(self, user_id: str) -> None:
1375-
"""
1376-
Removes the given user to the table of users who need to be parted from all the
1377-
rooms they're in, effectively marking that user as fully deactivated.
1378-
"""
1379-
# XXX: This should be simple_delete_one but we failed to put a unique index on
1380-
# the table, so somehow duplicate entries have ended up in it.
1381-
await self.db_pool.simple_delete(
1382-
"users_pending_deactivation",
1383-
keyvalues={"user_id": user_id},
1384-
desc="del_user_pending_deactivation",
1385-
)
1386-
1387-
async def get_user_pending_deactivation(self) -> Optional[str]:
1388-
"""
1389-
Gets one user from the table of users waiting to be parted from all the rooms
1390-
they're in.
1391-
"""
1392-
return await self.db_pool.simple_select_one_onecol(
1393-
"users_pending_deactivation",
1394-
keyvalues={},
1395-
retcol="user_id",
1396-
allow_none=True,
1397-
desc="get_users_pending_deactivation",
1398-
)
1399-
14001400
async def validate_threepid_session(
14011401
self, session_id: str, client_secret: str, token: str, current_ts: int
14021402
) -> Optional[str]:

0 commit comments

Comments
 (0)