-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
58 lines (48 loc) · 1.6 KB
/
app.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
from multiprocessing import Pool, Queue
from waitress import serve
import os
from sys import exit
from src.flask_app import FlaskWrapper
from src.worker import Worker
import src.log as log
def resolve_work(queue: Queue):
log.info(f'[{os.getpid()}] Starting worker process.')
try:
log.info(f'[{os.getpid()}] Listening to queue.')
while True:
if not queue.empty():
work = queue.get()
Worker(work).resolve()
except Exception as e:
log.error(f'[{os.getpid()}] Failed to start worker process.')
log.error(e.with_traceback())
if __name__ == '__main__':
log_dir = os.getenv('LOG_DIR')
log.init(log_dir)
log.info('Flask Process Started.')
try:
log.info('Creating Queue.')
work_queue = Queue()
log.info('Succesfully created Queue.')
except Exception as e:
log.error('Failed to Create Queue.')
log.error(e.with_traceback())
exit(1)
try:
log.info('Creating Pool.')
cpu_core = int(os.getenv('CPU_CORE'))
pool = Pool(cpu_core, initializer=resolve_work, initargs=(work_queue, ))
log.info('Succesfully Created Pool.')
except Exception as e:
log.error('Failed to Create Pool.')
log.error(e.with_traceback())
exit(1)
try:
log.info('Starting Server.')
listen_port = os.getenv('PORT')
FlaskWrapper.set_queue(work_queue)
serve(FlaskWrapper.app, port=listen_port)
except Exception as e:
log.error('Failed to start server.')
log.error(e.with_traceback())
exit(1)