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

Commit

Permalink
Merge commit 'b44bdd7f7' into dinsic
Browse files Browse the repository at this point in the history
* commit 'b44bdd7f7':
  Support running multiple media repos. (#7706)
  • Loading branch information
anoadragon453 committed Aug 3, 2020
2 parents 3261eb7 + b44bdd7 commit 6553158
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/7706.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for running multiple media repository workers. See [docs/workers.md](docs/workers.md) for instructions.
7 changes: 6 additions & 1 deletion docs/workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,12 @@ expose the `media` resource. For example:
- media
```

Note this worker cannot be load-balanced: only one instance should be active.
Note that if running multiple media repositories they must be on the same server
and you must configure a single instance to run the background tasks, e.g.:

```yaml
media_instance_running_background_jobs: "media-repository-1"
```

### `synapse.app.client_reader`

Expand Down
6 changes: 6 additions & 0 deletions synapse/config/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def read_config(self, config, **kwargs):
else:
self.can_load_media_repo = True

# Whether this instance should be the one to run the background jobs to
# e.g clean up old URL previews.
self.media_instance_running_background_jobs = config.get(
"media_instance_running_background_jobs",
)

self.max_upload_size = self.parse_size(config.get("max_upload_size", "10M"))
self.max_image_pixels = self.parse_size(config.get("max_image_pixels", "32M"))
self.max_spider_size = self.parse_size(config.get("max_spider_size", "10M"))
Expand Down
18 changes: 15 additions & 3 deletions synapse/rest/media/v1/preview_url_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ def __init__(self, hs, media_repo, media_storage):
self.primary_base_path = media_repo.primary_base_path
self.media_storage = media_storage

# We run the background jobs if we're the instance specified (or no
# instance is specified, where we assume there is only one instance
# serving media).
instance_running_jobs = hs.config.media.media_instance_running_background_jobs
self._worker_run_media_background_jobs = (
instance_running_jobs is None
or instance_running_jobs == hs.get_instance_name()
)

self.url_preview_url_blacklist = hs.config.url_preview_url_blacklist
self.url_preview_accept_language = hs.config.url_preview_accept_language

Expand All @@ -94,9 +103,10 @@ def __init__(self, hs, media_repo, media_storage):
expiry_ms=60 * 60 * 1000,
)

self._cleaner_loop = self.clock.looping_call(
self._start_expire_url_cache_data, 10 * 1000
)
if self._worker_run_media_background_jobs:
self._cleaner_loop = self.clock.looping_call(
self._start_expire_url_cache_data, 10 * 1000
)

def render_OPTIONS(self, request):
request.setHeader(b"Allow", b"OPTIONS, GET")
Expand Down Expand Up @@ -397,6 +407,8 @@ async def _expire_url_cache_data(self):
"""
# TODO: Delete from backup media store

assert self._worker_run_media_background_jobs

now = self.clock.time_msec()

logger.debug("Running url preview cache expiry")
Expand Down

0 comments on commit 6553158

Please sign in to comment.