-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an internal queue manager library (#446)
* feat: create an internal queue manager lib * wip: tests * fix: basic unit tests including mocks * feat: use create a basic producer to send messages over to memphis cloud * fix: linter * fix: remove novu module from app * fix: tests * fix: linter --------- Co-authored-by: orig <[email protected]> Co-authored-by: orig <[email protected]>
- Loading branch information
1 parent
8b99fce
commit ebdbc04
Showing
24 changed files
with
366 additions
and
95 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
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,7 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
||
export const Ip = createParamDecorator((data: unknown, ctx: ExecutionContext) => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
const ip = (request.headers['x-forwarded-for'] as string) || request.socket.remoteAddress; | ||
return ip; | ||
}); |
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,12 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ProducerService } from '@reduced.to/queue-manager'; | ||
|
||
const SHORTENER_PRODUCER_NAME = 'shortener'; | ||
const SHORTENER_QUEUE_NAME = 'stats'; | ||
|
||
@Injectable() | ||
export class ShortenerProducer extends ProducerService { | ||
constructor() { | ||
super(SHORTENER_PRODUCER_NAME, SHORTENER_QUEUE_NAME); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
const nxPreset = require('@nx/jest/preset').default; | ||
|
||
module.exports = { ...nxPreset }; | ||
|
||
// Set the NODE_ENV to test | ||
process.env = Object.assign(process.env, { | ||
NODE_ENV: 'test', | ||
}); |
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,18 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
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,7 @@ | ||
# queue-manager | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
Run `nx test queue-manager` to execute the unit tests via [Jest](https://jestjs.io). |
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,11 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'queue-manager', | ||
preset: '../../jest.preset.js', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../coverage/libs/queue-manager', | ||
}; |
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,30 @@ | ||
{ | ||
"name": "queue-manager", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/queue-manager/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["libs/queue-manager/**/*.ts"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/queue-manager/jest.config.ts", | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
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,3 @@ | ||
export * from './lib/queue-manager.module'; | ||
export * from './lib/queue-manager.service'; | ||
export * from './lib/producer/producer.service'; |
19 changes: 19 additions & 0 deletions
19
libs/queue-manager/src/lib/__mocks__/queue-manager.service.ts
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,19 @@ | ||
export class MemphisMock { | ||
produce(payload: { stationName: string; producerName: string; message: any }) { | ||
console.log(`Producing message to ${payload.stationName}`); | ||
} | ||
} | ||
|
||
// Mock implementation for the queue manager service | ||
export class QueueManagerService { | ||
private readonly queueManager: MemphisMock; | ||
|
||
constructor() { | ||
this.queueManager = new MemphisMock(); | ||
} | ||
|
||
get client() { | ||
// Mock implementation for getting the queue manager | ||
return this.queueManager; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
libs/queue-manager/src/lib/producer/producer.service.spec.ts
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,76 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ProducerService } from './producer.service'; | ||
import { AppConfigModule, AppConfigService } from '@reduced.to/config'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { AppLoggerModule } from '@reduced.to/logger'; | ||
import { QueueManagerService } from '../queue-manager.service'; | ||
import { QueueManagerModule } from '../queue-manager.module'; | ||
|
||
jest.mock('../queue-manager.service'); | ||
|
||
describe('ProducerService', () => { | ||
const TEST_PRODUCER_NAME = 'test-producer'; | ||
const TEST_QUEUE_NAME = 'test-queue'; | ||
|
||
@Injectable() | ||
class TestProducerService extends ProducerService { | ||
constructor() { | ||
super(TEST_PRODUCER_NAME, TEST_QUEUE_NAME); | ||
} | ||
} | ||
|
||
let service: TestProducerService; | ||
let queueManager: QueueManagerService; | ||
let configService: AppConfigService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [AppConfigModule, AppLoggerModule, QueueManagerModule], | ||
providers: [TestProducerService, QueueManagerService], | ||
}).compile(); | ||
|
||
service = module.get<TestProducerService>(TestProducerService); | ||
queueManager = module.get<QueueManagerService>(QueueManagerService); | ||
configService = module.get<AppConfigService>(AppConfigService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
it('should get the producer name', () => { | ||
expect(service.name).toBe(TEST_PRODUCER_NAME); | ||
}); | ||
|
||
it('should get the queue name', () => { | ||
expect(service.queueName).toBe(TEST_QUEUE_NAME); | ||
}); | ||
|
||
it('should not publish a message to the queue if we are in test environment', async () => { | ||
const queueManagerSpy = jest.spyOn(queueManager.client, 'produce'); | ||
|
||
const PAYLOAD = { message: 'test', 1: 2 }; | ||
|
||
await service.publish(PAYLOAD); | ||
expect(queueManagerSpy).toBeCalledTimes(0); | ||
}); | ||
|
||
// It is not actually going to publish a message to the queue, but it is going to call the produce method of the queue-manager mock | ||
it('should publish a message to the queue if we are not in test environment', async () => { | ||
// Mock the config service to return the development environment | ||
const configMock = jest.spyOn(configService, 'getConfig'); | ||
configMock.mockReturnValue({ general: { env: 'development' }, memphis: { enable: true } } as any); | ||
|
||
const queueManagerSpy = jest.spyOn(queueManager.client, 'produce'); | ||
|
||
const PAYLOAD = { message: 'test', 1: 2 }; | ||
|
||
await service.publish(PAYLOAD); | ||
expect(queueManagerSpy).toBeCalledTimes(1); | ||
expect(queueManagerSpy).toBeCalledWith({ | ||
stationName: TEST_QUEUE_NAME, | ||
producerName: TEST_PRODUCER_NAME, | ||
message: PAYLOAD, | ||
}); | ||
}); | ||
}); |
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,34 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { QueueManagerService } from '../queue-manager.service'; | ||
import { AppConfigService } from '@reduced.to/config'; | ||
import { AppLoggerSerivce } from '@reduced.to/logger'; | ||
|
||
export abstract class ProducerService { | ||
@Inject(AppLoggerSerivce) private readonly logger: AppLoggerSerivce; | ||
@Inject(AppConfigService) private readonly config: AppConfigService; | ||
@Inject(QueueManagerService) private readonly queueManager: QueueManagerService; | ||
|
||
constructor(private readonly producerName: string, private readonly queue: string) {} | ||
|
||
get name() { | ||
return this.producerName; | ||
} | ||
|
||
get queueName() { | ||
return this.queue; | ||
} | ||
|
||
async publish(message: any) { | ||
// Do not publish if Memphis is disabled or if we are in test environment | ||
if (this.config.getConfig().general.env === 'test' || !this.config.getConfig().memphis.enable) { | ||
return; | ||
} | ||
|
||
this.logger.log(`Publishing message to ${this.queueName} with producer ${this.producerName}`); | ||
return this.queueManager.client.produce({ | ||
stationName: this.queue, | ||
producerName: this.name, | ||
message, | ||
}); | ||
} | ||
} |
Oops, something went wrong.