mirror of
https://github.com/tuna/tunasync.git
synced 2025-04-21 04:42:46 +00:00
job hook update
This commit is contained in:
parent
8ae38ff406
commit
a615648333
@ -4,7 +4,7 @@ tunasync
|
||||
## TODO
|
||||
|
||||
- [ ] use context manager to handle job contexts
|
||||
- [ ] Hooks need "pre_try", "post_try"
|
||||
- [x] Hooks need "before_exec", "after_exec"
|
||||
- [x] implement `tunasynctl tail` and `tunasynctl log` or equivalent feature
|
||||
- [x] status file
|
||||
- [ ] mirror size
|
||||
|
@ -10,4 +10,10 @@ class JobHook(object):
|
||||
def after_job(self, *args, **kwargs):
|
||||
raise NotImplementedError("")
|
||||
|
||||
def before_exec(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def after_exec(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
# vim: ts=4 sw=4 sts=4 expandtab
|
||||
|
@ -43,15 +43,16 @@ def run_job(sema, child_q, manager_q, provider, **settings):
|
||||
manager_q.put(("UPDATE", (provider.name, status, ctx)))
|
||||
|
||||
try:
|
||||
# before_job hooks
|
||||
for hook in provider.hooks:
|
||||
hook.before_job(provider=provider, ctx=ctx)
|
||||
except Exception:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
status = "fail"
|
||||
else:
|
||||
status = "syncing"
|
||||
|
||||
for retry in range(max_retry):
|
||||
# before_exec hooks
|
||||
for hook in provider.hooks:
|
||||
hook.before_exec(provider=provider, ctx=ctx)
|
||||
|
||||
status = "syncing"
|
||||
manager_q.put(("UPDATE", (provider.name, status, ctx)))
|
||||
print("start syncing {}, retry: {}".format(provider.name, retry))
|
||||
|
||||
@ -63,19 +64,25 @@ def run_job(sema, child_q, manager_q, provider, **settings):
|
||||
else:
|
||||
status = "success"
|
||||
|
||||
# after_exec hooks
|
||||
for hook in provider.hooks:
|
||||
hook.after_exec(provider=provider, status=status, ctx=ctx)
|
||||
|
||||
if status == "success":
|
||||
break
|
||||
|
||||
try:
|
||||
# after_job hooks
|
||||
for hook in provider.hooks[::-1]:
|
||||
hook.after_job(provider=provider, status=status, ctx=ctx)
|
||||
|
||||
except Exception:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
status = "fail"
|
||||
|
||||
sema.release()
|
||||
aquired = False
|
||||
finally:
|
||||
sema.release()
|
||||
aquired = False
|
||||
|
||||
print("syncing {} finished, sleep {} minutes for the next turn".format(
|
||||
provider.name, provider.interval
|
||||
|
@ -11,7 +11,13 @@ class LogLimitHook(JobHook):
|
||||
def __init__(self, limit=10):
|
||||
self.limit = limit
|
||||
|
||||
def before_job(self, provider, ctx={}, *args, **kwargs):
|
||||
def before_job(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def after_job(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def before_exec(self, provider, ctx={}, *args, **kwargs):
|
||||
log_dir = provider.log_dir
|
||||
self.ensure_log_dir(log_dir)
|
||||
log_file = provider.log_file.format(
|
||||
@ -45,7 +51,7 @@ class LogLimitHook(JobHook):
|
||||
# create a soft link
|
||||
self.create_link(log_link, log_file)
|
||||
|
||||
def after_job(self, status=None, ctx={}, *args, **kwargs):
|
||||
def after_exec(self, status=None, ctx={}, *args, **kwargs):
|
||||
log_file = ctx.get('log_file', None)
|
||||
log_link = ctx.get('log_link', None)
|
||||
if log_file == "/dev/null":
|
||||
|
Loading…
x
Reference in New Issue
Block a user