-
Notifications
You must be signed in to change notification settings - Fork 34
Add gunicorn with TLS as web server for mapping app #529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4951751
Add gunicorn web serwer for mapping app
MikolajKasprzak d5826ef
change to 2 spaces
MikolajKasprzak 03c3d0a
fix rebase adjust to new implementation
MikolajKasprzak 943214c
fix model issues
MikolajKasprzak af1b701
fix log
MikolajKasprzak cc59a5e
fix healthcheck
MikolajKasprzak 092f072
Merge branch 'feature/mapping-service' into tls-mapping-service
saratpoluri 6675050
Merge branch 'feature/mapping-service' into tls-mapping-service
saratpoluri 3dd547c
Address code review comments
saratpoluri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,4 @@ flask-cors==4.0.0 | |
| gradio | ||
| open3d-cpu[headless]==0.19.0 | ||
| requests | ||
| gunicorn==21.2.0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,11 @@ | |
| Flask service with build-time model selection (no runtime model parameter needed). | ||
| """ | ||
|
|
||
| import argparse | ||
| import base64 | ||
| import os | ||
| import signal | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import time | ||
|
|
@@ -257,9 +259,105 @@ def signalHandler(sig, frame): | |
| log.info("Received SIGINT (Ctrl+C), shutting down gracefully...") | ||
| sys.exit(0) | ||
|
|
||
| def runDevelopmentServer(): | ||
| """Run Flask development server""" | ||
| log.info("Starting in DEVELOPMENT mode...") | ||
| log.info("Flask development server starting on http://0.0.0.0:8000") | ||
| log.info("Press Ctrl+C to stop the server") | ||
|
|
||
| try: | ||
| # Run Flask development server | ||
| app.run( | ||
| host="0.0.0.0", | ||
| port=8000, | ||
| debug=True, | ||
| threaded=True | ||
| ) | ||
| except KeyboardInterrupt: | ||
| log.info("Server interrupted by user") | ||
| except Exception as e: | ||
| log.error(f"Server error: {e}") | ||
| finally: | ||
| log.info("Server shutdown complete") | ||
|
|
||
| def runProductionServer(cert_file=None, key_file=None): | ||
| """Run Gunicorn production server with TLS""" | ||
| log.info("Starting in PRODUCTION mode with TLS...") | ||
|
|
||
| # Check if certificates exist | ||
| if not os.path.exists(cert_file): | ||
| log.error(f"TLS certificate file not found: {cert_file}") | ||
| sys.exit(1) | ||
|
|
||
| if not os.path.exists(key_file): | ||
| log.error(f"TLS key file not found: {key_file}") | ||
| sys.exit(1) | ||
|
|
||
| log.info(f"Using TLS certificate: {cert_file}") | ||
| log.info(f"Using TLS key: {key_file}") | ||
| log.info("Gunicorn HTTPS server starting on https://0.0.0.0:8000") | ||
|
|
||
| # Determine the service module based on model type | ||
| model_type = os.getenv("MODEL_TYPE", "mapanything") | ||
| service_module = f"{model_type}_service:app" | ||
|
|
||
| # Gunicorn command arguments | ||
| gunicorn_cmd = [ | ||
| "gunicorn", | ||
| "--bind", "0.0.0.0:8000", | ||
| "--workers", "1", | ||
saratpoluri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "--worker-class", "sync", | ||
| "--timeout", "300", | ||
| "--keep-alive", "5", | ||
| "--max-requests", "1000", | ||
| "--max-requests-jitter", "100", | ||
| "--access-logfile", "-", | ||
| "--error-logfile", "-", | ||
| "--log-level", "info", | ||
| "--certfile", cert_file, | ||
| "--keyfile", key_file, | ||
| service_module | ||
| ] | ||
|
|
||
| log.info(f"Starting Gunicorn with service module: {service_module}") | ||
|
|
||
| try: | ||
| # Run Gunicorn with TLS | ||
| subprocess.run(gunicorn_cmd, check=True) | ||
| except subprocess.CalledProcessError as e: | ||
| log.error(f"Gunicorn failed to start: {e}") | ||
| sys.exit(1) | ||
| except KeyboardInterrupt: | ||
| log.info("Server interrupted by user") | ||
| except Exception as e: | ||
| log.error(f"Server error: {e}") | ||
| sys.exit(1) | ||
|
|
||
| def startApp(): | ||
| """Start the application with model initialization""" | ||
| global device, loaded_model, model_name | ||
| """Start the application with command line argument parsing""" | ||
| parser = argparse.ArgumentParser(description="3D Mapping Models API Server") | ||
| parser.add_argument( | ||
| "--dev-mode", | ||
| action="store_true", | ||
| help="Run in development mode with Flask development server (default: production mode with Gunicorn + TLS)" | ||
| ) | ||
|
Comment on lines
+339
to
+343
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please consider removing dev-mode, I don't think we need it at all. |
||
| parser.add_argument( | ||
| "--development", | ||
| action="store_true", | ||
| help="Alias for --dev-mode" | ||
| ) | ||
| parser.add_argument( | ||
| "--cert-file", | ||
| default="/run/secrets/certs/scenescape-mapping.crt", | ||
| help="Path to TLS certificate file (default: /run/secrets/certs/scenescape-mapping.crt)" | ||
| ) | ||
| parser.add_argument( | ||
| "--key-file", | ||
| default="/run/secrets/certs/scenescape-mapping.key", | ||
| help="Path to TLS private key file (default: /run/secrets/certs/scenescape-mapping.key)" | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Set up signal handler for graceful shutdown | ||
| signal.signal(signal.SIGINT, signalHandler) | ||
|
|
@@ -268,23 +366,23 @@ def startApp(): | |
| log.info("Starting 3D Mapping API server...") | ||
|
|
||
| # Initialize model before starting server | ||
| global device, loaded_model, model_name | ||
| device = "cpu" | ||
| log.info(f"Using device: {device}") | ||
|
|
||
| try: | ||
| # Only initialize model if not already loaded | ||
| loaded_model, model_name = initializeModel() | ||
| log.info("API Service startup completed successfully") | ||
|
|
||
| log.info("Flask server starting on http://0.0.0.0:8000") | ||
| log.info("Press Ctrl+C to stop the server") | ||
| # Determine which server to run | ||
| dev_mode = args.dev_mode or args.development or os.getenv("DEV_MODE", "").lower() in ("true", "1", "yes") | ||
|
|
||
| if dev_mode: | ||
| runDevelopmentServer() | ||
| else: | ||
| runProductionServer(cert_file=args.cert_file, key_file=args.key_file) | ||
|
|
||
| # Run Flask development server | ||
| app.run( | ||
| host="0.0.0.0", | ||
| port=8000, | ||
| debug=False, | ||
| threaded=True | ||
| ) | ||
| except KeyboardInterrupt: | ||
| log.info("Server interrupted by user") | ||
| except Exception as e: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,10 @@ secrets: | |
| file: ${SECRETSDIR}/certs/scenescape-camcalibration.crt | ||
| camcalibration-key: | ||
| file: ${SECRETSDIR}/certs/scenescape-camcalibration.key | ||
| mapping-cert: | ||
| file: ${SECRETSDIR}/certs/scenescape-mapping.crt | ||
| mapping-key: | ||
| file: ${SECRETSDIR}/certs/scenescape-mapping.key | ||
|
|
||
| services: | ||
| ntpserv: | ||
|
|
@@ -258,7 +262,8 @@ services: | |
| retail-cams: | ||
| condition: service_started | ||
| healthcheck: | ||
| test: ["CMD", "curl", "-I", "-s", "http://localhost:8080/pipelines"] | ||
| test: | ||
| ["CMD", "curl", "-k", "-I", "-s", "https://localhost:8080/pipelines"] | ||
| interval: 10s | ||
| timeout: 5s | ||
| retries: 5 | ||
|
|
@@ -422,8 +427,15 @@ services: | |
| - NO_PROXY=${no_proxy},.scenescape.intel.com,influxdb2 | ||
| - HTTP_PROXY=${http_proxy} | ||
| - HTTPS_PROXY=${https_proxy} | ||
| secrets: | ||
| - source: root-cert | ||
| target: certs/scenescape-ca.pem | ||
| - source: mapping-cert | ||
| target: certs/scenescape-mapping.crt | ||
| - source: mapping-key | ||
| target: certs/scenescape-mapping.key | ||
| healthcheck: | ||
| test: ["CMD", "curl", "-I", "-s", "http://localhost:8000/health"] | ||
| test: ["CMD", "curl", "-k", "-I", "-s", "https://localhost:8000/health"] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you change also port number? 8000 is typically used for HTTP |
||
| interval: 10s | ||
| timeout: 60s | ||
| retries: 5 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.