Wait for arnied in addition to generate to preserve backward compatibility
authorPlamen Dimitrov <plamen.dimitrov@intra2net.com>
Thu, 21 Apr 2022 07:48:40 +0000 (10:48 +0300)
committerPlamen Dimitrov <plamen.dimitrov@intra2net.com>
Tue, 17 May 2022 02:44:36 +0000 (05:44 +0300)
We simplify this a bit though by waiting for the arnied availability
just once during initialization of the cnf store and not on each
get/set/unset_cnf call (now query, commit, and delete calls).

As this is done for backward compability which is guaranteed for
the binary cnfvar driver only, we leave a TODO where we will have
to consider what is the best future for the varlink driver: either
busy-loop on socket availability to once again wait with a timeout
or drop this assumption entirely for better control and easier use.

src/cnfvar/store.py

index c22e217..915e087 100644 (file)
@@ -52,6 +52,9 @@ class CnfStore:
         :type: :py:class:`arnied_api.Arnied`
         """
         self._driver = backend_driver
+        # TODO: implement `self._wait_for_arnied()` which should busy-loop with
+        # the arnied varlink socket and handle "Disconnected" errors, then perhaps
+        # drop the old binary cnf store method from the arnied wrapper
         log.debug(f"Initialized cnf store with driver `{backend_driver.__name__}`")
 
     def query(self, name=None, instance=None):
@@ -258,6 +261,9 @@ class BinaryCnfStore(CnfStore):
         :type: :py:class:`CnfBinary`
         """
         super().__init__(backend_driver=backend_driver)
+        # We assume that any external events happening to arnied require reinitializing
+        # the cnf store which is bound to the lifespan of a single arnied process.
+        self._call_arnied(arnied_wrapper.verify_running, timeout=self.ARNIED_TIMEOUT)
         log.debug(f"Initialized binary cnf store with driver `{backend_driver.__name__}`")
 
     def query(self, name=None, instance=None):
@@ -309,7 +315,6 @@ class BinaryCnfStore(CnfStore):
         cnf.renumber()
         log.debug("Committing variables via binaries:\n%s", cnf)
 
-        self._call_arnied(arnied_wrapper.verify_running, timeout=self.ARNIED_TIMEOUT)
         try:
             self._driver.set_cnf(input_str=str(cnf), fix_problems=fix_problems)
         except subprocess.CalledProcessError as ex:
@@ -338,7 +343,6 @@ class BinaryCnfStore(CnfStore):
         cnf.renumber()
         log.debug("Deleting variables via binaries:\n%s", cnf)
 
-        self._call_arnied(arnied_wrapper.verify_running, timeout=self.ARNIED_TIMEOUT)
         try:
             self._driver.set_cnf(input_str=str(cnf), delete=True, fix_problems=fix_problems)
         except subprocess.CalledProcessError as ex: