|
| 1 | +import { WorkflowIdConflictPolicy, WorkflowIdReusePolicy } from '@temporalio/workflow' |
| 2 | + |
1 | 3 | import { DEFAULT_TENANT_ID } from '@crowd/common' |
2 | 4 | import { moveActivityRelationsToAnotherOrganization } from '@crowd/data-access-layer/src/activityRelations' |
3 | 5 | import { |
@@ -35,31 +37,55 @@ export async function finishOrganizationMergingUpdateActivities( |
35 | 37 | await moveActivityRelationsToAnotherOrganization(qx, secondaryId, primaryId) |
36 | 38 | } |
37 | 39 |
|
38 | | -export async function recalculateActivityAffiliationsOfOrganizationSynchronous( |
| 40 | +export async function recalculateActivityAffiliationsOfOrganizationAsync( |
39 | 41 | organizationId: string, |
40 | 42 | ): Promise<void> { |
41 | | - await svc.temporal.workflow.start('organizationUpdate', { |
42 | | - taskQueue: 'profiles', |
43 | | - workflowId: `${TemporalWorkflowId.ORGANIZATION_UPDATE}/${organizationId}`, |
44 | | - followRuns: true, |
45 | | - retry: { |
46 | | - maximumAttempts: 10, |
47 | | - }, |
48 | | - args: [ |
49 | | - { |
50 | | - organization: { |
51 | | - id: organizationId, |
52 | | - }, |
53 | | - recalculateAffiliations: true, |
54 | | - syncOptions: { |
55 | | - doSync: false, |
56 | | - withAggs: false, |
57 | | - }, |
| 43 | + const workflowId = `${TemporalWorkflowId.ORGANIZATION_UPDATE}/${organizationId}` |
| 44 | + |
| 45 | + try { |
| 46 | + const handle = svc.temporal.workflow.getHandle(workflowId) |
| 47 | + const { status } = await handle.describe() |
| 48 | + |
| 49 | + if (status.name === 'RUNNING') { |
| 50 | + await handle.result() |
| 51 | + } |
| 52 | + } catch (err) { |
| 53 | + if (err.name !== 'WorkflowNotFoundError') { |
| 54 | + svc.log.error({ err }, 'Failed to check workflow state') |
| 55 | + throw err |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + await svc.temporal.workflow.start('organizationUpdate', { |
| 61 | + taskQueue: 'profiles', |
| 62 | + workflowId, |
| 63 | + workflowIdReusePolicy: WorkflowIdReusePolicy.ALLOW_DUPLICATE, |
| 64 | + workflowIdConflictPolicy: WorkflowIdConflictPolicy.FAIL, |
| 65 | + retry: { |
| 66 | + maximumAttempts: 10, |
58 | 67 | }, |
59 | | - ], |
60 | | - }) |
| 68 | + args: [ |
| 69 | + { |
| 70 | + organization: { |
| 71 | + id: organizationId, |
| 72 | + }, |
| 73 | + recalculateAffiliations: true, |
| 74 | + syncOptions: { |
| 75 | + doSync: false, |
| 76 | + withAggs: false, |
| 77 | + }, |
| 78 | + }, |
| 79 | + ], |
| 80 | + }) |
| 81 | + } catch (err) { |
| 82 | + if (err.name === 'WorkflowExecutionAlreadyStartedError') { |
| 83 | + svc.log.info({ workflowId }, 'Workflow already started, skipping') |
| 84 | + return |
| 85 | + } |
61 | 86 |
|
62 | | - await svc.temporal.workflow.result(`${TemporalWorkflowId.ORGANIZATION_UPDATE}/${organizationId}`) |
| 87 | + throw err |
| 88 | + } |
63 | 89 | } |
64 | 90 |
|
65 | 91 | export async function syncOrganization(organizationId: string, syncStart: Date): Promise<void> { |
|
0 commit comments