forked from Haidra-Org/AI-Horde
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
106 lines (84 loc) · 3.11 KB
/
Copy pathserver.py
File metadata and controls
106 lines (84 loc) · 3.11 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# SPDX-FileCopyrightText: 2022 Konstantinos Thoukydidis <mail@dbzer0.com>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
import logging
import os
from dotenv import load_dotenv
profile = os.environ.get("PROFILE")
if profile is not None:
env_file = f".env_{profile}"
load_dotenv(env_file)
else:
load_dotenv()
from horde.argparser import args
from horde.flask import create_app
from horde.logger import logger, reconfigure_from_args
from horde.metrics import waitress_metrics
from horde.telemetry import init_telemetry_late, telemetry_enabled
reconfigure_from_args(args)
app = create_app()
if telemetry_enabled():
init_telemetry_late(app)
# CLI modes (moved from horde/__init__.py)
if args.force_subscription:
from horde.ops import force_subscription_kudos
logger.info(f"forcing kudos on user_id: {args.force_subscription}")
force_subscription_kudos(args.force_subscription, args.prevent_date_change)
import sys
sys.exit()
if args.test:
from horde.sandbox import test
test()
if args.check_prompts:
import horde.database.threads as threads
threads.check_waiting_prompts()
import sys
sys.exit()
if args.new_patreons:
import sys
sys.exit()
if __name__ == "__main__":
# Only setting this for the WSGI logs
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(module)s:%(lineno)d - %(message)s",
level=logging.WARNING,
)
# waitress.queue logs "Task queue depth is N" at WARNING for every task
# enqueued while no worker thread is idle. Under load this fires thousands
# of times per minute and drowns out useful signal. Saturation is already
# tracked via the waitress_metrics histogram + telemetry, so silence the
# per-event log.
logging.getLogger("waitress.queue").setLevel(logging.ERROR)
import waitress
# Monkeypatch to get metrics until below is done
# https://github.com/Pylons/waitress/issues/182
_create_server = waitress.create_server
def create_server(*args, **kwargs):
server = _create_server(*args, **kwargs)
waitress_metrics.setup(server.task_dispatcher)
return server
waitress.create_server = create_server
logger.init("WSGI Server", status="Starting")
url_scheme = "https"
if args.insecure:
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
url_scheme = "http"
logger.init_warn("WSGI Mode", status="Insecure")
logger.warning(
"INSECURE MODE (-i/--insecure): Serving over HTTP. "
"OAuth redirect URIs will use http:// instead of https://. "
"This causes redirect_uri_mismatch errors with OAuth providers "
"(Google, Discord, GitHub) unless each provider's developer "
"console explicitly allows http:// callbacks. "
"This flag MUST NOT be used in production."
)
waitress.serve(
app,
host=args.listen,
port=args.port,
url_scheme=url_scheme,
threads=args.waitress_threads,
connection_limit=args.waitress_connection_limit,
asyncore_use_poll=True,
)
logger.init("WSGI Server", status="Stopped")