lookyloo/bin/start_website.py

48 lines
1.4 KiB
Python
Raw Normal View History

2019-01-23 15:13:29 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import signal
2019-01-23 15:13:29 +01:00
from subprocess import Popen
from lookyloo.helpers import get_homedir, shutdown_requested, set_running, unset_running, get_socket_path, get_config
2019-04-05 16:14:30 +02:00
from redis import StrictRedis
2019-01-23 15:13:29 +01:00
def main():
r = StrictRedis(unix_socket_path=get_socket_path('cache'))
r.delete('cache_loaded')
2019-01-30 14:30:01 +01:00
website_dir = get_homedir() / 'website'
ip = get_config('generic', 'website_listen_ip')
port = get_config('generic', 'website_listen_port')
2019-01-23 15:13:29 +01:00
try:
p = Popen(['gunicorn', '-w', '10',
2020-01-21 17:39:18 +01:00
'--graceful-timeout', '2', '--timeout', '300',
'-b', f'{ip}:{port}',
2019-11-02 05:05:08 +01:00
'--log-level', 'info',
'web:app'],
cwd=website_dir)
set_running('website')
while True:
if p.poll() is not None:
print('gunicorn stopped itself.')
break
if shutdown_requested():
print('"shutdown" key present in the cache database.')
break
time.sleep(1)
2019-01-23 15:13:29 +01:00
except KeyboardInterrupt:
print('Website killed by user.')
finally:
print('Shutting down website.')
try:
# Killing everything if possible.
p.send_signal(signal.SIGWINCH)
p.send_signal(signal.SIGTERM)
except Exception:
pass
unset_running('website')
if __name__ == '__main__':
main()