-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdaemon.py
More file actions
102 lines (91 loc) · 3.63 KB
/
daemon.py
File metadata and controls
102 lines (91 loc) · 3.63 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
import sentry_sdk
import subprocess
import psycopg2
import logging
import sys
import os
import generate_tusker_config
import generate_uwsgi_config
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from src.internals.database import database
from src.config import Configuration
if __name__ == '__main__':
''' Bugs to fix at a later time: '''
''' - Pages can get stuck with an older version of their '''
''' HTML, even after disabling anything and everything '''
''' related to cache. The only resolution as of now is '''
''' a restart of the entire webserver. '''
environment_vars = {
**os.environ.copy(),
'FLASK_ENV': 'development' if Configuration().development_mode else 'production',
'NODE_ENV': 'development' if Configuration().development_mode else 'production',
'KEMONO_SITE': Configuration().webserver['site']
}
try:
''' Initialize Sentry. '''
if Configuration().sentry_dsn:
sentry_sdk.init(
integrations=[FlaskIntegration(), RedisIntegration()],
dsn=Configuration().sentry_dsn
)
''' Install client dependencies. '''
if not os.path.isdir('./client/node_modules'):
subprocess.run(
['npm', 'ci', '--also=dev'],
check=True,
cwd='client',
env=environment_vars
)
''' Build or run client development server depending on config. '''
if Configuration().development_mode:
subprocess.Popen(
['npm', 'run', 'dev'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd='client',
env=environment_vars
)
else:
subprocess.run(
['npm', 'run', 'build'],
check=True,
cwd='client',
env=environment_vars
)
''' Run `tusd`. '''
if Configuration().filehaus['tus']['manage']:
subprocess.Popen(
[
'tusd',
'-upload-dir=./storage/uploads',
'--hooks-enabled-events',
'post-create',
'-hooks-http',
'http://127.0.0.1:3343'
# 'http://127.0.0.1:6942/shares/tus'
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=environment_vars
)
database.init()
try:
if Configuration().automatic_migrations:
''' Run migrations. '''
generate_tusker_config.run_migrations()
''' Initialize Pgroonga if needed. '''
with database.pool.getconn() as conn:
with conn.cursor() as db:
db.execute('CREATE EXTENSION IF NOT EXISTS pgroonga')
db.execute('CREATE INDEX IF NOT EXISTS pgroonga_posts_idx ON posts USING pgroonga (title, content)')
db.execute('CREATE INDEX IF NOT EXISTS pgroonga_dms_idx ON dms USING pgroonga (content)')
conn.commit()
database.pool.putconn(conn)
finally:
''' "Close" the database pool. '''
database.close_pool()
generate_uwsgi_config.generate()
subprocess.run(['uwsgi', '--ini', './uwsgi.ini'], check=True, close_fds=True, env=environment_vars)
except KeyboardInterrupt:
sys.exit()