diff --git a/f_run_once.py b/f_run_once.py new file mode 100644 index 0000000..32b6dba --- /dev/null +++ b/f_run_once.py @@ -0,0 +1,16 @@ +import sys +import fcntl +import os + +_run_once_file_handle = 0 + + +def run_once(): + global _run_once_file_handle + + host_script_file_path = os.path.realpath(sys.argv[0]) + _run_once_file_handle = open(host_script_file_path, 'r') + try: + fcntl.flock(_run_once_file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB) + except: + sys.exit('Another instance already running') diff --git a/f_sigint.py b/f_sigint.py new file mode 100644 index 0000000..de1f475 --- /dev/null +++ b/f_sigint.py @@ -0,0 +1,6 @@ +import signal +import sys + +def sigint_handler(signal, frame): + sys.exit(0) +signal.signal(signal.SIGINT, sigint_handler) diff --git a/main.py b/main.py index 373dbff..8ab8045 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,6 @@ +import argparse, time import feedparser +import f_run_once, f_sigint from settings import * from mastodon import Mastodon from os.path import exists @@ -83,6 +85,28 @@ def main(): for lines in send_data: f.write('%s\n' %lines) f.close() - +def startup(): + f_run_once.run_once() + print("ok") + parser = argparse.ArgumentParser(description = "Mastodon robot posting RSS-Feeds") + parser.add_argument('--daemon', dest='daemon', action='store_true', help='run in daemon mode and repeat after a delay') + parser.add_argument('--delay', dest='daemon_delay', action='store', type=int, help='number of seconds to wait for next run default=3600') + args = parser.parse_args() + run_as_daemon = False + if args.daemon or args.daemon_delay: + run_as_daemon = True + if run_as_daemon: + daemon_delay = 3600 + if args.daemon_delay: + daemon_delay = args.daemon_delay + if daemon_delay == 0 or not isinstance(daemon_delay, int): + daemon_delay = 3600 + + if run_as_daemon: + while True: + main() + time.sleep(daemon_delay) + else: + main() if __name__ == "__main__": - main() + startup()