Add ability to cancel snapshot creation

This commit is contained in:
Heliguy
2024-09-23 12:16:05 -04:00
parent 9add61101c
commit b0443840f8
4 changed files with 33 additions and 17 deletions

View File

@@ -12,10 +12,6 @@ class LoadingStatus(Gtk.ScrolledWindow):
progress_bar = gtc()
progress_label = gtc()
button = gtc()
def set_progress_label(self, *args):
text = self.progress_bar.get_fraction() * 100
self.progress_label.set_label(f"{text:.0f}%")
def __init__(self, title, description, show_progress=False, on_cancel=None, **kwargs):
super().__init__(**kwargs)
@@ -26,6 +22,4 @@ class LoadingStatus(Gtk.ScrolledWindow):
if on_cancel is None:
self.button.set_visible(False)
else:
self.button.connect("clicked", lambda *_: on_cancel)
# self.progress_bar.connect("notify::fraction", self.set_progress_label)
self.button.connect("clicked", lambda *_: on_cancel())

View File

@@ -105,7 +105,7 @@ class NewSnapshotDialog(Adw.Dialog):
self.workers.append(worker)
worker.compress()
GLib.timeout_add(10, self.get_total_fraction)
GLib.timeout_add(200, self.get_total_fraction)
self.close()
def on_invalidate(self, search_entry):

View File

@@ -171,7 +171,8 @@ class SnapshotPage(Adw.BreakpointBin):
overlay.add_toast(ErrorToast(_("Could not open folder"), str(e)).toast)
def on_cancel(self):
pass
for worker in self.new_snapshot_dialog.workers:
worker.do_cancel("manual_cancel")
def on_new(self, *args):
self.new_snapshot_dialog.present(HostInfo.main_window)
@@ -204,4 +205,5 @@ class SnapshotPage(Adw.BreakpointBin):
# Apply
self.loading_view.set_content(LoadingStatus(_("Loading Snapshots"), _("This should only take a moment")))
self.snapshotting_view.set_content(self.snapshotting_status)
self.snapshotting_status.button.set_label(_("Cancel"))
self.split_view.set_content(self.list_page)

View File

@@ -13,6 +13,7 @@ class TarWorker:
self.stop = False
self.fraction = 0.0
self.total = 0
self.process = None
def compress_thread(self, *args):
try:
@@ -21,7 +22,13 @@ class TarWorker:
self.total = int(subprocess.run(['du', '-s', self.existing_path], check=True, text=True, capture_output=True).stdout.split('\t')[0])
self.total /= 2.2 # estimate for space savings
subprocess.run(['tar', 'cafv', f'{self.new_path}/{self.file_name}.tar.zst', '-C', self.existing_path, '.'], check=True, capture_output=True)
self.process = subprocess.Popen(['tar', 'cafv', f'{self.new_path}/{self.file_name}.tar.zst', '-C', self.existing_path, '.'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = self.process.communicate()
if self.process.returncode != 0:
raise subprocess.CalledProcessError(self.process.returncode, self.process.args, output=stdout, stderr=stderr)
with open(f"{self.new_path}/{self.file_name}.json", 'w') as file:
data = {
@@ -33,12 +40,26 @@ class TarWorker:
self.stop = True # tell the check timeout to stop, because we know the file is done being made
except subprocess.CalledProcessError as cpe:
self.stop = True
print(cpe.stderr)
print("Called Error")
self.do_cancel(cpe.stderr.decode()) # stderr is in bytes, so decode it
except Exception as e:
self.stop = True
print(f"Error during compression: {e}")
print("Exception")
self.do_cancel(str(e))
def do_cancel(self, error_str=None):
self.process.terminate()
self.process.wait()
if error_str == "manual_cancel":
try:
subprocess.run(['gio', 'trash', f'{self.new_path}/{self.file_name}.tar.zst'],capture_output=True)
subprocess.run(['gio', 'trash', f'{self.new_path}/{self.file_name}.json'],capture_output=True)
except Exception:
pass
self.stop = True
print("Error in compression:", error_str)
def check_size(self):
try:
@@ -46,13 +67,12 @@ class TarWorker:
working_total = int(output)
self.fraction = working_total / self.total
return not self.stop
except subprocess.CalledProcessError as cpe:
return not self.stop # continue the timeout or stop the timeout
def compress(self):
self.compress = True
self.stop = False
Gio.Task.new(None, None, None).run_in_thread(self.compress_thread)
GLib.timeout_add(10, self.check_size)
GLib.timeout_add(200, self.check_size)