-
Notifications
You must be signed in to change notification settings - Fork 61
feat(Statement): Add alternative publish to queue logic without using Redis (LLC-2162) #890
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
Show all changes
12 commits
Select commit
Hold shift + click to select a range
db4ecd4
build(Statement): Add aws-sqs package
PrinceWaune 31ce5d1
feat(Statement): Add new SQS types
PrinceWaune 3e2fc36
feat(Statement): Add new SQS configuration
PrinceWaune 9f1b001
feat(Statement): Add new SQS factory
PrinceWaune db24929
feat(Statement): Create SQS client
PrinceWaune 06674af
feat(Statement): Publish to SQS instead of Redis
PrinceWaune c22fd28
feat(Statement): Use existing aws global vars
PrinceWaune b062ea5
feat(Statement): Add EVENTS_REPO variable
PrinceWaune 0abd52e
feat(Statement): Remove logs
PrinceWaune fa47c42
feat(Statement): Add queue namespace var to the .env
PrinceWaune f66383b
Merge branch 'master' into LLC-2162
PrinceWaune d80c705
Merge branch 'master' into LLC-2162
PrinceWaune 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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import RedisFactoryConfig from './utils/redisEvents/FactoryConfig'; | ||
| import SQSFactoryConfig from './utils/sqsEvents/FactoryConfig'; | ||
|
|
||
| export default interface FactoryConfig { | ||
| readonly facade?: string; | ||
| readonly redis?: RedisFactoryConfig; | ||
| readonly sqs?: SQSFactoryConfig; | ||
| } |
70 changes: 70 additions & 0 deletions
70
src/apps/statements/repo/eventsRepo/emitNewStatements/sqs.ts
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,70 @@ | ||
| import { | ||
| GetQueueUrlCommand, | ||
| SQSClient, | ||
| SendMessageBatchCommand, | ||
| SendMessageBatchRequestEntry, | ||
| } from '@aws-sdk/client-sqs'; | ||
| import { v4 } from 'uuid'; | ||
| import { getPrefixWithProcessingPriority } from '../utils/getPrefixWithProcessingPriority'; | ||
| import { StatementProcessingPriority } from '../../../enums/statementProcessingPriority.enum'; | ||
| import FacadeConfig from '../utils/sqsEvents/FacadeConfig'; | ||
| import { STATEMENT_QUEUE } from '../utils/constants'; | ||
| import Signature from './Signature'; | ||
|
|
||
| const MAX_BATCH_SIZE = 10; | ||
|
|
||
| let queueUrl: string | undefined; | ||
|
|
||
| const publishMessages = async (sqsClient: SQSClient, statementProperties: string[]) => { | ||
| const statementPropertiesBatchRequest = statementProperties.map( | ||
| (statementProperty): SendMessageBatchRequestEntry => ({ | ||
| Id: v4(), | ||
| MessageBody: statementProperty, | ||
| }), | ||
| ); | ||
|
|
||
| for (let index = 0; index < statementPropertiesBatchRequest.length; index += MAX_BATCH_SIZE) { | ||
| await sqsClient.send( | ||
| new SendMessageBatchCommand({ | ||
| QueueUrl: queueUrl, | ||
| Entries: statementPropertiesBatchRequest.slice(index, index + MAX_BATCH_SIZE), | ||
| }), | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| const getQueueUrl = async ( | ||
| sqsClient: SQSClient, | ||
| prefix: string, | ||
| priority: StatementProcessingPriority, | ||
| isQueuePriorityEnabled: boolean, | ||
| ) => { | ||
| if (queueUrl) { | ||
| return queueUrl; | ||
| } | ||
|
|
||
| const prefixWithPriority = getPrefixWithProcessingPriority( | ||
| prefix, | ||
| priority, | ||
| isQueuePriorityEnabled, | ||
| ); | ||
|
|
||
| const getQueueUrlCommand = new GetQueueUrlCommand({ | ||
| QueueName: `${prefixWithPriority}_${STATEMENT_QUEUE}`, | ||
| }); | ||
|
|
||
| const commandResult = await sqsClient.send(getQueueUrlCommand); | ||
|
|
||
| queueUrl = commandResult.QueueUrl; | ||
|
|
||
| return queueUrl; | ||
| }; | ||
|
|
||
| export default (config: FacadeConfig): Signature => { | ||
| return async ({ statementProperties, priority }) => { | ||
| const sqsClient = await config.client(); | ||
|
|
||
| await getQueueUrl(sqsClient, config.prefix, priority, config.isQueuePriorityEnabled); | ||
| await publishMessages(sqsClient, statementProperties); | ||
| }; | ||
| }; | ||
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 |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| import Facade from './Facade'; | ||
| import FactoryConfig from './FactoryConfig'; | ||
| import redisFactory from './utils/redisEvents/factory'; | ||
| import sqsFactory from './utils/sqsEvents/factory'; | ||
|
|
||
| export default (config: FactoryConfig): Facade => { | ||
| switch (config.facade) { | ||
| default: | ||
| case 'redis': | ||
| return redisFactory(config.redis); | ||
| case 'sqs': | ||
| return sqsFactory(config.sqs); | ||
| } | ||
| }; |
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export const EVENT_NAME = 'statement.new'; | ||
| export const CHANNEL_NAME = 'statement.notify'; | ||
| export const STATEMENT_QUEUE = 'STATEMENT_QUEUE'; |
7 changes: 7 additions & 0 deletions
7
src/apps/statements/repo/eventsRepo/utils/sqsEvents/FacadeConfig.ts
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,7 @@ | ||
| import { SQSClient } from '@aws-sdk/client-sqs'; | ||
|
|
||
| export default interface FacadeConfig { | ||
| readonly client: () => Promise<SQSClient>; | ||
| readonly prefix: string; | ||
| readonly isQueuePriorityEnabled: boolean; | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/apps/statements/repo/eventsRepo/utils/sqsEvents/FactoryConfig.ts
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,7 @@ | ||
| import { SQSClient } from '@aws-sdk/client-sqs'; | ||
|
|
||
| export default interface FactoryConfig { | ||
| readonly client?: () => Promise<SQSClient>; | ||
| readonly prefix?: string; | ||
| readonly isQueuePriorityEnabled?: boolean; | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/apps/statements/repo/eventsRepo/utils/sqsEvents/factory.ts
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,26 @@ | ||
| import { defaultTo } from 'lodash'; | ||
| import emitNewStatements from '../../emitNewStatements/sqs'; | ||
| import Facade from '../../Facade'; | ||
| import connectToSQS from "../../../../../../utils/connectToSQS"; | ||
| import FacadeConfig from './FacadeConfig'; | ||
| import FactoryConfig from './FactoryConfig'; | ||
|
|
||
| export default (factoryConfig: FactoryConfig = {}): Facade => { | ||
| const facadeConfig: FacadeConfig = { | ||
| client: defaultTo(factoryConfig.client, connectToSQS()), | ||
| prefix: defaultTo(factoryConfig.prefix, 'xapistatements'), | ||
| isQueuePriorityEnabled: defaultTo(factoryConfig.isQueuePriorityEnabled, false), | ||
| }; | ||
| return { | ||
| emitNewStatements: emitNewStatements(facadeConfig), | ||
| clearRepo: async () => { | ||
| // Do nothing. | ||
| }, | ||
| migrate: async () => { | ||
| // Do nothing. | ||
| }, | ||
| rollback: async () => { | ||
| // Do nothing. | ||
| }, | ||
| }; | ||
| }; |
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
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,22 @@ | ||
| import { SQSClient } from '@aws-sdk/client-sqs'; | ||
| import { once } from 'lodash'; | ||
| import config from '../config'; | ||
| import logger from '../logger'; | ||
|
|
||
| export default once((): (() => Promise<SQSClient>) => { | ||
| return once(async () => { | ||
|
Comment on lines
+6
to
+7
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PrinceWaune , double |
||
| logger.info('Creating SQS connection'); | ||
|
|
||
| return new SQSClient({ | ||
| ...(config.aws.region ? { region: config.aws.region } : null), | ||
| ...(config.aws.accessKeyId && config.aws.secretAccessKey | ||
| ? { | ||
| credentials: { | ||
| accessKeyId: config.aws.accessKeyId as string, | ||
| secretAccessKey: config.aws.secretAccessKey as string, | ||
| }, | ||
| } | ||
| : null), | ||
| }); | ||
| }); | ||
| }); | ||
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.