-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
72 lines (63 loc) · 2.46 KB
/
manage.py
File metadata and controls
72 lines (63 loc) · 2.46 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
#!/usr/bin/env python
from config import Config
import subprocess
import argparse
import sys
import os
# Ensure the root directory is in sys.path
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
def run(workers="1", host="localhost", port=8080, reload=False):
"""
Runs the application.
"""
# Validate.
Config.validate()
if reload:
from app import create_app
app = create_app()
app.run(
host=host,
port=port,
debug=True,
extra_files=[
'templates/**/*.html',
'static/**/*.scss'
]
)
else:
subprocess.run([
"gunicorn",
"--bind", f"{host}:{port}",
"--workers", workers,
"--worker-class", "gevent",
"--timeout", "300", # Increase timeout to 5 minutes.
"--keep-alive", "5", # Keep connections alive for 5 seconds.
"--max-requests", "1000", # Restart workers after 1000 requests.
"--max-requests-jitter", "50", # Add some randomness to prevent all workers from restarting at once.
"--worker-connections", "1000", # Maximum number of simultaneous connections per worker.
"--graceful-timeout", "120", # Give workers 2 minutes to finish their work.
"--log-level", "info", # Log level for Gunicorn.
"--threads", "4", # Threads per worker.
"app:app"
])
def main():
"""
Main function to parse arguments and run the appropriate command.
"""
parser = argparse.ArgumentParser(description='Flask Management Script')
subparsers = parser.add_subparsers(dest='command', help='Available commands')
# Run command
run_parser = subparsers.add_parser('run', help='Run the application')
run_parser.add_argument('--workers', default='1', help='Number of workers to run with Gunicorn')
run_parser.add_argument('--host', default='localhost', help='Host to run the application on')
run_parser.add_argument('--port', type=int, default=8080, help='Port to run the application on')
run_parser.add_argument('--reload', action='store_true', help='Run with development server')
args = parser.parse_args()
if args.command == 'run':
print("Starting server...")
print(f"Starting with reload: {args.reload}")
run(args.workers, args.host, args.port, args.reload)
else:
parser.print_help()
if __name__ == "__main__":
main()