diff --git a/tunasync/btrfs_snapshot.py b/tunasync/btrfs_snapshot.py index a5288f8..615eaad 100644 --- a/tunasync/btrfs_snapshot.py +++ b/tunasync/btrfs_snapshot.py @@ -17,10 +17,10 @@ class BtrfsHook(JobHook): self.working_dir = working_dir self.gc_dir = gc_dir - def before_job(self): + def before_job(self, *args, **kwargs): self._create_working_snapshot() - def after_job(self): + def after_job(self, *args, **kwargs): self._commit_changes() def _ensure_subvolume(self): diff --git a/tunasync/hook.py b/tunasync/hook.py index adf6092..f0d8602 100644 --- a/tunasync/hook.py +++ b/tunasync/hook.py @@ -4,10 +4,10 @@ class JobHook(object): - def before_job(self): + def before_job(self, *args, **kwargs): raise NotImplementedError("") - def after_job(self): + def after_job(self, *args, **kwargs): raise NotImplementedError("") # vim: ts=4 sw=4 sts=4 expandtab diff --git a/tunasync/jobs.py b/tunasync/jobs.py index fd16940..3dadb95 100644 --- a/tunasync/jobs.py +++ b/tunasync/jobs.py @@ -1,5 +1,6 @@ #!/usr/bin/env python2 # -*- coding:utf-8 -*- +import sh import sys import signal @@ -28,10 +29,20 @@ def run_job(sema, child_q, manager_q, provider): hook.before_job() provider.run() - provider.wait() + + try: + provider.wait() + except sh.ErrorReturnCode: + status = "fail" + else: + status = "success" for hook in provider.hooks[::-1]: - hook.after_job() + try: + hook.after_job(status=status) + except Exception: + import traceback + traceback.print_exc() sema.release() aquired = False diff --git a/tunasync/tunasync.py b/tunasync/tunasync.py index f08ccac..e304843 100644 --- a/tunasync/tunasync.py +++ b/tunasync/tunasync.py @@ -9,6 +9,7 @@ from multiprocessing import Process, Semaphore, Queue from . import jobs from .mirror_provider import RsyncProvider, ShellProvider from .btrfs_snapshot import BtrfsHook +from .hook import JobHook class MirrorConfig(object): @@ -145,6 +146,7 @@ class TUNASync(object): self.processes = {} self.semaphore = Semaphore(self._settings.getint("global", "concurrent")) self.channel = Queue() + self._hooks = [] self.mirror_root = self._settings.get("global", "mirror_root") self.use_btrfs = self._settings.getboolean("global", "use_btrfs") @@ -155,8 +157,12 @@ class TUNASync(object): self.btrfs_gc_dir_tmpl = self._settings.get( "btrfs", "gc_dir") + def add_hook(self, h): + assert isinstance(h, JobHook) + self._hooks.append(h) + def hooks(self): - return [] + return self._hooks @property def mirrors(self):