Compare commits

..

6 Commits

2 changed files with 30 additions and 12 deletions

View File

@ -47,6 +47,10 @@ def parse_args():
'-L', '--no-lock', '-L', '--no-lock',
help='Allow multiple simultanious executions of this cron job', help='Allow multiple simultanious executions of this cron job',
action='store_true') action='store_true')
parser.add_argument(
'-r', '--restart',
help='Restart the process if not running; exit with success if previous instance is running',
action='store_true')
parser.add_argument( parser.add_argument(
@ -104,7 +108,7 @@ def exec_command(args, outfile, errfile, resfile):
proc.terminate() proc.terminate()
proc.communicate(timeout=10) proc.communicate(timeout=10)
now=datetime.datetime.utcnow() now=datetime.datetime.now()
nowstr=now.strftime('%Y-%m-%d_%H%M.%S') nowstr=now.strftime('%Y-%m-%d_%H%M.%S')
resfile.write('{}\n{}'.format(nowstr, proc.returncode)) resfile.write('{}\n{}'.format(nowstr, proc.returncode))
return proc.returncode return proc.returncode
@ -167,7 +171,7 @@ def print_runs(runs, clean=True):
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.now()
nowstr = now.strftime(time_format) nowstr = now.strftime(time_format)
libdir = os.path.join(args.cachedir[0], args.name, nowstr) libdir = os.path.join(args.cachedir[0], args.name, nowstr)
lckdir = os.path.join(args.lockdir[0], args.name) lckdir = os.path.join(args.lockdir[0], args.name)
@ -181,9 +185,16 @@ def main():
resfile = os.path.join(libdir, 'result') resfile = os.path.join(libdir, 'result')
success = True success = True
has_lock = True
with open(outfile, 'w') as o, open(errfile, 'w+') as e, open(resfile, 'w') as r: 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:
res = exec_command(args, o, e, r) res = exec_command(args, o, e, r)
else:
has_lock = aquire_lock(lckfile)
if has_lock:
res = exec_command(args, o, e, r)
elif args.restart:
res = 0
else: else:
e.write("CRONWRAPPER: Unable to aquire lock, previous instance still running?\n") e.write("CRONWRAPPER: Unable to aquire lock, previous instance still running?\n")
r.write("\nFalse\n") r.write("\nFalse\n")
@ -207,10 +218,16 @@ def main():
previous_runs = {} previous_runs = {}
for root, dirs, files in os.walk(os.path.join(args.cachedir[0], args.name)): for root, dirs, files in os.walk(os.path.join(args.cachedir[0], args.name)):
previous_runs = { for d in dirs:
os.path.join(root, d): datetime.datetime.strptime(d, time_format) if datetime.datetime.strptime(d, time_format) < datetime.datetime.strptime(nowstr, time_format):
for d in dirs with open(os.path.join(root, d, 'result'), 'r') as f:
if datetime.datetime.strptime(d, time_format) < datetime.datetime.strptime(nowstr, time_format)} 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 break
if success: if success:
@ -243,6 +260,7 @@ def main():
print("Cronjob failed\n") print("Cronjob failed\n")
print_runs([libdir], clean=False) print_runs([libdir], clean=False)
if has_lock:
release_lock(lckfile) release_lock(lckfile)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -5,7 +5,7 @@ with open('README.md', 'r') as fh:
setuptools.setup( setuptools.setup(
name='cronwrapper', name='cronwrapper',
version='0.1.3', version='0.1.5',
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',