@@ -21,7 +21,6 @@ import localDevBypassAuthPermissions from './auth/local-dev/permissions.js'
2121import MutableOrgDS from './model/MutableOrganizationDataSource.js'
2222import UserDataSource from './model/UserDataSource.js'
2323import BulkImportDataSource from './model/BulkImportDataSource.js'
24- import { errorMonitor } from './utils/ErrorMonitor.js'
2524
2625/**
2726 * Create a GraphQL server
@@ -50,95 +49,26 @@ export async function createServer (): Promise<{ app: express.Application, serve
5049 schema,
5150 plugins : [ ApolloServerPluginDrainHttpServer ( { httpServer } ) ] ,
5251 cache : new InMemoryLRUCache ( {
53- max : 100 ,
54- maxSize : 1024 * 1024 * 20 , // Increased cache size
55- ttl : 300000 // 5 minutes TTL to prevent memory leaks
56- } ) ,
57- // Enhanced error handling
58- formatError : ( formattedError , _error ) => {
59- // Log the error with enhanced monitoring
60- errorMonitor . logGraphQLError ( formattedError , undefined , undefined )
61-
62- // Don't expose internal errors in production
63- if ( process . env . NODE_ENV === 'production' ) {
64- // Remove stack trace and internal details
65- if ( formattedError . extensions ?. exception != null ) {
66- delete ( formattedError . extensions . exception as any ) . stacktrace
67- }
68- if ( formattedError . message . includes ( 'internal' ) ||
69- formattedError . message . includes ( 'database' ) ) {
70- return new Error ( 'Internal server error' )
71- }
72- }
73-
74- return formattedError
75- }
52+ max : 50 ,
53+ maxSize : 1024 * 1024 * 10
54+ } )
7655 } )
7756 // server must be started before applying middleware
7857 await server . start ( )
7958
8059 const context = process . env . LOCAL_DEV_BYPASS_AUTH === 'true' ? localDevBypassAuthContext : createContext
8160
82- // Enhanced health check with memory monitoring
8361 app . get ( '/health' , ( req , res ) => {
8462 const memUsage = process . memoryUsage ( )
85- const uptime = process . uptime ( )
86-
87- // Check if memory usage is getting too high
88- const heapUsedMB = Math . round ( memUsage . heapUsed / 1024 / 1024 )
89- const heapTotalMB = Math . round ( memUsage . heapTotal / 1024 / 1024 )
90- const memoryUsagePercent = ( heapUsedMB / heapTotalMB ) * 100
91-
92- const status = memoryUsagePercent > 85 ? 'warning' : 'ok'
93-
9463 res . json ( {
95- status,
64+ status : 'ok' ,
9665 timestamp : new Date ( ) . toISOString ( ) ,
97- uptime : `${ Math . round ( uptime / 60 ) } minutes` ,
9866 memory : {
9967 rss : `${ Math . round ( memUsage . rss / 1024 / 1024 ) } MB` ,
100- heapTotal : `${ heapTotalMB } MB` ,
101- heapUsed : `${ heapUsedMB } MB` ,
102- external : `${ Math . round ( memUsage . external / 1024 / 1024 ) } MB` ,
103- usagePercent : `${ Math . round ( memoryUsagePercent ) } %`
104- } ,
105- warnings : memoryUsagePercent > 85 ? [ 'High memory usage detected' ] : [ ]
106- } )
107-
108- // Log warning if memory usage is high
109- if ( memoryUsagePercent > 85 ) {
110- console . warn ( `High memory usage: ${ Math . round ( memoryUsagePercent ) } % (${ heapUsedMB } MB/${ heapTotalMB } MB)` )
111- }
112- } )
113-
114- // Periodic memory cleanup and monitoring
115- setInterval ( ( ) => {
116- const memUsage = process . memoryUsage ( )
117- const heapUsedMB = Math . round ( memUsage . heapUsed / 1024 / 1024 )
118- const heapTotalMB = Math . round ( memUsage . heapTotal / 1024 / 1024 )
119- const memoryUsagePercent = ( heapUsedMB / heapTotalMB ) * 100
120-
121- // Force garbage collection if memory usage is high and gc is available
122- if ( memoryUsagePercent > 80 && global . gc != null ) {
123- console . log ( 'Running garbage collection due to high memory usage' )
124- global . gc ( )
125- }
126-
127- // Log memory stats every 5 minutes
128- console . log ( `Memory usage: ${ heapUsedMB } MB/${ heapTotalMB } MB (${ Math . round ( memoryUsagePercent ) } %)` )
129- } , 5 * 60 * 1000 ) // Every 5 minutes
130-
131- // Error monitoring endpoint
132- app . get ( '/errors' , ( req , res ) => {
133- const stats = errorMonitor . getStats ( )
134- const healthStatus = errorMonitor . getHealthStatus ( )
135-
136- res . json ( {
137- healthStatus,
138- totalErrors : stats . totalErrors ,
139- errorsByType : Object . fromEntries ( stats . errorsByType ) ,
140- recentErrors : stats . recentErrors . slice ( - 10 ) , // Last 10 errors
141- lastReset : stats . lastReset
68+ heapTotal : `${ Math . round ( memUsage . heapTotal / 1024 / 1024 ) } MB` ,
69+ heapUsed : `${ Math . round ( memUsage . heapUsed / 1024 / 1024 ) } MB` ,
70+ external : `${ Math . round ( memUsage . external / 1024 / 1024 ) } MB`
71+ }
14272 } )
14373 } )
14474
0 commit comments