-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
main.py
124 lines (99 loc) · 3.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import sys
from threading import Thread
from time import sleep
import requests
import webview
from kanmail.license import validate_or_remove_license
from kanmail.log import logger
from kanmail.server.app import boot, server
from kanmail.server.mail.folder_cache import (
remove_stale_folders,
remove_stale_headers,
vacuum_folder_cache,
)
from kanmail.settings import get_window_settings
from kanmail.settings.constants import DEBUG, GUI_LIB, SERVER_HOST
from kanmail.version import get_version
from kanmail.window import create_window, destroy_main_window, init_window_hacks
def run_cache_cleanup_later():
sleep(120) # TODO: make this more intelligent?
remove_stale_folders()
remove_stale_headers()
vacuum_folder_cache()
def run_server():
logger.debug(f"Starting server on {SERVER_HOST}:{server.get_port()}")
try:
server.serve()
except Exception as e:
logger.exception(f"Exception in server thread!: {e}")
def monitor_threads(*threads):
while True:
for thread in threads:
if not thread.is_alive():
logger.critical(f"Thread: {thread} died, exiting!")
destroy_main_window()
server.stop()
sys.exit(2)
else:
sleep(0.5)
def run_thread(target):
def wrapper(thread_name):
try:
target()
except Exception as e:
logger.exception(f"Unexpected exception in thread {thread_name}!: {e}")
thread = Thread(
target=wrapper,
args=(target.__name__,),
)
thread.daemon = True
thread.start()
def main():
logger.info(f"\n#\n# Booting Kanmail {get_version()}\n#")
init_window_hacks()
boot()
server_thread = Thread(name="Server", target=run_server)
server_thread.daemon = True
server_thread.start()
run_thread(validate_or_remove_license)
run_thread(run_cache_cleanup_later)
# Ensure the webserver is up & running by polling it
waits = 0
while waits < 10:
try:
response = requests.get(f"http://{SERVER_HOST}:{server.get_port()}/ping")
response.raise_for_status()
except requests.RequestException as e:
logger.warning(f"Waiting for main window: {e}")
sleep(0.1 * waits)
waits += 1
else:
break
else:
logger.critical("Webserver did not start properly!")
sys.exit(2)
create_window(
unique_key="main",
**get_window_settings(),
)
# Let's hope this thread doesn't fail!
monitor_thread = Thread(
name="Thread monitor",
target=monitor_threads,
args=(server_thread,),
)
monitor_thread.daemon = True
monitor_thread.start()
if DEBUG:
sleep(1) # give webpack a second to start listening
# Start the GUI - this will block until the main window is destroyed
webview.start(gui=GUI_LIB, debug=DEBUG)
logger.debug("Main window closed, shutting down...")
server.stop()
sys.exit()
if __name__ == "__main__":
try:
main()
except Exception:
server.stop()
raise