(
connectorId: string,
options?: { fullSync?: boolean; requestId?: string }
)
| 265 | * Dispatch a connector sync using the configured background execution backend. |
| 266 | */ |
| 267 | export async function dispatchSync( |
| 268 | connectorId: string, |
| 269 | options?: { fullSync?: boolean; requestId?: string } |
| 270 | ): Promise<void> { |
| 271 | const requestId = options?.requestId ?? generateId() |
| 272 | |
| 273 | if (isTriggerAvailable()) { |
| 274 | const connectorRows = await db |
| 275 | .select({ |
| 276 | knowledgeBaseId: knowledgeConnector.knowledgeBaseId, |
| 277 | connectorArchivedAt: knowledgeConnector.archivedAt, |
| 278 | connectorDeletedAt: knowledgeConnector.deletedAt, |
| 279 | workspaceId: knowledgeBase.workspaceId, |
| 280 | userId: knowledgeBase.userId, |
| 281 | kbDeletedAt: knowledgeBase.deletedAt, |
| 282 | }) |
| 283 | .from(knowledgeConnector) |
| 284 | .innerJoin(knowledgeBase, eq(knowledgeBase.id, knowledgeConnector.knowledgeBaseId)) |
| 285 | .where(eq(knowledgeConnector.id, connectorId)) |
| 286 | .limit(1) |
| 287 | |
| 288 | const row = connectorRows[0] |
| 289 | if (!row) { |
| 290 | logger.warn(`Skipping sync dispatch: connector not found`, { connectorId, requestId }) |
| 291 | return |
| 292 | } |
| 293 | if (row.kbDeletedAt) { |
| 294 | logger.warn(`Skipping sync dispatch: knowledge base is deleted`, { |
| 295 | connectorId, |
| 296 | knowledgeBaseId: row.knowledgeBaseId, |
| 297 | requestId, |
| 298 | }) |
| 299 | await db |
| 300 | .update(knowledgeConnector) |
| 301 | .set({ |
| 302 | status: 'error', |
| 303 | nextSyncAt: null, |
| 304 | lastSyncError: 'Knowledge base deleted', |
| 305 | updatedAt: new Date(), |
| 306 | }) |
| 307 | .where(eq(knowledgeConnector.id, connectorId)) |
| 308 | return |
| 309 | } |
| 310 | if (row.connectorArchivedAt || row.connectorDeletedAt) { |
| 311 | logger.warn(`Skipping sync dispatch: connector is archived or deleted`, { |
| 312 | connectorId, |
| 313 | requestId, |
| 314 | }) |
| 315 | return |
| 316 | } |
| 317 | |
| 318 | const tags = [`connectorId:${connectorId}`] |
| 319 | if (row.knowledgeBaseId) tags.push(`knowledgeBaseId:${row.knowledgeBaseId}`) |
| 320 | if (row.workspaceId) tags.push(`workspaceId:${row.workspaceId}`) |
| 321 | if (row.userId) tags.push(`userId:${row.userId}`) |
| 322 | |
| 323 | await knowledgeConnectorSync.trigger( |
| 324 | { |
no test coverage detected