11import { and , eq , or } from 'drizzle-orm'
2- import { db } from '../../db'
2+ import { drizzle } from 'drizzle-orm/postgres-js'
3+ import postgres from 'postgres'
4+ import * as schema from '../../db/schema'
35import { workflow , workflowBlocks , workflowEdges , workflowSubflows } from '../../db/schema'
6+ import { env } from '../../lib/env'
47import { createLogger } from '../../lib/logs/console-logger'
58import { loadWorkflowFromNormalizedTables } from '../../lib/workflows/db-helpers'
69
710const logger = createLogger ( 'SocketDatabase' )
811
12+ // Create dedicated database connection for socket server with optimized settings
13+ const connectionString = env . POSTGRES_URL ?? env . DATABASE_URL
14+ const socketDb = drizzle (
15+ postgres ( connectionString , {
16+ prepare : false ,
17+ idle_timeout : 10 , // Shorter idle timeout for socket operations
18+ connect_timeout : 5 , // Faster connection timeout
19+ max : 2 , // Very small pool for socket server to avoid exhausting Supabase limit
20+ onnotice : ( ) => { } , // Disable notices
21+ debug : false , // Disable debug for socket operations
22+ } ) ,
23+ { schema }
24+ )
25+
26+ // Use dedicated connection for socket operations, fallback to shared db for compatibility
27+ const db = socketDb
28+
929// Constants
1030const DEFAULT_LOOP_ITERATIONS = 5
1131
@@ -115,9 +135,20 @@ export async function getWorkflowState(workflowId: string) {
115135
116136// Persist workflow operation
117137export async function persistWorkflowOperation ( workflowId : string , operation : any ) {
138+ const startTime = Date . now ( )
118139 try {
119140 const { operation : op , target, payload, timestamp, userId } = operation
120141
142+ // Log high-frequency operations for monitoring
143+ if ( op === 'update-position' && Math . random ( ) < 0.01 ) {
144+ // Log 1% of position updates
145+ logger . debug ( 'Socket DB operation sample:' , {
146+ operation : op ,
147+ target,
148+ workflowId : `${ workflowId . substring ( 0 , 8 ) } ...` ,
149+ } )
150+ }
151+
121152 await db . transaction ( async ( tx ) => {
122153 // Update the workflow's last modified timestamp first
123154 await tx
@@ -140,9 +171,22 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
140171 throw new Error ( `Unknown operation target: ${ target } ` )
141172 }
142173 } )
174+
175+ // Log slow operations for monitoring
176+ const duration = Date . now ( ) - startTime
177+ if ( duration > 100 ) {
178+ // Log operations taking more than 100ms
179+ logger . warn ( 'Slow socket DB operation:' , {
180+ operation : operation . operation ,
181+ target : operation . target ,
182+ duration : `${ duration } ms` ,
183+ workflowId : `${ workflowId . substring ( 0 , 8 ) } ...` ,
184+ } )
185+ }
143186 } catch ( error ) {
187+ const duration = Date . now ( ) - startTime
144188 logger . error (
145- `❌ Error persisting workflow operation (${ operation . operation } on ${ operation . target } ):` ,
189+ `❌ Error persisting workflow operation (${ operation . operation } on ${ operation . target } ) after ${ duration } ms :` ,
146190 error
147191 )
148192 throw error
0 commit comments