forked from anoncoder9/Kemono2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.py
76 lines (66 loc) · 2.76 KB
/
daemon.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
import subprocess
import psycopg2
import sys
import os
import generate_tusker_config
import generate_uwsgi_config
from src.config import Configuration
from src.internals.database import database
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:
''' Install client dependencies. '''
if not os.path.isdir('./client/node_modules'):
subprocess.run(
['npm', 'install'],
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
)
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_comments_idx ON comments USING pgroonga (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()