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

Commit

Permalink
Allow modules to run looping call on all instances (#10638)
Browse files Browse the repository at this point in the history
By default the calls only ran on the worker configured to run background
tasks.
  • Loading branch information
erikjohnston authored Aug 18, 2021
1 parent 703e3a9 commit 5581dd7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/10638.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add option to allow modules to run periodic tasks on all instances, rather than just the one configured to run background tasks.
9 changes: 8 additions & 1 deletion synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,15 @@ def looping_background_call(
msec: float,
*args,
desc: Optional[str] = None,
run_on_all_instances: bool = False,
**kwargs,
):
"""Wraps a function as a background process and calls it repeatedly.
NOTE: Will only run on the instance that is configured to run
background processes (which is the main process by default), unless
`run_on_all_workers` is set.
Waits `msec` initially before calling `f` for the first time.
Args:
Expand All @@ -618,12 +623,14 @@ def looping_background_call(
msec: How long to wait between calls in milliseconds.
*args: Positional arguments to pass to function.
desc: The background task's description. Default to the function's name.
run_on_all_instances: Whether to run this on all instances, rather
than just the instance configured to run background tasks.
**kwargs: Key arguments to pass to function.
"""
if desc is None:
desc = f.__name__

if self._hs.config.run_background_tasks:
if self._hs.config.run_background_tasks or run_on_all_instances:
self._clock.looping_call(
run_as_background_process,
msec,
Expand Down

0 comments on commit 5581dd7

Please sign in to comment.