-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use create a basic producer to send messages over to memphis cloud
- Loading branch information
orig
committed
Sep 22, 2023
1 parent
129496f
commit 27a3564
Showing
12 changed files
with
106 additions
and
38 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
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
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
34 changes: 19 additions & 15 deletions
34
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
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,21 +1,34 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { QUEUE_MANAGER_INJECTION_TOKEN, QueueManagerService } from '@reduced.to/queue-manager'; | ||
import { Memphis } from 'memphis-dev/*'; | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { QueueManagerService } from '@reduced.to/queue-manager'; | ||
import { AppConfigService } from '@reduced.to/config'; | ||
import { AppLoggerSerivce } from '@reduced.to/logger'; | ||
|
||
export abstract class ProducerService extends QueueManagerService { | ||
constructor(@Inject(QUEUE_MANAGER_INJECTION_TOKEN) queueManager: Memphis, private readonly name: string) { | ||
super(queueManager); | ||
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; | ||
} | ||
|
||
getName() { | ||
return this.name; | ||
get queueName() { | ||
return this.queue; | ||
} | ||
|
||
async publish(queueName: string, data: any) { | ||
return this.getQueueManager().produce({ | ||
stationName: queueName, | ||
async publish(message: any) { | ||
// Do not publish if Memphis is disabled | ||
if (!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: data, | ||
message, | ||
}); | ||
} | ||
} |
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,12 +1,12 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { QUEUE_MANAGER_INJECTION_TOKEN } from '@reduced.to/queue-manager'; | ||
import { Memphis } from 'memphis-dev/*'; | ||
import { Memphis } from 'memphis-dev'; | ||
|
||
@Injectable() | ||
export class QueueManagerService { | ||
constructor(@Inject(QUEUE_MANAGER_INJECTION_TOKEN) private readonly queueManager: Memphis) {} | ||
|
||
getQueueManager() { | ||
get client() { | ||
return this.queueManager; | ||
} | ||
} |