-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
50 changed files
with
1,763 additions
and
880 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -28,3 +28,4 @@ grafana | |
deploy_key | ||
.lh | ||
.vscode | ||
TODO.md |
This file contains 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 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 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 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,133 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
|
||
import { Processor, Queue, Worker } from 'bullmq' | ||
import { Scope } from 'src/authentication' | ||
import { logger } from '../logger' | ||
import { isTask, SavedBundle } from '../types' | ||
|
||
type MetricsJobPayload = | ||
| { | ||
action: 'sent-notification-for-review' | ||
payload: { | ||
bundle: SavedBundle | ||
userScopes: Scope[] | ||
} | ||
} | ||
| { | ||
action: 'waiting-external-validation' | ||
payload: { | ||
bundle: SavedBundle | ||
} | ||
} | ||
| { | ||
action: 'sent-for-approval' | ||
payload: { | ||
bundle: SavedBundle | ||
} | ||
} | ||
| { | ||
action: 'sent-notification' | ||
payload: { | ||
bundle: SavedBundle | ||
} | ||
} | ||
|
||
export function useMetricsQueue(redisHost: string) { | ||
async function addMetric<T extends MetricsJobPayload['action']>( | ||
action: T, | ||
payload: Extract<MetricsJobPayload, { action: T }>['payload'] | ||
) { | ||
const queue = new Queue<MetricsJobPayload>('metrics', { | ||
connection: { host: redisHost, port: 6379 } | ||
}) | ||
|
||
await queue.waitUntilReady() | ||
await queue.add('metrics-event', { payload, action } as any, { | ||
attempts: Number.MAX_SAFE_INTEGER, | ||
backoff: { | ||
type: 'exponential', | ||
delay: 1000 | ||
} | ||
}) | ||
await queue.close() | ||
} | ||
return addMetric | ||
} | ||
|
||
export async function registerMetricsWorker( | ||
redisHost: string, | ||
processJob: Processor<MetricsJobPayload> | ||
) { | ||
const worker = new Worker<MetricsJobPayload>('metrics', processJob, { | ||
connection: { host: redisHost, port: 6379 } | ||
}) | ||
worker.on('failed', (job, err) => { | ||
logger.error(`Metrics worker error: ${err}`) | ||
}) | ||
worker.on('error', (err) => { | ||
logger.error(`Metrics worker error: ${err}`) | ||
}) | ||
await worker.waitUntilReady() | ||
return worker | ||
} | ||
|
||
type SearchIndexingJobPayload = { | ||
bundle: SavedBundle | ||
} | ||
|
||
export function useSearchIndexingQueue(redisHost: string) { | ||
return async function indexRecord(bundle: SavedBundle) { | ||
const queue = new Queue<SearchIndexingJobPayload>('search-indexing', { | ||
connection: { host: redisHost, port: 6379 } | ||
}) | ||
|
||
await queue.waitUntilReady() | ||
|
||
console.log( | ||
'indexing record', | ||
bundle.entry | ||
.map((entry) => entry.resource) | ||
.filter(isTask) | ||
.map((t) => t.businessStatus.coding) | ||
) | ||
|
||
await queue.add( | ||
'index-record', | ||
{ bundle }, | ||
{ | ||
attempts: Number.MAX_SAFE_INTEGER, | ||
backoff: { | ||
type: 'exponential', | ||
delay: 1000 | ||
} | ||
} | ||
) | ||
await queue.close() | ||
} | ||
} | ||
|
||
export async function registerSearchIndexingWorker( | ||
redisHost: string, | ||
processJob: Processor<SearchIndexingJobPayload> | ||
) { | ||
const worker = new Worker<SearchIndexingJobPayload>( | ||
'search-indexing', | ||
processJob, | ||
{ | ||
connection: { host: redisHost, port: 6379 } | ||
} | ||
) | ||
|
||
await worker.waitUntilReady() | ||
|
||
return worker | ||
} |
This file contains 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 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,50 @@ | ||
import { logger } from '../logger' | ||
|
||
export type RunFunction = (sideEffect: SideEffect<any>) => Promise<any> | ||
type Saga<R> = (run: <T>(effect: SideEffect<T>) => Promise<T>) => Promise<R> | ||
|
||
export async function saga<R>(callback: Saga<R>): Promise<R> { | ||
const rollbacks: (() => Promise<any>)[] = [] | ||
async function run(sideEffect: SideEffect<any>) { | ||
try { | ||
logger.info(`Running side effect: ${sideEffect.label}`) | ||
const result = await sideEffect.commit() | ||
rollbacks.unshift(async () => { | ||
logger.info(`Rolling back side effect: ${sideEffect.label}`) | ||
|
||
try { | ||
await sideEffect.rollback(result) | ||
} catch (e) { | ||
logger.error( | ||
`Error during rollback: ${sideEffect.label}. Error: ${e.message}` | ||
) | ||
} | ||
}) | ||
return result | ||
} catch (e) { | ||
for (const rollback of rollbacks) { | ||
await rollback() | ||
} | ||
throw e | ||
} | ||
} | ||
return callback(run) | ||
} | ||
|
||
type SideEffect<T> = { | ||
commit: () => Promise<T> | ||
rollback: (param: T) => Promise<any> | ||
label?: string | ||
} | ||
|
||
export function effect<T>( | ||
label: string, | ||
commit: () => Promise<T>, | ||
rollback: (param: T) => Promise<any> | ||
): SideEffect<T> { | ||
return { | ||
commit, | ||
rollback, | ||
label | ||
} | ||
} |
This file contains 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,125 @@ | ||
import { saga, effect } from '.' | ||
|
||
test('executes invocations in order', async () => { | ||
const result = await saga<number>(async (run) => { | ||
const a = await run( | ||
effect( | ||
'', | ||
() => Promise.resolve(1), | ||
() => Promise.resolve() | ||
) | ||
) | ||
const b = await run( | ||
effect( | ||
'', | ||
() => Promise.resolve(a + 1), | ||
() => Promise.resolve() | ||
) | ||
) | ||
const c = await run( | ||
effect( | ||
'', | ||
() => Promise.resolve(b + 1), | ||
() => Promise.resolve() | ||
) | ||
) | ||
return c | ||
}) | ||
expect(result).toBe(3) | ||
}) | ||
|
||
test('executes compensates in order if anything fails', async () => { | ||
const compensateSpy = jest.fn() | ||
|
||
const result = saga<number>(async (run) => { | ||
const a = await run( | ||
effect( | ||
'', | ||
() => Promise.resolve(1), | ||
() => compensateSpy('compensate 1') | ||
) | ||
) | ||
await run( | ||
effect( | ||
'', | ||
() => Promise.resolve(a + 1), | ||
() => compensateSpy('compensate 2') | ||
) | ||
) | ||
const c = await run( | ||
effect( | ||
'', | ||
() => Promise.reject(new Error('error')), | ||
() => compensateSpy('compensate 3') | ||
) | ||
) | ||
|
||
return c | ||
}) | ||
|
||
await expect(result).rejects.toThrowError('error') | ||
expect(compensateSpy).toHaveBeenCalledTimes(2) | ||
expect(compensateSpy).toHaveBeenCalledWith('compensate 2') | ||
expect(compensateSpy).toHaveBeenCalledWith('compensate 1') | ||
}) | ||
|
||
test('executes all compensates even if one of them fails', async () => { | ||
const compensateSpy = jest.fn() | ||
|
||
const result = saga<number>(async (run) => { | ||
const a = await run( | ||
effect( | ||
'', | ||
() => Promise.resolve(1), | ||
() => compensateSpy('compensate 1') | ||
) | ||
) | ||
await run( | ||
effect( | ||
'', | ||
() => Promise.resolve(a + 1), | ||
() => Promise.reject(new Error('error')) | ||
) | ||
) | ||
const c = await run( | ||
effect( | ||
'', | ||
() => Promise.reject(new Error('error')), | ||
() => compensateSpy('compensate 3') | ||
) | ||
) | ||
|
||
return c | ||
}) | ||
|
||
await expect(result).rejects.toThrowError('error') | ||
expect(compensateSpy).toHaveBeenCalledTimes(1) | ||
expect(compensateSpy).toHaveBeenCalledWith('compensate 1') | ||
}) | ||
test('compensate handler receives the invoke return value as a parameter', async () => { | ||
const compensateSpy = jest.fn() | ||
const shouldNotBeCalled = jest.fn(() => { | ||
throw new Error('This function should not have been called') | ||
}) | ||
const result = saga<number>(async (run) => { | ||
const a = await run( | ||
effect( | ||
'', | ||
() => Promise.resolve(1), | ||
() => compensateSpy('compensate 1') | ||
) | ||
) | ||
await run(effect('', () => Promise.resolve(a + 1), compensateSpy)) | ||
|
||
const c = await run( | ||
effect('', () => Promise.reject(new Error('error')), shouldNotBeCalled) | ||
) | ||
|
||
return c | ||
}) | ||
|
||
await expect(result).rejects.toThrowError('error') | ||
expect(compensateSpy).toHaveBeenCalledTimes(2) | ||
expect(compensateSpy).toHaveBeenCalledWith(2) | ||
expect(compensateSpy).toHaveBeenCalledWith('compensate 1') | ||
}) |
This file contains 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.