2021-11-08 09:08:02 -07:00
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
import logging
|
2021-11-19 12:39:46 -07:00
|
|
|
from http import HTTPStatus
|
2021-11-08 09:08:02 -07:00
|
|
|
from typing import TYPE_CHECKING, Tuple
|
|
|
|
|
|
|
|
from synapse.api.errors import SynapseError
|
2021-11-19 12:39:46 -07:00
|
|
|
from synapse.http.servlet import (
|
|
|
|
RestServlet,
|
|
|
|
assert_params_in_dict,
|
|
|
|
parse_json_object_from_request,
|
|
|
|
)
|
2021-11-08 09:08:02 -07:00
|
|
|
from synapse.http.site import SynapseRequest
|
2021-12-08 09:59:40 -07:00
|
|
|
from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin
|
2021-11-08 09:08:02 -07:00
|
|
|
from synapse.types import JsonDict
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class BackgroundUpdateEnabledRestServlet(RestServlet):
|
|
|
|
"""Allows temporarily disabling background updates"""
|
|
|
|
|
2021-11-19 12:39:46 -07:00
|
|
|
PATTERNS = admin_patterns("/background_updates/enabled$")
|
2021-11-08 09:08:02 -07:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-11-19 12:39:46 -07:00
|
|
|
self._auth = hs.get_auth()
|
|
|
|
self._data_stores = hs.get_datastores()
|
2021-11-08 09:08:02 -07:00
|
|
|
|
|
|
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2021-12-08 09:59:40 -07:00
|
|
|
await assert_requester_is_admin(self._auth, request)
|
2021-11-08 09:08:02 -07:00
|
|
|
|
|
|
|
# We need to check that all configured databases have updates enabled.
|
|
|
|
# (They *should* all be in sync.)
|
2021-11-19 12:39:46 -07:00
|
|
|
enabled = all(db.updates.enabled for db in self._data_stores.databases)
|
2021-11-08 09:08:02 -07:00
|
|
|
|
2021-11-19 12:39:46 -07:00
|
|
|
return HTTPStatus.OK, {"enabled": enabled}
|
2021-11-08 09:08:02 -07:00
|
|
|
|
|
|
|
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2021-12-08 09:59:40 -07:00
|
|
|
await assert_requester_is_admin(self._auth, request)
|
2021-11-08 09:08:02 -07:00
|
|
|
|
|
|
|
body = parse_json_object_from_request(request)
|
|
|
|
|
|
|
|
enabled = body.get("enabled", True)
|
|
|
|
|
|
|
|
if not isinstance(enabled, bool):
|
2021-11-19 12:39:46 -07:00
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST, "'enabled' parameter must be a boolean"
|
|
|
|
)
|
2021-11-08 09:08:02 -07:00
|
|
|
|
2021-11-19 12:39:46 -07:00
|
|
|
for db in self._data_stores.databases:
|
2021-11-08 09:08:02 -07:00
|
|
|
db.updates.enabled = enabled
|
|
|
|
|
|
|
|
# If we're re-enabling them ensure that we start the background
|
|
|
|
# process again.
|
|
|
|
if enabled:
|
|
|
|
db.updates.start_doing_background_updates()
|
|
|
|
|
2021-11-19 12:39:46 -07:00
|
|
|
return HTTPStatus.OK, {"enabled": enabled}
|
2021-11-08 09:08:02 -07:00
|
|
|
|
|
|
|
|
|
|
|
class BackgroundUpdateRestServlet(RestServlet):
|
|
|
|
"""Fetch information about background updates"""
|
|
|
|
|
2021-11-19 12:39:46 -07:00
|
|
|
PATTERNS = admin_patterns("/background_updates/status$")
|
2021-11-08 09:08:02 -07:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-11-19 12:39:46 -07:00
|
|
|
self._auth = hs.get_auth()
|
|
|
|
self._data_stores = hs.get_datastores()
|
2021-11-08 09:08:02 -07:00
|
|
|
|
|
|
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2021-12-08 09:59:40 -07:00
|
|
|
await assert_requester_is_admin(self._auth, request)
|
2021-11-08 09:08:02 -07:00
|
|
|
|
|
|
|
# We need to check that all configured databases have updates enabled.
|
|
|
|
# (They *should* all be in sync.)
|
2021-11-19 12:39:46 -07:00
|
|
|
enabled = all(db.updates.enabled for db in self._data_stores.databases)
|
2021-11-08 09:08:02 -07:00
|
|
|
|
|
|
|
current_updates = {}
|
|
|
|
|
2021-11-19 12:39:46 -07:00
|
|
|
for db in self._data_stores.databases:
|
2021-11-08 09:08:02 -07:00
|
|
|
update = db.updates.get_current_update()
|
|
|
|
if not update:
|
|
|
|
continue
|
|
|
|
|
|
|
|
current_updates[db.name()] = {
|
|
|
|
"name": update.name,
|
|
|
|
"total_item_count": update.total_item_count,
|
|
|
|
"total_duration_ms": update.total_duration_ms,
|
|
|
|
"average_items_per_ms": update.average_items_per_ms(),
|
|
|
|
}
|
|
|
|
|
2021-11-19 12:39:46 -07:00
|
|
|
return HTTPStatus.OK, {"enabled": enabled, "current_updates": current_updates}
|
|
|
|
|
|
|
|
|
|
|
|
class BackgroundUpdateStartJobRestServlet(RestServlet):
|
|
|
|
"""Allows to start specific background updates"""
|
|
|
|
|
2021-12-08 09:59:40 -07:00
|
|
|
PATTERNS = admin_patterns("/background_updates/start_job$")
|
2021-11-19 12:39:46 -07:00
|
|
|
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
|
|
|
self._auth = hs.get_auth()
|
2022-02-23 04:04:02 -07:00
|
|
|
self._store = hs.get_datastores().main
|
2021-11-19 12:39:46 -07:00
|
|
|
|
|
|
|
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2021-12-08 09:59:40 -07:00
|
|
|
await assert_requester_is_admin(self._auth, request)
|
2021-11-19 12:39:46 -07:00
|
|
|
|
|
|
|
body = parse_json_object_from_request(request)
|
|
|
|
assert_params_in_dict(body, ["job_name"])
|
|
|
|
|
|
|
|
job_name = body["job_name"]
|
|
|
|
|
|
|
|
if job_name == "populate_stats_process_rooms":
|
2022-01-13 17:44:18 -07:00
|
|
|
jobs = [("populate_stats_process_rooms", "{}", "")]
|
2021-11-19 12:39:46 -07:00
|
|
|
elif job_name == "regenerate_directory":
|
|
|
|
jobs = [
|
2022-01-13 17:44:18 -07:00
|
|
|
("populate_user_directory_createtables", "{}", ""),
|
|
|
|
(
|
|
|
|
"populate_user_directory_process_rooms",
|
|
|
|
"{}",
|
|
|
|
"populate_user_directory_createtables",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"populate_user_directory_process_users",
|
|
|
|
"{}",
|
|
|
|
"populate_user_directory_process_rooms",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"populate_user_directory_cleanup",
|
|
|
|
"{}",
|
|
|
|
"populate_user_directory_process_users",
|
|
|
|
),
|
2021-11-19 12:39:46 -07:00
|
|
|
]
|
|
|
|
else:
|
|
|
|
raise SynapseError(HTTPStatus.BAD_REQUEST, "Invalid job_name")
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self._store.db_pool.simple_insert_many(
|
|
|
|
table="background_updates",
|
2022-01-13 17:44:18 -07:00
|
|
|
keys=("update_name", "progress_json", "depends_on"),
|
2021-11-19 12:39:46 -07:00
|
|
|
values=jobs,
|
|
|
|
desc=f"admin_api_run_{job_name}",
|
|
|
|
)
|
|
|
|
except self._store.db_pool.engine.module.IntegrityError:
|
|
|
|
raise SynapseError(
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
"Job %s is already in queue of background updates." % (job_name,),
|
|
|
|
)
|
|
|
|
|
|
|
|
self._store.db_pool.updates.start_doing_background_updates()
|
|
|
|
|
|
|
|
return HTTPStatus.OK, {}
|