summaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
authorAleš Mrázek <ales.mrazek@nic.cz>2024-11-21 14:18:31 +0100
committerAleš Mrázek <ales.mrazek@nic.cz>2024-11-21 14:18:31 +0100
commita22c43ee60cb3e42f3c82787040142a3df9d0bb3 (patch)
treea65147601a4fcbee7bfeca4efe81035d5cd6c7e4 /python
parentMerge !1638: ci: pkg: add Ubuntu 24.10 (diff)
downloadknot-resolver-a22c43ee60cb3e42f3c82787040142a3df9d0bb3.tar.xz
knot-resolver-a22c43ee60cb3e42f3c82787040142a3df9d0bb3.zip
python: controller: write subprocess config synchronously
Diffstat (limited to 'python')
-rw-r--r--python/knot_resolver/controller/interface.py57
1 files changed, 27 insertions, 30 deletions
diff --git a/python/knot_resolver/controller/interface.py b/python/knot_resolver/controller/interface.py
index 0544dac2..43c24257 100644
--- a/python/knot_resolver/controller/interface.py
+++ b/python/knot_resolver/controller/interface.py
@@ -14,7 +14,6 @@ from knot_resolver.controller.exceptions import SubprocessControllerError
from knot_resolver.controller.registered_workers import register_worker, unregister_worker
from knot_resolver.datamodel.config_schema import KresConfig
from knot_resolver.manager.constants import kresd_config_file, policy_loader_config_file
-from knot_resolver.utils.async_utils import writefile
logger = logging.getLogger(__name__)
@@ -111,19 +110,33 @@ class Subprocess(ABC):
self._config = config
self._registered_worker: bool = False
+ self._config_file: Optional[Path] = None
+ if self.type is SubprocessType.KRESD:
+ self._config_file = kresd_config_file(self._config, self.id)
+ elif self.type is SubprocessType.POLICY_LOADER:
+ self._config_file = policy_loader_config_file(self._config)
+
+ def _render_lua(self) -> Optional[str]:
+ if self.type is SubprocessType.KRESD:
+ return self._config.render_lua()
+ if self.type is SubprocessType.POLICY_LOADER:
+ return self._config.render_lua_policy()
+ return None
+
+ def _write_config(self) -> None:
+ config_lua = self._render_lua()
+ if config_lua and self._config_file:
+ with open(self._config_file, "w", encoding="utf8") as file:
+ file.write(config_lua)
+
+ def _unlink_config(self) -> None:
+ if self._config_file:
+ self._config_file.unlink(missing_ok=True)
+
async def start(self, new_config: Optional[KresConfig] = None) -> None:
if new_config:
self._config = new_config
-
- config_file: Optional[Path] = None
- if self.type is SubprocessType.KRESD:
- config_lua = self._config.render_lua()
- config_file = kresd_config_file(self._config, self.id)
- await writefile(config_file, config_lua)
- elif self.type is SubprocessType.POLICY_LOADER:
- config_lua = self._config.render_lua_policy()
- config_file = policy_loader_config_file(self._config)
- await writefile(config_file, config_lua)
+ self._write_config()
try:
await self._start()
@@ -131,8 +144,7 @@ class Subprocess(ABC):
register_worker(self)
self._registered_worker = True
except SubprocessControllerError as e:
- if config_file:
- config_file.unlink()
+ self._unlink_config()
raise e
async def apply_new_config(self, new_config: KresConfig) -> None:
@@ -140,16 +152,7 @@ class Subprocess(ABC):
# update config file
logger.debug(f"Writing config file for {self.id}")
-
- config_file: Optional[Path] = None
- if self.type is SubprocessType.KRESD:
- config_lua = self._config.render_lua()
- config_file = kresd_config_file(self._config, self.id)
- await writefile(config_file, config_lua)
- elif self.type is SubprocessType.POLICY_LOADER:
- config_lua = self._config.render_lua_policy()
- config_file = policy_loader_config_file(self._config)
- await writefile(config_file, config_lua)
+ self._write_config()
# update runtime status
logger.debug(f"Restarting {self.id}")
@@ -166,13 +169,7 @@ class Subprocess(ABC):
Remove temporary files and all traces of this instance running. It is NOT SAFE to call this while
the kresd is running, because it will break automatic restarts (at the very least).
"""
-
- if self.type is SubprocessType.KRESD:
- config_file = kresd_config_file(self._config, self.id)
- config_file.unlink()
- elif self.type is SubprocessType.POLICY_LOADER:
- config_file = policy_loader_config_file(self._config)
- config_file.unlink()
+ self._unlink_config()
def __eq__(self, o: object) -> bool:
return isinstance(o, type(self)) and o.type == self.type and o.id == self.id