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

Commit 71ade09

Browse files
committed
Allow background tasks to be run on a separate worker.
1 parent 4f3096d commit 71ade09

21 files changed

+82
-35
lines changed

changelog.d/8369.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/app/generic_worker.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ def start(config_options):
885885
# For backwards compatibility let any of the old app names.
886886
assert config.worker_app in (
887887
"synapse.app.appservice",
888+
"synapse.app.background_worker",
888889
"synapse.app.client_reader",
889890
"synapse.app.event_creator",
890891
"synapse.app.federation_reader",
@@ -961,6 +962,22 @@ def start(config_options):
961962
# For other worker types we force this to off.
962963
config.worker.send_federation = False
963964

965+
if config.worker_app == "synapse.app.background_worker":
966+
if config.worker.run_background_tasks:
967+
sys.stderr.write(
968+
"\nThe run_background_tasks must be disabled in the main synapse process"
969+
"\nbefore they can be run in a separate worker."
970+
"\nPlease add ``run_background_tasks: false`` to the main config"
971+
"\n"
972+
)
973+
sys.exit(1)
974+
975+
# Force the background tasks to start since they will be disabled in the main config
976+
config.worker.run_background_tasks = True
977+
else:
978+
# For other worker types we force this to off.
979+
config.worker.run_background_tasks = False
980+
964981
synapse.events.USE_FROZEN_DICTS = config.use_frozen_dicts
965982

966983
hs = GenericWorkerServer(

synapse/app/homeserver.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,16 +620,18 @@ def generate_user_daily_visit_stats():
620620
# Rather than update on per session basis, batch up the requests.
621621
# If you increase the loop period, the accuracy of user_daily_visits
622622
# table will decrease
623-
clock.looping_call(generate_user_daily_visit_stats, 5 * 60 * 1000)
623+
if hs.config.run_background_tasks:
624+
clock.looping_call(generate_user_daily_visit_stats, 5 * 60 * 1000)
624625

625626
# monthly active user limiting functionality
626627
def reap_monthly_active_users():
627628
return run_as_background_process(
628629
"reap_monthly_active_users", hs.get_datastore().reap_monthly_active_users
629630
)
630631

631-
clock.looping_call(reap_monthly_active_users, 1000 * 60 * 60)
632-
reap_monthly_active_users()
632+
if hs.config.run_background_tasks:
633+
clock.looping_call(reap_monthly_active_users, 1000 * 60 * 60)
634+
reap_monthly_active_users()
633635

634636
async def generate_monthly_active_users():
635637
current_mau_count = 0
@@ -656,11 +658,13 @@ def start_generate_monthly_active_users():
656658
)
657659

658660
start_generate_monthly_active_users()
659-
if hs.config.limit_usage_by_mau or hs.config.mau_stats_only:
661+
if hs.config.run_background_tasks and (
662+
hs.config.limit_usage_by_mau or hs.config.mau_stats_only
663+
):
660664
clock.looping_call(start_generate_monthly_active_users, 5 * 60 * 1000)
661665
# End of monthly active user settings
662666

663-
if hs.config.report_stats:
667+
if hs.config.run_background_tasks and hs.config.report_stats:
664668
logger.info("Scheduling stats reporting for 3 hour intervals")
665669
clock.looping_call(start_phone_stats_home, 3 * 60 * 60 * 1000)
666670

synapse/config/workers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ def read_config(self, config, **kwargs):
132132

133133
self.events_shard_config = ShardedWorkerHandlingConfig(self.writers.events)
134134

135+
# Whether this worker should run background tasks or not.
136+
#
137+
# As a note for developers, the background tasks guarded by this should
138+
# be able to run on only a single instance (meaning that they don't
139+
# depend on any in-memory state of a particular worker).
140+
#
141+
# Effort is not made to ensure only a single instance of these tasks is
142+
# running.
143+
self.run_background_tasks = config.get("run_background_tasks", True)
144+
135145
def generate_config_section(self, config_dir_path, server_name, **kwargs):
136146
return """\
137147
## Workers ##

synapse/handlers/account_validity.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def __init__(self, hs):
4242
if (
4343
self._account_validity.enabled
4444
and self._account_validity.renew_by_email_enabled
45+
and hs.config.run_background_tasks
4546
):
4647
# Don't do email-specific configuration if renewal by email is disabled.
4748
self._template_html = self.config.account_validity_template_html

synapse/handlers/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def __init__(self, hs):
203203
self._clock = self.hs.get_clock()
204204

205205
# Expire old UI auth sessions after a period of time.
206-
if hs.config.worker_app is None:
206+
if hs.config.run_background_tasks:
207207
self._clock.looping_call(
208208
run_as_background_process,
209209
5 * 60 * 1000,

synapse/handlers/deactivate_account.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(self, hs):
4242

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

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

synapse/handlers/device.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,12 +522,13 @@ def __init__(self, hs, device_handler):
522522

523523
# Attempt to resync out of sync device lists every 30s.
524524
self._resync_retry_in_progress = False
525-
self.clock.looping_call(
526-
run_as_background_process,
527-
30 * 1000,
528-
func=self._maybe_retry_device_resync,
529-
desc="_maybe_retry_device_resync",
530-
)
525+
if hs.config.run_background_tasks:
526+
self.clock.looping_call(
527+
run_as_background_process,
528+
30 * 1000,
529+
func=self._maybe_retry_device_resync,
530+
desc="_maybe_retry_device_resync",
531+
)
531532

532533
@trace
533534
async def incoming_device_list_update(self, origin, edu_content):

synapse/handlers/message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def __init__(self, hs: "HomeServer"):
413413
self._consent_uri_builder = ConsentURIBuilder(self.config)
414414

415415
if (
416-
not self.config.worker_app
416+
self.config.run_background_tasks
417417
and self.config.cleanup_extremities_with_dummy_events
418418
):
419419
self.clock.looping_call(

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)

0 commit comments

Comments
 (0)