forked from onyekaugochukwu/sre-week-three-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
33 lines (26 loc) · 844 Bytes
/
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
from flask import Flask, request
import threading
import time
import atexit
import os
import signal
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Stripe Portal is up!'
def shutdown():
try:
time.sleep(60)
func = request.environ.get('werkzeug.server.shutdown')
if func is not None:
func()
except RuntimeError: # Handle exception if no request context
print("Shutdown failed due to missing request context")
os.kill(os.getpid(), signal.SIGTERM) # Alternative shutdown method
shutdown_thread = threading.Thread(target=shutdown)
shutdown_thread.start()
def shutdown_on_exit():
shutdown_thread.join(1) # Wait for shutdown thread to finish
atexit.register(shutdown_on_exit)
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=5003) # Listen on all interfaces, port 5000