-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix(sockets-server-disconnection): on reconnect force sync store to db #638
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| import crypto from 'crypto' | ||
| import { eq } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { getSession } from '@/lib/auth' | ||
| import { createLogger } from '@/lib/logs/console-logger' | ||
| import { getUserEntityPermissions } from '@/lib/permissions/utils' | ||
| import { saveWorkflowToNormalizedTables } from '@/lib/workflows/db-helpers' | ||
| import { workflowStateApiSchema } from '@/lib/workflows/validation' | ||
| import { db } from '@/db' | ||
| import { workflow } from '@/db/schema' | ||
|
|
||
| const logger = createLogger('ForceSync') | ||
|
|
||
| /** | ||
| * POST /api/workflows/[id]/force-sync | ||
| * Force sync local workflow state to database immediately | ||
| * Used during socket reconnection to ensure local changes are persisted | ||
| */ | ||
| export async function POST(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { | ||
| const requestId = crypto.randomUUID().slice(0, 8) | ||
| const { id: workflowId } = await params | ||
|
|
||
| try { | ||
| logger.info(`[${requestId}] Force sync request for workflow ${workflowId}`) | ||
|
|
||
| // Get session | ||
| const session = await getSession() | ||
| if (!session?.user?.id) { | ||
| logger.warn(`[${requestId}] Unauthorized force sync attempt`) | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const userId = session.user.id | ||
|
|
||
| // Get workflow and verify access (inline implementation) | ||
| const workflowData = await db | ||
| .select({ | ||
| userId: workflow.userId, | ||
| workspaceId: workflow.workspaceId, | ||
| name: workflow.name, | ||
| }) | ||
| .from(workflow) | ||
| .where(eq(workflow.id, workflowId)) | ||
| .limit(1) | ||
|
|
||
| if (!workflowData.length) { | ||
| logger.warn(`[${requestId}] Workflow ${workflowId} not found`) | ||
| return NextResponse.json({ error: 'Workflow not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| const workflowRecord = workflowData[0] | ||
| let hasAccess = false | ||
|
|
||
| // Check if user owns the workflow | ||
| if (workflowRecord.userId === userId) { | ||
| hasAccess = true | ||
| } | ||
|
|
||
| // Check workspace membership if workflow belongs to a workspace | ||
| if (!hasAccess && workflowRecord.workspaceId) { | ||
| const userPermission = await getUserEntityPermissions( | ||
| userId, | ||
| 'workspace', | ||
| workflowRecord.workspaceId | ||
| ) | ||
| hasAccess = userPermission !== null | ||
| } | ||
|
|
||
| if (!hasAccess) { | ||
| logger.warn(`[${requestId}] Access denied for user ${userId} to workflow ${workflowId}`) | ||
| return NextResponse.json({ error: 'Access denied' }, { status: 403 }) | ||
| } | ||
|
|
||
| // Parse and validate the workflow state from request body | ||
| const body = await request.json() | ||
| const { workflowState } = body | ||
icecrasher321 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!workflowState) { | ||
| return NextResponse.json({ error: 'Missing workflowState in request body' }, { status: 400 }) | ||
| } | ||
|
|
||
| // Validate the workflow state structure | ||
| const validationResult = workflowStateApiSchema.safeParse(workflowState) | ||
| if (!validationResult.success) { | ||
| logger.error(`[${requestId}] Invalid workflow state structure:`, { | ||
| error: validationResult.error, | ||
| receivedData: JSON.stringify(workflowState, null, 2), | ||
| }) | ||
| return NextResponse.json( | ||
| { | ||
| error: 'Invalid workflow state structure', | ||
| details: validationResult.error.issues, | ||
| receivedKeys: Object.keys(workflowState || {}), | ||
| }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| const validatedState = validationResult.data | ||
|
|
||
| // Save to normalized tables | ||
| logger.info(`[${requestId}] Saving workflow state to normalized tables`) | ||
|
|
||
| // Convert deployedAt to Date if it's a string | ||
| let deployedAt: Date | undefined | ||
| if (validatedState.deployedAt) { | ||
| if (typeof validatedState.deployedAt === 'string') { | ||
| deployedAt = new Date(validatedState.deployedAt) | ||
| } else if (validatedState.deployedAt instanceof Date) { | ||
| deployedAt = validatedState.deployedAt | ||
| } | ||
| } | ||
|
|
||
| const saveResult = await saveWorkflowToNormalizedTables(workflowId, { | ||
| blocks: validatedState.blocks, | ||
| edges: validatedState.edges, | ||
| loops: validatedState.loops || {}, | ||
| parallels: validatedState.parallels || {}, | ||
| lastSaved: Date.now(), | ||
| isDeployed: validatedState.isDeployed, | ||
| deployedAt, | ||
| deploymentStatuses: validatedState.deploymentStatuses || {}, | ||
| hasActiveSchedule: validatedState.hasActiveSchedule || false, | ||
| hasActiveWebhook: validatedState.hasActiveWebhook || false, | ||
| }) | ||
icecrasher321 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!saveResult.success) { | ||
| logger.error(`[${requestId}] Failed to save workflow state:`, saveResult.error) | ||
| return NextResponse.json( | ||
| { error: saveResult.error || 'Failed to save workflow state' }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
|
|
||
| // Update workflow's last_synced timestamp | ||
| await db | ||
| .update(workflow) | ||
| .set({ | ||
| lastSynced: new Date(), | ||
| updatedAt: new Date(), | ||
| }) | ||
| .where(eq(workflow.id, workflowId)) | ||
|
|
||
| logger.info(`[${requestId}] Successfully force synced workflow ${workflowId}`) | ||
|
|
||
| // Notify socket server about the sync for real-time updates | ||
| try { | ||
| const socketServerUrl = process.env.SOCKET_SERVER_URL || 'http://localhost:3002' | ||
| await fetch(`${socketServerUrl}/api/workflow-synced`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ | ||
| workflowId, | ||
| timestamp: Date.now(), | ||
| userId, | ||
| }), | ||
| }) | ||
| logger.debug(`[${requestId}] Notified socket server about force sync`) | ||
| } catch (socketError) { | ||
| // Don't fail the request if socket notification fails | ||
| logger.warn(`[${requestId}] Failed to notify socket server about sync:`, socketError) | ||
| } | ||
|
|
||
| return NextResponse.json({ | ||
| success: true, | ||
| message: 'Workflow state synced successfully', | ||
| timestamp: Date.now(), | ||
| }) | ||
| } catch (error: any) { | ||
| logger.error(`[${requestId}] Force sync error:`, error) | ||
| return NextResponse.json({ error: error.message || 'Internal server error' }, { status: 500 }) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.