Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
88 lines (64 loc) · 2.21 KB
/
Copy pathmain.py
File metadata and controls
88 lines (64 loc) · 2.21 KB
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
"""Warmup runtime entry point.
python main.py
Reads config.yaml, opens the SQLite DB, starts the APScheduler, and parks the
main thread until Ctrl+C.
"""
from __future__ import annotations
import logging
import logging.handlers
import signal
import sys
import time
from pathlib import Path
import yaml
from dotenv import load_dotenv
import database
import scheduler as scheduler_module
LOG_PATH = Path(__file__).resolve().parent / "warmup.log"
ENV_PATH = Path(__file__).resolve().parent / ".env"
def _setup_logging() -> None:
fmt = logging.Formatter("%(asctime)s %(levelname)s %(name)s | %(message)s")
root = logging.getLogger()
root.setLevel(logging.INFO)
file_handler = logging.handlers.RotatingFileHandler(
LOG_PATH, maxBytes=5_000_000, backupCount=5, encoding="utf-8"
)
file_handler.setFormatter(fmt)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(fmt)
root.handlers.clear()
root.addHandler(file_handler)
root.addHandler(stream_handler)
def _load_config() -> dict:
with open(Path(__file__).resolve().parent / "config.yaml", encoding="utf-8") as f:
return yaml.safe_load(f)
def _check_required_env() -> None:
import os
missing = [v for v in ("SMTP_USERNAME", "SMTP_PASSWORD") if not os.environ.get(v)]
if missing:
print(f"Missing required env vars: {', '.join(missing)}", file=sys.stderr)
print("See .env.example for the full list.", file=sys.stderr)
sys.exit(2)
def main() -> None:
load_dotenv(ENV_PATH)
_setup_logging()
_check_required_env()
config = _load_config()
database.init_db()
if not database.all_inboxes():
print("No inboxes in DB. Run `python setup.py` first.", file=sys.stderr)
sys.exit(3)
warmup = scheduler_module.Warmup(config)
warmup.start()
def _stop(signum, frame): # noqa: ANN001
logging.info("Shutting down (signal %s)...", signum)
warmup.shutdown()
sys.exit(0)
signal.signal(signal.SIGINT, _stop)
if hasattr(signal, "SIGTERM"):
signal.signal(signal.SIGTERM, _stop)
logging.info("warmup running. Ctrl+C to stop.")
while True:
time.sleep(60)
if __name__ == "__main__":
main()