-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next' of https://github.com/novuhq/novu into next
- Loading branch information
Showing
6 changed files
with
159 additions
and
34 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,35 @@ | ||
import { IoAdapter } from '@nestjs/platform-socket.io'; | ||
import { ServerOptions } from 'socket.io'; | ||
import { createAdapter } from '@socket.io/redis-adapter'; | ||
import { getRedisPrefix } from '@novu/shared'; | ||
import { WebSocketsInMemoryProviderService } from '@novu/application-generic'; | ||
|
||
export class InMemoryIoAdapter extends IoAdapter { | ||
private webSocketsInMemoryProviderService: WebSocketsInMemoryProviderService; | ||
private adapterConstructor: ReturnType<typeof createAdapter>; | ||
|
||
async connectToInMemoryCluster(): Promise<void> { | ||
// TODO: Pending to inject in the provider instantiation | ||
const keyPrefix = getRedisPrefix() ? `socket.io#${getRedisPrefix()}` : 'socket.io'; | ||
|
||
this.webSocketsInMemoryProviderService = new WebSocketsInMemoryProviderService(); | ||
|
||
await this.webSocketsInMemoryProviderService.initialize(); | ||
|
||
const pubClient = this.webSocketsInMemoryProviderService.getClient(); | ||
const subClient = pubClient?.duplicate(); | ||
|
||
/* | ||
* TODO: Might not be needed to connect as we are checking it is initialized already. | ||
* await Promise.all([pubClient?.connect(), subClient?.connect()]); | ||
*/ | ||
this.adapterConstructor = createAdapter(pubClient, subClient); | ||
} | ||
|
||
createIOServer(port: number, options?: ServerOptions): any { | ||
const server = super.createIOServer(port, options); | ||
server.adapter(this.adapterConstructor); | ||
|
||
return server; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
1 change: 1 addition & 0 deletions
1
packages/application-generic/src/services/in-memory-provider/index.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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './cache-in-memory-provider.service'; | ||
export * from './in-memory-provider.service'; | ||
export * from './web-sockets-in-memory-provider.service'; | ||
export * from './workflow-in-memory-provider.service'; | ||
export * from './types'; |
101 changes: 101 additions & 0 deletions
101
...ication-generic/src/services/in-memory-provider/web-sockets-in-memory-provider.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,101 @@ | ||
import { Logger } from '@nestjs/common'; | ||
|
||
import { InMemoryProviderService } from './in-memory-provider.service'; | ||
import { | ||
InMemoryProviderEnum, | ||
InMemoryProviderClient, | ||
ScanStream, | ||
} from './types'; | ||
|
||
import { GetIsInMemoryClusterModeEnabled } from '../../usecases'; | ||
|
||
const LOG_CONTEXT = 'WebSocketsInMemoryProviderService'; | ||
|
||
export class WebSocketsInMemoryProviderService { | ||
public inMemoryProviderService: InMemoryProviderService; | ||
public isCluster: boolean; | ||
private getIsInMemoryClusterModeEnabled: GetIsInMemoryClusterModeEnabled; | ||
|
||
constructor() { | ||
this.getIsInMemoryClusterModeEnabled = | ||
new GetIsInMemoryClusterModeEnabled(); | ||
|
||
const provider = this.selectProvider(); | ||
this.isCluster = this.isClusterMode(); | ||
|
||
this.inMemoryProviderService = new InMemoryProviderService( | ||
provider, | ||
this.isCluster | ||
); | ||
} | ||
|
||
/** | ||
* Rules for the provider selection: | ||
* - For our self hosted users we assume all of them have a single node Redis | ||
* instance. | ||
* - For Novu we will use Elasticache. We fallback to a Redis Cluster configuration | ||
* if Elasticache not configured properly. That's happening in the provider | ||
* mapping in the /in-memory-provider/providers/index.ts | ||
*/ | ||
private selectProvider(): InMemoryProviderEnum { | ||
if (process.env.IS_DOCKER_HOSTED) { | ||
return InMemoryProviderEnum.REDIS; | ||
} | ||
|
||
return InMemoryProviderEnum.ELASTICACHE; | ||
} | ||
|
||
private descriptiveLogMessage(message) { | ||
return `[Provider: ${this.selectProvider()}] ${message}`; | ||
} | ||
|
||
private isClusterMode(): boolean { | ||
const isClusterModeEnabled = this.getIsInMemoryClusterModeEnabled.execute(); | ||
|
||
Logger.log( | ||
this.descriptiveLogMessage( | ||
`Cluster mode ${ | ||
isClusterModeEnabled ? 'IS' : 'IS NOT' | ||
} enabled for ${LOG_CONTEXT}` | ||
), | ||
LOG_CONTEXT | ||
); | ||
|
||
return isClusterModeEnabled; | ||
} | ||
|
||
public async initialize(): Promise<void> { | ||
await this.inMemoryProviderService.delayUntilReadiness(); | ||
} | ||
|
||
public getClient(): InMemoryProviderClient { | ||
return this.inMemoryProviderService.inMemoryProviderClient; | ||
} | ||
|
||
public getClientStatus(): string { | ||
return this.getClient().status; | ||
} | ||
|
||
public getTtl(): number { | ||
return this.inMemoryProviderService.inMemoryProviderConfig.ttl; | ||
} | ||
|
||
public inMemoryScan(pattern: string): ScanStream { | ||
return this.inMemoryProviderService.inMemoryScan(pattern); | ||
} | ||
|
||
public isReady(): boolean { | ||
return this.inMemoryProviderService.isClientReady(); | ||
} | ||
|
||
public providerInUseIsInClusterMode(): boolean { | ||
const providerConfigured = | ||
this.inMemoryProviderService.getProvider.configured; | ||
|
||
return this.isCluster || providerConfigured !== InMemoryProviderEnum.REDIS; | ||
} | ||
|
||
public async shutdown(): Promise<void> { | ||
await this.inMemoryProviderService.shutdown(); | ||
} | ||
} |