Coverage for src/app.py: 0%

54 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-04-23 15:35 +0000

1import logging 

2import signal 

3import sys 

4from os import environ 

5from tempfile import TemporaryDirectory 

6 

7# these two lines need to be at the top of the file before we span child processes 

8# this temp dir will be destroyed when prometheus_multiproc_dir is destroyed, aka at the end of the program 

9prometheus_multiproc_dir = TemporaryDirectory() 

10environ["PROMETHEUS_MULTIPROC_DIR"] = prometheus_multiproc_dir.name 

11# ruff: noqa: E402 

12 

13import sentry_sdk 

14from sentry_sdk.integrations import argv, atexit, dedupe, modules, stdlib, threading 

15from sentry_sdk.integrations import logging as sentry_logging 

16from sqlalchemy.sql import text 

17 

18from couchers.config import check_config, config 

19from couchers.db import apply_migrations, session_scope 

20from couchers.jobs.worker import start_jobs_scheduler, start_jobs_worker 

21from couchers.metrics import create_prometheus_server 

22from couchers.server import create_main_server, create_media_server 

23from couchers.tracing import setup_tracing 

24from dummy_data import add_dummy_data 

25 

26check_config() 

27 

28logging.basicConfig(format="[%(process)5d:%(thread)20d] %(asctime)s: %(name)s: %(message)s", level=logging.INFO) 

29logger = logging.getLogger(__name__) 

30 

31if config["SENTRY_ENABLED"]: 

32 # Sends exception tracebacks to Sentry, a cloud service for collecting exceptions 

33 sentry_sdk.init( 

34 config["SENTRY_URL"], 

35 traces_sample_rate=0.0, 

36 environment=config["COOKIE_DOMAIN"], 

37 release=config["VERSION"], 

38 default_integrations=False, 

39 integrations=[ 

40 # we need to manually list out the integrations, there is no other way of disabling the global excepthook integration 

41 # we want to disable that because it seems to be picking up already handled gRPC errors (e.g. grpc.StatusCode.NOT_FOUND) 

42 argv.ArgvIntegration(), 

43 atexit.AtexitIntegration(), 

44 dedupe.DedupeIntegration(), 

45 sentry_logging.LoggingIntegration(), 

46 modules.ModulesIntegration(), 

47 stdlib.StdlibIntegration(), 

48 threading.ThreadingIntegration(), 

49 ], 

50 ) 

51 

52# used to export metrics 

53create_prometheus_server(8000) 

54 

55 

56def log_unhandled_exception(exc_type, exc_value, exc_traceback): 

57 """Make sure that any unhandled exceptions will write to the logs""" 

58 if issubclass(exc_type, KeyboardInterrupt): 

59 # call the default excepthook saved at __excepthook__ 

60 sys.__excepthook__(exc_type, exc_value, exc_traceback) 

61 return 

62 logger.critical("Unhandled exception", exc_info=(exc_type, exc_value, exc_traceback)) 

63 

64 

65sys.excepthook = log_unhandled_exception 

66 

67logger.info("Checking DB connection") 

68 

69with session_scope() as session: 

70 res = session.execute(text("SELECT 42;")) 

71 if list(res) != [(42,)]: 

72 raise Exception("Failed to connect to DB") 

73 

74logger.info("Running DB migrations") 

75 

76apply_migrations() 

77 

78if config["ADD_DUMMY_DATA"]: 

79 add_dummy_data() 

80 

81logger.info("Starting") 

82 

83if config["ROLE"] in ["scheduler", "all"]: 

84 scheduler = start_jobs_scheduler() 

85 

86if config["ROLE"] in ["worker", "all"]: 

87 for _ in range(config["BACKGROUND_WORKER_COUNT"]): 

88 start_jobs_worker() 

89 

90setup_tracing() 

91 

92if config["ROLE"] in ["api", "all"]: 

93 server = create_main_server(port=1751) 

94 server.start() 

95 media_server = create_media_server(port=1753) 

96 media_server.start() 

97 logger.info("Serving on 1751 (secure) and 1753 (media)") 

98 

99logger.info("App waiting for signal...") 

100 

101signal.pause()