comparison libervia/backend/core/main.py @ 4266:9fc3d28bc3f6

core (main): add a mechanism to have a shared temp directory: this directory may be used to share files between backend and frontends. Normally, an os-dependent temporary directory is created for that, but if this option is set, the directory will be created in <local_dir>/<cache_dir>, which may be useful in some use case (e.g. containerized frontends and backend).
author Goffi <goffi@goffi.org>
date Wed, 12 Jun 2024 22:47:34 +0200
parents e11b13418ba6
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4265:2417ad1d0f23 4266:9fc3d28bc3f6
20 import os.path 20 import os.path
21 import uuid 21 import uuid
22 import hashlib 22 import hashlib
23 import copy 23 import copy
24 from pathlib import Path 24 from pathlib import Path
25 import shutil
25 from typing import Optional, List, Tuple, Dict, cast 26 from typing import Optional, List, Tuple, Dict, cast
26 27
27 from wokkel.data_form import Option 28 from wokkel.data_form import Option
28 from libervia import backend 29 from libervia import backend
29 from libervia.backend.core.i18n import _, D_, language_switch 30 from libervia.backend.core.i18n import _, D_, language_switch
101 try: 102 try:
102 self.bridge = bridge_module.bridge() 103 self.bridge = bridge_module.bridge()
103 except exceptions.BridgeInitError: 104 except exceptions.BridgeInitError:
104 log.exception("bridge can't be initialised, can't start Libervia Backend") 105 log.exception("bridge can't be initialised, can't start Libervia Backend")
105 sys.exit(1) 106 sys.exit(1)
107
108 # If set, a temporary dir in cache will be used to share files between backend and
109 # frontends, useful when they are separated (e.g. when using containers). If
110 # unset, a temporary dir will be automatically created in os-relevant location.
111 use_local_shared_tmp: bool = C.bool(
112 self.memory.config_get("", "use_local_shared_tmp", C.BOOL_FALSE)
113 )
114 if use_local_shared_tmp:
115 self.local_shared_path = self.get_local_path(
116 None, C.CACHE_DIR, C.LOCAL_SHARED_DIR
117 )
118 shutil.rmtree(self.local_shared_path, ignore_errors=True)
119 self.local_shared_path.mkdir(0o700, parents=True, exist_ok=True)
120 else:
121 self.local_shared_path = None
122
106 123
107 defer.ensureDeferred(self._post_init()) 124 defer.ensureDeferred(self._post_init())
108 125
109 @property 126 @property
110 def version(self): 127 def version(self):
645 def startService(self): 662 def startService(self):
646 self._init() 663 self._init()
647 log.info("Salut à toi ô mon frère !") 664 log.info("Salut à toi ô mon frère !")
648 665
649 def stopService(self): 666 def stopService(self):
650 log.info("Salut aussi à Rantanplan") 667 if self.local_shared_path is not None:
668 log.debug("Cleaning shared temporary dir.")
669 shutil.rmtree(self.local_shared_path, ignore_errors=True)
670 log.info("Salut aussi à Rantanplan.")
671
651 return self.plugins_unload() 672 return self.plugins_unload()
652 673
653 def run(self): 674 def run(self):
654 log.debug(_("running app")) 675 log.debug(_("running app"))
655 reactor.run() 676 reactor.run()