Successfully migrated from MATLAB Engine API (direct Python-MATLAB integration) to HTTP-based microservice architecture.
- Web container attempted to install MATLAB Engine API Python package
- Direct Python-to-MATLAB connection via
matlab.engine - Complex dependency management and version compatibility issues
- Required MATLAB Engine compiled libraries in web container
- MATLAB container runs Flask HTTP API server on port 9090
- Web container makes HTTP requests to MATLAB container
- Clean separation: MATLAB stays fully in MATLAB container
- No MATLAB dependencies in web container
Purpose: Flask HTTP API server running in MATLAB container
Endpoints:
GET /health- Health check endpoint for Docker healthcheckPOST /execute- Execute MATLAB functions with args/kwargsPOST /eval- Evaluate MATLAB code strings
Features:
- Initializes MATLAB engine on startup
- Adds MATLAB paths and initializes COBRA Toolbox
- Converts MATLAB results to Python-compatible JSON
- Comprehensive error handling with 300s timeout
- Graceful shutdown handling
Purpose: HTTP client for web container to communicate with MATLAB API
Key Methods:
execute(function_name, *args, **kwargs)- Call MATLAB functionseval(matlab_code)- Evaluate MATLAB codehealth_check()- Verify MATLAB server availability
Features:
- Singleton pattern for connection pooling
- 300s timeout for long-running MATLAB operations
- Automatic retry logic
- Connection error handling
- Backwards compatible alias:
MatlabSessionManager = MatlabHTTPClient
Changes:
- ✅ Removed entire
matlab-builderstage - ✅ Removed all MATLAB Engine API installation steps
- ✅ Removed COPY commands from matlab-builder
- ✅ Simplified to single-stage build with Python 3.11-slim
- ✅ No MATLAB dependencies in web container
Changes:
- ✅ Added Flask installation:
pip3 install --break-system-packages flask - ✅ Changed CMD from
matlab_engine_server.pytomatlab_http_server.py - ✅ MATLAB Engine API remains available for Flask server to use
Web Service Changes:
- ✅ Removed:
MATLAB_REMOTE_ENABLED,MATLAB_SESSION_NAME - ✅ Added:
MATLAB_HOST=matlab,MATLAB_HTTP_PORT=9090
MATLAB Service Changes:
- ✅ Updated healthcheck from Python MATLAB Engine test to:
test: ["CMD", "curl", "-f", "http://localhost:9090/health", "||", "exit", "1"]
- ✅ Flask server starts on port 9090
Changes:
- ✅ Simplified import:
from reactions.utils.MatlabHTTPClient import MatlabSessionManager - ✅ Removed 80+ lines of MATLAB Engine import logic
- ✅ Removed all debug logging
- ✅ Uses HTTP client transparently
Changes:
- ✅ Simplified import logic
- ✅ Removed conditional import based on
MATLAB_REMOTE_ENABLED - ✅ Now only imports:
from reactions.utils.MatlabHTTPClient import MatlabSessionManager - ✅ All existing code using
MatlabSessionManager()works unchanged
Changes:
- ✅ Updated to use
MatlabHTTPClient - ✅ Changed environment variable checks to
MATLAB_HOSTandMATLAB_HTTP_PORT - ✅ Test suite remains compatible with
.execute()interface
The following files continue to work without modification:
/curationTool/reactions/utils/add_to_vmh_utils.pyadd_reaction_matlab()- Usesmatlab_session.execute('add_rxn_python', ...)add_metabolites_matlab()- Usesmatlab_session.execute('add_metab_python', ...)
All application code using the .execute() interface remains 100% compatible.
These files are no longer used but kept for reference:
/curationTool/reactions/utils/MatlabSessionManager.py- Old local MATLAB Engine API/curationTool/reactions/utils/MatlabSessionManagerRemote.py- Old remote MATLAB Engine API/docker/matlab/matlab_engine_server.py- Old MATLAB Engine sharing server
Note: These files won't be imported anymore and can be safely deleted after verifying the new system works.
MATLAB_REMOTE_ENABLED=true
MATLAB_SESSION_NAME=matlab_shared_sessionMATLAB_HOST=matlab # Docker service name
MATLAB_HTTP_PORT=9090 # Flask API portfrom reactions.utils.MatlabHTTPClient import MatlabSessionManager
matlab = MatlabSessionManager()
result = matlab.execute('generateVMHMetAbbr', 'glucose')
if result['status'] == 'success':
abbr = result['result']
print(f"Generated abbreviation: {abbr}")
else:
print(f"Error: {result['message']}")result = matlab.eval('2 + 2')
# Returns: {'status': 'success', 'result': 4.0}is_healthy = matlab.health_check()
# Returns: True if MATLAB server is responsivesudo docker compose buildsudo docker compose up -d# Check MATLAB container logs
sudo docker compose logs matlab
# Should see: "Starting MATLAB HTTP API server on port 9090"
# Should see: "MATLAB HTTP API server is ready on http://0.0.0.0:9090"# From host
curl http://localhost:9090/health
# Should return: {"status": "healthy", "matlab": "initialized"}# Run the test script
sudo docker compose exec web python /app/docker/test_matlab_connection.py
# Or access the web interface and test:
# - Generate VMH abbreviations
# - Add metabolites/reactions to VMH- No complex Python version matching
- No compiled library dependencies
- Clear container boundaries
- Easy to debug with curl/HTTP tools
- Clear API contracts
- Independent deployment and scaling
- HTTP timeouts prevent hanging
- Health checks for monitoring
- Graceful error handling
- Can be called from any language
- Easy to add new endpoints
- Simple to extend functionality
If issues occur, environment variables can be temporarily adjusted, but the old approach is deprecated. The HTTP API is the recommended production solution.
- ✅ All code updated
- ⏳ Rebuild Docker images
- ⏳ Test HTTP connectivity
- ⏳ Verify MATLAB function execution
- ⏳ Test full application workflow
- ⏳ Delete deprecated files after verification
For issues or questions:
- Check MATLAB container logs:
sudo docker compose logs matlab - Check web container logs:
sudo docker compose logs web - Test HTTP endpoint directly:
curl http://localhost:9090/health - Verify environment variables are set correctly