From c5359539155e193589954cfd08758f672809ccc6 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sun, 1 Jan 2023 20:48:13 -0300 Subject: [PATCH] Update index automatically in non GUI mode too - Poll scheduler every minute using threading.Timer - Use 60 seconds polling interval to avoid fork bombing - Schedule next via the same poll scheduler - Allow clean program interrupt by running scheduler in daemon mode --- src/main.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main.py b/src/main.py index a44ef3ca..20ef8121 100644 --- a/src/main.py +++ b/src/main.py @@ -3,6 +3,7 @@ import os import signal import sys import logging +import threading import warnings from platform import system @@ -73,6 +74,8 @@ def run(): logger.info("Starting Khoj...") if args.no_gui: + # Setup task scheduler + poll_task_scheduler() # Start Server configure_server(args, required=True) start_server(app, host=args.host, port=args.port, socket=args.socket) @@ -143,6 +146,13 @@ def start_server(app, host=None, port=None, socket=None): uvicorn.run(app, host=host, port=port) +def poll_task_scheduler(): + timer_thread = threading.Timer(60.0, poll_task_scheduler) + timer_thread.daemon = True + timer_thread.start() + schedule.run_pending() + + class ServerThread(QThread): def __init__(self, app, host=None, port=None, socket=None): super(ServerThread, self).__init__()