Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
e5bf87c7ab
|
|||
a69cde3160
|
|||
572f43010e
|
|||
3846e8f303
|
|||
07ccb09910
|
|||
235f5b1e07
|
|||
b9c04b8b00
|
@ -79,7 +79,7 @@ def parse_args():
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if not args.name:
|
if not args.name:
|
||||||
args.name = [os.path.basename(args.command[0])]
|
args.name = os.path.basename(args.command[0])
|
||||||
|
|
||||||
return args
|
return args
|
||||||
|
|
||||||
@ -159,25 +159,29 @@ def print_runs(runs, clean=True):
|
|||||||
print("\n\n")
|
print("\n\n")
|
||||||
if clean:
|
if clean:
|
||||||
for run in runs:
|
for run in runs:
|
||||||
|
try:
|
||||||
shutil.rmtree(run)
|
shutil.rmtree(run)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
time_format = '%Y-%m-%d_%H%M'
|
time_format = '%Y-%m-%d_%H%M'
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
now = datetime.datetime.utcnow()
|
now = datetime.datetime.utcnow()
|
||||||
nowstr = now.strftime(time_format)
|
nowstr = now.strftime(time_format)
|
||||||
libdir = os.path.join(args.cachedir[0], args.name[0], nowstr)
|
libdir = os.path.join(args.cachedir[0], args.name, nowstr)
|
||||||
lckdir = os.path.join(args.lockdir[0], args.name[0])
|
lckdir = os.path.join(args.lockdir[0], args.name)
|
||||||
|
|
||||||
|
|
||||||
os.makedirs(lckdir, exist_ok=True)
|
os.makedirs(lckdir, exist_ok=True)
|
||||||
os.makedirs(libdir)
|
os.makedirs(libdir)
|
||||||
lckfile = os.path.join(lckdir, args.name[0])
|
lckfile = os.path.join(lckdir, args.name)
|
||||||
outfile = os.path.join(libdir, 'stdout')
|
outfile = os.path.join(libdir, 'stdout')
|
||||||
errfile = os.path.join(libdir, 'stderr')
|
errfile = os.path.join(libdir, 'stderr')
|
||||||
resfile = os.path.join(libdir, 'result')
|
resfile = os.path.join(libdir, 'result')
|
||||||
|
|
||||||
with open(outfile, 'w') as o, open(errfile, 'w') as e, open(resfile, 'w') as r:
|
success = True
|
||||||
|
with open(outfile, 'w') as o, open(errfile, 'w+') as e, open(resfile, 'w') as r:
|
||||||
if args.no_lock or aquire_lock(lckfile):
|
if args.no_lock or aquire_lock(lckfile):
|
||||||
res = exec_command(args, o, e, r)
|
res = exec_command(args, o, e, r)
|
||||||
else:
|
else:
|
||||||
@ -185,35 +189,48 @@ def main():
|
|||||||
r.write("\nFalse\n")
|
r.write("\nFalse\n")
|
||||||
res = False
|
res = False
|
||||||
|
|
||||||
previous_runs = {}
|
|
||||||
for root, dirs, files in os.walk(os.path.join(args.cachedir[0], args.name[0])):
|
|
||||||
previous_runs = {
|
|
||||||
os.path.join(root, d): datetime.datetime.strptime(d, time_format)
|
|
||||||
for d in dirs
|
|
||||||
if datetime.datetime.strptime(d, time_format) < datetime.datetime.strptime(nowstr, time_format)}
|
|
||||||
break
|
|
||||||
|
|
||||||
if res in args.success_exit_codes:
|
if res in args.success_exit_codes:
|
||||||
# Possible success, check error output
|
# Possible success, check error output
|
||||||
re_checks = [re.compile(r) for r in args.ignore_error]
|
re_checks = [re.compile(r) for r in args.ignore_error]
|
||||||
ok = True
|
e.seek(0)
|
||||||
with open(errfile, 'r') as f:
|
for line in e:
|
||||||
for line in f:
|
success = False
|
||||||
ok = False
|
|
||||||
for r in re_checks:
|
for r in re_checks:
|
||||||
if re.match(r, line):
|
if re.match(r, line):
|
||||||
ok = True
|
success = True
|
||||||
break
|
break
|
||||||
if not ok:
|
if not success:
|
||||||
break
|
break
|
||||||
if ok:
|
else:
|
||||||
|
success = False
|
||||||
|
|
||||||
|
|
||||||
|
previous_runs = {}
|
||||||
|
for root, dirs, files in os.walk(os.path.join(args.cachedir[0], args.name)):
|
||||||
|
for d in dirs:
|
||||||
|
if datetime.datetime.strptime(d, time_format) < datetime.datetime.strptime(nowstr, time_format):
|
||||||
|
with open(os.path.join(root, d, 'result'), 'r') as f:
|
||||||
|
try:
|
||||||
|
retcode = f.read().splitlines()[-1]
|
||||||
|
except IndexError:
|
||||||
|
# Previous run is probably not completed yet, ignore
|
||||||
|
# this entry
|
||||||
|
continue
|
||||||
|
previous_runs[os.path.join(root, d)] = datetime.datetime.strptime(d, time_format)
|
||||||
|
break
|
||||||
|
|
||||||
|
if success:
|
||||||
# Yes! Success! report any errors until now
|
# Yes! Success! report any errors until now
|
||||||
if previous_runs:
|
if previous_runs:
|
||||||
print("Success after {} failed runs\n".format(len(previous_runs)))
|
print("Success after {} failed runs\n".format(len(previous_runs)))
|
||||||
print_runs(previous_runs.keys())
|
print_runs(previous_runs.keys())
|
||||||
|
try:
|
||||||
shutil.rmtree(libdir)
|
shutil.rmtree(libdir)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
# Failure
|
# Failure
|
||||||
if previous_runs:
|
if previous_runs:
|
||||||
# Not the first failure...
|
# Not the first failure...
|
||||||
|
2
setup.py
2
setup.py
@ -5,7 +5,7 @@ with open('README.md', 'r') as fh:
|
|||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name='cronwrapper',
|
name='cronwrapper',
|
||||||
version='0.1.0',
|
version='0.1.4',
|
||||||
author='Fredrik Eriksson',
|
author='Fredrik Eriksson',
|
||||||
author_email='feffe@fulh.ax',
|
author_email='feffe@fulh.ax',
|
||||||
description='A small wrapper to handle cronjob failures',
|
description='A small wrapper to handle cronjob failures',
|
||||||
|
Reference in New Issue
Block a user