diff --git a/archivebox/cli/archivebox_server.py b/archivebox/cli/archivebox_server.py index 05ac96e4..f25cc0c4 100644 --- a/archivebox/cli/archivebox_server.py +++ b/archivebox/cli/archivebox_server.py @@ -58,6 +58,11 @@ def main(args: Optional[List[str]]=None, stdin: Optional[IO]=None, pwd: Optional action='store_true', help='Run archivebox manage createsuperuser before starting the server', ) + parser.add_argument( + '--daemonize', + action='store_true', + help='Run the server in the background as a daemon', + ) command = parser.parse_args(args or ()) reject_stdin(__command__, stdin) @@ -68,6 +73,7 @@ def main(args: Optional[List[str]]=None, stdin: Optional[IO]=None, pwd: Optional init=command.init, quick_init=command.quick_init, createsuperuser=command.createsuperuser, + daemonize=command.daemonize, out_dir=Path(pwd) if pwd else DATA_DIR, ) diff --git a/archivebox/queues/supervisor_util.py b/archivebox/queues/supervisor_util.py index 71094235..a109828e 100644 --- a/archivebox/queues/supervisor_util.py +++ b/archivebox/queues/supervisor_util.py @@ -266,8 +266,8 @@ def stop_worker(supervisor, daemon_name): -def start_server_workers(host='0.0.0.0', port='8000'): - supervisor = get_or_create_supervisord_process(daemonize=False) +def start_server_workers(host='0.0.0.0', port='8000', daemonize=False): + supervisor = get_or_create_supervisord_process(daemonize=daemonize) bg_workers = [ { @@ -303,18 +303,19 @@ def start_server_workers(host='0.0.0.0', port='8000'): start_worker(supervisor, worker) print() - try: - watch_worker(supervisor, "worker_daphne") - except KeyboardInterrupt: - print("\n[🛑] Got Ctrl+C, stopping gracefully...") - except SystemExit: - pass - except BaseException as e: - print(f"\n[🛑] Got {e.__class__.__name__} exception, stopping web server gracefully...") - raise - finally: - stop_worker(supervisor, "worker_daphne") - time.sleep(0.5) + if not daemonize: + try: + watch_worker(supervisor, "worker_daphne") + except KeyboardInterrupt: + print("\n[🛑] Got Ctrl+C, stopping gracefully...") + except SystemExit: + pass + except BaseException as e: + print(f"\n[🛑] Got {e.__class__.__name__} exception, stopping web server gracefully...") + raise + finally: + stop_worker(supervisor, "worker_daphne") + time.sleep(0.5) def start_cli_workers(watch=False):