borgend/backup.py

changeset 87
a214d475aa28
parent 86
2fe66644c50d
child 88
dfd52898f175
--- a/borgend/backup.py	Sun Jan 28 17:54:14 2018 +0000
+++ b/borgend/backup.py	Sun Jan 28 19:27:34 2018 +0000
@@ -296,7 +296,7 @@
                 self.logger.debug('Unrecognised log entry %s' % str(status))
 
             if callback:
-                callback(status, errors=errormsg)
+                callback(status, errorlog=errormsg)
 
         self.logger.debug('Waiting for borg subprocess to terminate in log thread')
 
@@ -354,9 +354,13 @@
     def __do_launch(self, op, archive_or_repository,
                     common_params, op_params, paths=[]):
 
+        self.logger.debug('Creating BorgInstance')
+
         inst=BorgInstance(op.operation, archive_or_repository,
                           common_params, op_params, paths)
 
+        self.logger.debug('Launching BorgInstance via repository')
+
         # Only the Repository object has access to the passphrase
         self.repository.launch_borg_instance(inst)
 
@@ -456,37 +460,45 @@
 
     def __main_thread(self):
         with self._cond:
-            try:
-                while not self._terminate:
+            while not self._terminate:
+                try:
                     assert(not self.current_operation)
                     self.__main_thread_wait_schedule()
                     if not self._terminate:
                         self.__main_thread_queue_and_launch()
-            except Exception as err:
-                self.logger.exception("Error with backup '%s'" % self.backup_name)
-                self.errors=Errors.ERRORS
+                except Exception as err:
+                    self.logger.exception("Error with backup '%s'" % self.backup_name)
+                    self.errors=Errors.ERRORS
+                    self.__cleanup()
 
-            self.state=State.INACTIVE
-            self.scheduled_operation=None
-
-            # Clean up to terminate: kill borg instance and communication threads
-            if self.borg_instance:
-                self.logger.debug("Terminating a borg instance")
-                self.borg_instance.terminate()
+            self.__cleanup()
 
-            # Store threads to use outside lock
-            thread_log=self.thread_log
-            thread_res=self.thread_res
-            self.thread_log=None
-            self.thread_res=None
+    def __cleanup(self):
+        self.state=State.INACTIVE
+        self.scheduled_operation=None
+        self.current_operation=None
+        thread_log=self.thread_log
+        thread_res=self.thread_res
+        borg_instance=self.borg_instance
+        self.thread_log=None
+        self.thread_res=None
+        self.borg_instance=None
 
-        self.logger.debug("Waiting for log and result threads to terminate")
+        self._cond.release()
+        try:
+            if borg_instance:
+                self.logger.debug("Terminating a borg instance")
+                borg_instance.terminate()
 
-        if thread_log:
-            thread_log.join()
+            if thread_log:
+                self.logger.debug("Waiting for log thread to terminate")
+                thread_log.join()
 
-        if thread_res:
-            thread_res.join()
+            if thread_res:
+                self.logger.debug("Waiting for result thread to terminate")
+                thread_res.join()
+        finally:
+            self._cond.acquire()
 
     # Main thread/2. Schedule next operation if there is no manually
     # requested one

mercurial