4 Commits
Author SHA1 Message Date
feffe acaa092a50 bump version 2024-05-28 06:37:42 +02:00
feffe 82c3d9d8a6 replace utcnow() with now() 2024-01-03 13:24:01 +01:00
feffe 74201e59d6 allow wrapper to work as a daemon-restarter... 2023-08-03 16:35:58 +02:00
feffe f56efb0085 do not remove lock if we were not the ones to aquire it... 2021-03-08 16:39:25 +01:00
2 changed files with 20 additions and 8 deletions
+19 -7
View File
@@ -47,6 +47,10 @@ def parse_args():
'-L', '--no-lock',
help='Allow multiple simultanious executions of this cron job',
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(
@@ -104,7 +108,7 @@ def exec_command(args, outfile, errfile, resfile):
proc.terminate()
proc.communicate(timeout=10)
now=datetime.datetime.utcnow()
now=datetime.datetime.now()
nowstr=now.strftime('%Y-%m-%d_%H%M.%S')
resfile.write('{}\n{}'.format(nowstr, proc.returncode))
return proc.returncode
@@ -167,7 +171,7 @@ def print_runs(runs, clean=True):
def main():
time_format = '%Y-%m-%d_%H%M'
args = parse_args()
now = datetime.datetime.utcnow()
now = datetime.datetime.now()
nowstr = now.strftime(time_format)
libdir = os.path.join(args.cachedir[0], args.name, nowstr)
lckdir = os.path.join(args.lockdir[0], args.name)
@@ -181,13 +185,20 @@ def main():
resfile = os.path.join(libdir, 'result')
success = True
has_lock = 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:
res = exec_command(args, o, e, r)
else:
e.write("CRONWRAPPER: Unable to aquire lock, previous instance still running?\n")
r.write("\nFalse\n")
res = False
has_lock = aquire_lock(lckfile)
if has_lock:
res = exec_command(args, o, e, r)
elif args.restart:
res = 0
else:
e.write("CRONWRAPPER: Unable to aquire lock, previous instance still running?\n")
r.write("\nFalse\n")
res = False
if res in args.success_exit_codes:
# Possible success, check error output
@@ -249,7 +260,8 @@ def main():
print("Cronjob failed\n")
print_runs([libdir], clean=False)
release_lock(lckfile)
if has_lock:
release_lock(lckfile)
if __name__ == '__main__':
sys.exit(main())
+1 -1
View File
@@ -5,7 +5,7 @@ with open('README.md', 'r') as fh:
setuptools.setup(
name='cronwrapper',
version='0.1.4',
version='0.1.5',
author='Fredrik Eriksson',
author_email='feffe@fulh.ax',
description='A small wrapper to handle cronjob failures',