-
Notifications
You must be signed in to change notification settings - Fork 396
Start background tasks after we fork the process (daemonize) #18886
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
Changes from 9 commits
38b67ca
e39a219
313da3e
ee29030
2f235e3
99b99c2
11c39c5
ed42696
74cd02d
02bb9b4
2ab99d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Start background tasks after we fork the process (daemonize). | ||
MadLittleMods marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,13 @@ def main() -> None: | |
# DB. | ||
hs.setup() | ||
|
||
# This will cause all of the relevant storage classes to be instantiated and call | ||
# `register_background_update_handler(...)`, | ||
# `register_background_index_update(...)`, | ||
# `register_background_validate_constraint(...)`, etc so they are available to use | ||
# if we are asked to run those background updates. | ||
hs.get_storage_controllers() | ||
Comment on lines
+123
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously this was handled in an extremely tenuous fashion because previously Now we just explicitly call this |
||
|
||
if args.run_background_updates: | ||
run_background_updates(hs) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -609,10 +609,28 @@ def run_sighup(*args: Any, **kwargs: Any) -> None: | |
setup_sentry(hs) | ||
setup_sdnotify(hs) | ||
|
||
# If background tasks are running on the main process or this is the worker in | ||
# charge of them, start collecting the phone home stats and shared usage metrics. | ||
# Register background tasks required by this server. This must be done | ||
# somewhat manually due to the background tasks not being registered | ||
# unless handlers are instantiated. | ||
# | ||
# While we could "start" these before the reactor runs, nothing will happen until | ||
# the reactor is running, so we may as well do it here in `start`. | ||
# | ||
# Additionally, this means we also start them after we daemonize and fork the | ||
# process which means we can avoid any potential problems with cputime metrics | ||
# getting confused about the per-thread resource usage appearing to go backwards | ||
# because we're comparing the resource usage (`rusage`) from the original process to | ||
# the forked process. | ||
if hs.config.worker.run_background_tasks: | ||
hs.start_background_tasks() | ||
|
||
# TODO: This should be moved to same pattern we use for other background tasks: | ||
# Add to `REQUIRED_ON_BACKGROUND_TASK_STARTUP` and rely on | ||
# `start_background_tasks` to start it. | ||
await hs.get_common_usage_metrics_manager().setup() | ||
|
||
# TODO: This feels like another pattern that should refactored as one of the | ||
# `REQUIRED_ON_BACKGROUND_TASK_STARTUP` | ||
start_phone_stats_home(hs) | ||
Comment on lines
+627
to
634
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are future refactors to do. But given we're already starting a few snowflake background tasks in |
||
|
||
# We now freeze all allocated objects in the hopes that (almost) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -366,12 +366,6 @@ def setup(self) -> None: | |||||||||||||||||||||||
self.datastores = Databases(self.DATASTORE_CLASS, self) | ||||||||||||||||||||||||
logger.info("Finished setting up.") | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# Register background tasks required by this server. This must be done | ||||||||||||||||||||||||
# somewhat manually due to the background tasks not being registered | ||||||||||||||||||||||||
# unless handlers are instantiated. | ||||||||||||||||||||||||
if self.config.worker.run_background_tasks: | ||||||||||||||||||||||||
self.setup_background_tasks() | ||||||||||||||||||||||||
Comment on lines
-369
to
-373
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In terms of where we moved this code from and to: Relevant starting point is here: synapse/synapse/app/homeserver.py Lines 407 to 417 in b2997a8
The main thing to see here is Previously, we started the background tasks in the Now we start the background tasks in |
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def __del__(self) -> None: | ||||||||||||||||||||||||
""" | ||||||||||||||||||||||||
Called when an the homeserver is garbage collected. | ||||||||||||||||||||||||
|
@@ -410,7 +404,7 @@ def start_listening(self) -> None: # noqa: B027 (no-op by design) | |||||||||||||||||||||||
appropriate listeners. | ||||||||||||||||||||||||
""" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
def setup_background_tasks(self) -> None: | ||||||||||||||||||||||||
def start_background_tasks(self) -> None: | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a rename to align with it's new home in |
||||||||||||||||||||||||
""" | ||||||||||||||||||||||||
Some handlers have side effects on instantiation (like registering | ||||||||||||||||||||||||
background updates). This function causes them to be fetched, and | ||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.