Compare commits
No commits in common. "master" and "v0.1.0" have entirely different histories.
@ -47,10 +47,6 @@ 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(
|
||||
@ -83,7 +79,7 @@ def parse_args():
|
||||
|
||||
args = parser.parse_args()
|
||||
if not args.name:
|
||||
args.name = os.path.basename(args.command[0])
|
||||
args.name = [os.path.basename(args.command[0])]
|
||||
|
||||
return args
|
||||
|
||||
@ -108,7 +104,7 @@ def exec_command(args, outfile, errfile, resfile):
|
||||
proc.terminate()
|
||||
proc.communicate(timeout=10)
|
||||
|
||||
now=datetime.datetime.now()
|
||||
now=datetime.datetime.utcnow()
|
||||
nowstr=now.strftime('%Y-%m-%d_%H%M.%S')
|
||||
resfile.write('{}\n{}'.format(nowstr, proc.returncode))
|
||||
return proc.returncode
|
||||
@ -163,84 +159,60 @@ def print_runs(runs, clean=True):
|
||||
print("\n\n")
|
||||
if clean:
|
||||
for run in runs:
|
||||
try:
|
||||
shutil.rmtree(run)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
shutil.rmtree(run)
|
||||
|
||||
def main():
|
||||
time_format = '%Y-%m-%d_%H%M'
|
||||
args = parse_args()
|
||||
now = datetime.datetime.now()
|
||||
now = datetime.datetime.utcnow()
|
||||
nowstr = now.strftime(time_format)
|
||||
libdir = os.path.join(args.cachedir[0], args.name, nowstr)
|
||||
lckdir = os.path.join(args.lockdir[0], args.name)
|
||||
libdir = os.path.join(args.cachedir[0], args.name[0], nowstr)
|
||||
lckdir = os.path.join(args.lockdir[0], args.name[0])
|
||||
|
||||
|
||||
os.makedirs(lckdir, exist_ok=True)
|
||||
os.makedirs(libdir)
|
||||
lckfile = os.path.join(lckdir, args.name)
|
||||
lckfile = os.path.join(lckdir, args.name[0])
|
||||
outfile = os.path.join(libdir, 'stdout')
|
||||
errfile = os.path.join(libdir, 'stderr')
|
||||
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:
|
||||
with open(outfile, 'w') as o, open(errfile, 'w') as e, open(resfile, 'w') as r:
|
||||
if args.no_lock or aquire_lock(lckfile):
|
||||
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:
|
||||
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
|
||||
re_checks = [re.compile(r) for r in args.ignore_error]
|
||||
e.seek(0)
|
||||
for line in e:
|
||||
success = False
|
||||
for r in re_checks:
|
||||
if re.match(r, line):
|
||||
success = True
|
||||
break
|
||||
if not success:
|
||||
break
|
||||
else:
|
||||
success = False
|
||||
|
||||
e.write("CRONWRAPPER: Unable to aquire lock, previous instance still running?\n")
|
||||
r.write("\nFalse\n")
|
||||
res = 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)
|
||||
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 success:
|
||||
# Yes! Success! report any errors until now
|
||||
if previous_runs:
|
||||
print("Success after {} failed runs\n".format(len(previous_runs)))
|
||||
print_runs(previous_runs.keys())
|
||||
try:
|
||||
if res in args.success_exit_codes:
|
||||
# Possible success, check error output
|
||||
re_checks = [re.compile(r) for r in args.ignore_error]
|
||||
ok = True
|
||||
with open(errfile, 'r') as f:
|
||||
for line in f:
|
||||
ok = False
|
||||
for r in re_checks:
|
||||
if re.match(r, line):
|
||||
ok = True
|
||||
break
|
||||
if not ok:
|
||||
break
|
||||
if ok:
|
||||
# Yes! Success! report any errors until now
|
||||
if previous_runs:
|
||||
print("Success after {} failed runs\n".format(len(previous_runs)))
|
||||
print_runs(previous_runs.keys())
|
||||
shutil.rmtree(libdir)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
return 0
|
||||
|
||||
return 0
|
||||
|
||||
# Failure
|
||||
if previous_runs:
|
||||
@ -260,8 +232,7 @@ def main():
|
||||
print("Cronjob failed\n")
|
||||
print_runs([libdir], clean=False)
|
||||
|
||||
if has_lock:
|
||||
release_lock(lckfile)
|
||||
release_lock(lckfile)
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
Loading…
Reference in New Issue
Block a user