-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·248 lines (203 loc) · 8.58 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·248 lines (203 loc) · 8.58 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
# RiskX Backend Startup Script for Render
set -e
echo "=== RiskX Backend Startup ==="
# Ensure we're in the correct directory
cd /opt/render/project
# Debug environment
echo "Current directory: $(pwd)"
echo "Python version: $(python3 --version)"
echo "Available Python modules:"
python3 -c "import sys; print('sys.path:', sys.path[:3])"
# Set Python path - only project root
export PYTHONPATH="/opt/render/project"
echo "PYTHONPATH set to: $PYTHONPATH"
# Create necessary directories
mkdir -p data/cache/data data/cache/metadata data/cache/fallback logs
# Validate environment
echo "Environment: ${ENVIRONMENT:-production}"
echo "Debug mode: ${DEBUG:-false}"
echo "Log level: ${LOG_LEVEL:-info}"
echo "Port: ${PORT:-10000}"
# CRITICAL: Debug and fix directory structure issue
echo "=== CRITICAL DIRECTORY STRUCTURE ANALYSIS ==="
python3 -c "
import sys
import os
print('=== ENVIRONMENT ANALYSIS ===')
print('Working directory:', os.getcwd())
print('PYTHONPATH env var:', os.environ.get('PYTHONPATH', 'NOT SET'))
print('sys.path (first 5):', sys.path[:5])
print('\n=== PROJECT STRUCTURE VALIDATION ===')
project_root = '/opt/render/project'
if os.path.exists(project_root):
root_contents = os.listdir(project_root)
print(f'Project root contents: {root_contents[:10]}')
# Check if src exists and what it contains
src_path = os.path.join(project_root, 'src')
if os.path.exists(src_path):
src_contents = os.listdir(src_path)
print(f'src directory contents: {src_contents[:10]}')
# CRITICAL CHECK: Does src contain the expected Python modules?
expected_modules = ['api', 'core', 'cache', 'data', 'ml']
found_modules = [m for m in expected_modules if m in src_contents]
missing_modules = [m for m in expected_modules if m not in src_contents]
print(f'Expected modules found: {found_modules}')
print(f'Missing modules: {missing_modules}')
if len(found_modules) >= 3:
print('✅ src directory appears to contain source code')
else:
print('❌ CRITICAL: src directory does NOT contain expected source code!')
print('❌ This explains the import failures!')
# Try to find the real source code
for item in root_contents:
item_path = os.path.join(project_root, item)
if os.path.isdir(item_path) and item != 'src':
try:
sub_contents = os.listdir(item_path)
if 'api' in sub_contents and 'core' in sub_contents:
print(f'🔍 Found potential source code in: {item}/')
print(f' Contents: {sub_contents[:10]}')
except:
pass
else:
print('❌ src directory does not exist at all!')
else:
print('❌ Project root directory does not exist!')
print('\n=== PYTHON PATH SETUP ===')
sys.path.insert(0, project_root)
print(f'Added {project_root} to sys.path')
print('Updated sys.path (first 3):', sys.path[:3])
"
# CRITICAL: Test imports with comprehensive error handling and recovery
echo "=== TESTING IMPORTS WITH RECOVERY MECHANISM ==="
python3 -c "
import sys
import os
# Ensure project root is in path
project_root = '/opt/render/project'
if project_root not in sys.path:
sys.path.insert(0, project_root)
print('Testing import with project root in path...')
print('sys.path (first 3):', sys.path[:3])
def test_import():
try:
# Test basic src import
import src
print('✅ src package import successful')
# Test api import
from src.api.main import app
print('✅ src.api.main import successful')
return True, None
except Exception as e:
print(f'❌ Import failed: {e}')
return False, str(e)
success, error = test_import()
if not success:
print('🔧 ATTEMPTING RECOVERY...')
# CRITICAL: Handle nested src directory structure
print('🔍 SEARCHING FOR REAL SOURCE CODE...')
# Check for nested src structure first (most likely based on error analysis)
nested_src_path = '/opt/render/project/src/src'
if os.path.exists(nested_src_path):
try:
nested_contents = os.listdir(nested_src_path)
print(f'Found nested src at: {nested_src_path}')
print(f'Nested src contents: {nested_contents[:10]}')
if any(module in nested_contents for module in ['api', 'core', 'cache']):
print('✅ FOUND REAL SOURCE CODE in nested src directory!')
# Add the parent of the real src to Python path
parent_path = '/opt/render/project/src'
if parent_path not in sys.path:
sys.path.insert(0, parent_path)
print(f'Added {parent_path} to sys.path')
# Test import with nested structure
try:
from src.api.main import app
print('✅ RECOVERY SUCCESSFUL: Nested src import works!')
success = True
except Exception as e2:
print(f'⚠️ Nested src recovery failed: {e2}')
except Exception as e:
print(f'Error checking nested src: {e}')
# If nested src didn't work, try other locations
if not success:
search_paths = [
'/opt/render/project',
'/opt/render/project/app',
'/app',
'/workspace',
]
for search_path in search_paths:
if os.path.exists(search_path):
try:
contents = os.listdir(search_path)
if 'src' in contents:
src_path = os.path.join(search_path, 'src')
src_contents = os.listdir(src_path)
if any(module in src_contents for module in ['api', 'core', 'cache']):
print(f'🔍 Found valid src directory at: {src_path}')
if search_path not in sys.path:
sys.path.insert(0, search_path)
print(f'Added {search_path} to sys.path')
# Test import again
try:
from src.api.main import app
print('✅ RECOVERY SUCCESSFUL: Import now works!')
success = True
break
except Exception as e2:
print(f'⚠️ Recovery attempt failed: {e2}')
except Exception:
continue
if not success:
print('❌ CRITICAL: Unable to locate valid source code')
print('❌ The deployment has structural issues that prevent startup')
exit(1)
print('🎯 Import validation completed successfully')
"
# Start the application with dynamically determined Python path
echo "=== STARTING FASTAPI SERVER ==="
echo "Port: ${PORT:-10000}"
# Determine the correct Python path based on where we found the source code
python3 -c "
import sys
import os
# Try to determine the correct path for imports
def find_correct_path():
# Check nested src structure first (most likely)
nested_src = '/opt/render/project/src/src'
if os.path.exists(nested_src):
nested_contents = os.listdir(nested_src)
if any(module in nested_contents for module in ['api', 'core', 'cache']):
return '/opt/render/project/src'
# Check standard structure
if os.path.exists('/opt/render/project/src'):
src_contents = os.listdir('/opt/render/project/src')
if any(module in src_contents for module in ['api', 'core', 'cache']):
return '/opt/render/project'
return '/opt/render/project' # Default fallback
correct_path = find_correct_path()
print(f'Using Python path: {correct_path}')
if correct_path not in sys.path:
sys.path.insert(0, correct_path)
print(f'Final sys.path: {sys.path[:3]}')
# Import and start the application
try:
import uvicorn
from src.api.main import app
print('✅ Successfully imported FastAPI app')
print('🚀 Starting uvicorn server...')
uvicorn.run(
app,
host='0.0.0.0',
port=${PORT:-10000},
workers=1,
log_level='${LOG_LEVEL:-info}'.lower()
)
except Exception as e:
print(f'❌ CRITICAL STARTUP FAILURE: {e}')
import traceback
traceback.print_exc()
exit(1)
"