Skip to content

Commit

Permalink
Make search sync asynchronous when merging orgs
Browse files Browse the repository at this point in the history
There are two issues with search sync being synchronous right now:
1. It might take longer to sync to search than to execute a finalizing
   temporal workflow. If this happens, temporal workflow will notify
   frontend about finishing org merging before this method returns. This
   means, frontend learns about org merging being done before it learns
   the org merging even started.
2. Synchronous search sync might fail. Since we execute it outside of a
   database transaction, the error frontend sees will be misleading.
   Because, on one hand, the request to merge orgs failed, but on the
   other hand it actually did something, transaction got commited, it
   can't be retried.

As a result, the solution:
1. Make search sync asynchronous, so it doesn't fail (or fails much less
   often)
2. Execute it before starting executing temporal workflow. So there is
   as little delay as possible between starting a temporal workflow and
   returning something to frontend
  • Loading branch information
sausage-todd committed Jan 9, 2024
1 parent 761c142 commit 9555938
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions backend/src/services/organizationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ export default class OrganizationService extends LoggerBase {
this.options,
)

const searchSyncService = new SearchSyncService(this.options, SyncMode.ASYNCHRONOUS)

await searchSyncService.triggerOrganizationSync(this.options.currentTenant.id, originalId)
await searchSyncService.triggerRemoveOrganization(this.options.currentTenant.id, toMergeId)

// sync organization members
await searchSyncService.triggerOrganizationMembersSync(
this.options.currentTenant.id,
originalId,
)

// sync organization activities
await searchSyncService.triggerOrganizationActivitiesSync(
this.options.currentTenant.id,
originalId,
)

await this.options.temporal.workflow.start('finishOrganizationMerging', {
taskQueue: 'entity-merging',
workflowId: `finishOrganizationMerging/${originalId}/${toMergeId}`,
Expand All @@ -178,23 +195,6 @@ export default class OrganizationService extends LoggerBase {
},
})

const searchSyncService = new SearchSyncService(this.options)

await searchSyncService.triggerOrganizationSync(this.options.currentTenant.id, originalId)
await searchSyncService.triggerRemoveOrganization(this.options.currentTenant.id, toMergeId)

// sync organization members
await searchSyncService.triggerOrganizationMembersSync(
this.options.currentTenant.id,
originalId,
)

// sync organization activities
await searchSyncService.triggerOrganizationActivitiesSync(
this.options.currentTenant.id,
originalId,
)

this.options.log.info({ originalId, toMergeId }, 'Organizations merged!')
return {
status: 200,
Expand Down

0 comments on commit 9555938

Please sign in to comment.