|
@@ -6,10 +6,7 @@ import { resolve } from 'path';
|
|
|
|
|
|
export class BufferService {
|
|
|
private messageStream: Subject<Message>
|
|
|
- // private messageFromApplication: Subject<Message>;
|
|
|
- // private messageFromBuffer: Subject<Message>
|
|
|
private connectionState: BehaviorSubject<ConnectionState>
|
|
|
- // private currentSource: Subject<Message>
|
|
|
private messageBuffer: Message[] = [];
|
|
|
private messageModel: Model<Message> | undefined;
|
|
|
private readonly dbUrl: string = process.env.MONGO as string;
|
|
@@ -21,21 +18,18 @@ export class BufferService {
|
|
|
dbName: string
|
|
|
) {
|
|
|
this.messageStream = messageFromApp;
|
|
|
- // this.messageFromBuffer = new Subject<Message>();
|
|
|
- // this.messageFromApplication = messageFromApp;
|
|
|
- // this.currentSource = this.messageFromBuffer
|
|
|
this.connectionState = connectionStateSubject;
|
|
|
this.setupSubscriptions(); // Note: The handle buffer will push the data in local array before pushing to mongo via initial check up model
|
|
|
|
|
|
/* Disable for now. Use local array first */
|
|
|
- // this.initializeDatabaseConnection(dbName).then((connection: mongoose.Connection) => {
|
|
|
- // const grpcMessageSchema = require('../models/message.schema');
|
|
|
- // this.messageModel = connection.model<Message>('Message', grpcMessageSchema)
|
|
|
- // this.transferLocalBufferToMongoDB() // transfer all data from local array into mongodb after the mongo setup is complete
|
|
|
- // }).catch(error => {
|
|
|
- // console.error('Database initialization failed:', error);
|
|
|
- // // Implement retry logic or additional error handling here
|
|
|
- // });
|
|
|
+ this.initializeDatabaseConnection(dbName).then((connection: mongoose.Connection) => {
|
|
|
+ const grpcMessageSchema = require('../models/message.schema');
|
|
|
+ this.messageModel = connection.model<Message>('Message', grpcMessageSchema)
|
|
|
+ this.transferLocalBufferToMongoDB() // transfer all data from local array into mongodb after the mongo setup is complete
|
|
|
+ }).catch(error => {
|
|
|
+ console.error('Database initialization failed:', error);
|
|
|
+ // Implement retry logic or additional error handling here
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public getMessages(): Observable<Message> {
|
|
@@ -140,6 +134,10 @@ export class BufferService {
|
|
|
private releaseBufferedMessages(messageFromBuffer: Subject<Message>): Promise<boolean> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
if (this.messageModel) {
|
|
|
+ this.messageModel.countDocuments({}).then((count) => {
|
|
|
+ console.log(`There is ${count} messages in datatbase buffer at the moment. Releasing them....`);
|
|
|
+
|
|
|
+ })
|
|
|
const stream = this.messageModel.find().cursor();
|
|
|
|
|
|
stream.on('data', async (message) => {
|
|
@@ -186,18 +184,21 @@ export class BufferService {
|
|
|
}
|
|
|
|
|
|
private async transferLocalBufferToMongoDB(): Promise<void> {
|
|
|
- if (this.messageModel) {
|
|
|
- this.messageBuffer.forEach(async message => {
|
|
|
- try {
|
|
|
- if (this.messageModel) {
|
|
|
- await this.messageModel.create(message);
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ console.log(`Releasing buffer Message: currently there is ${this.messageBuffer.length}. Transferring to database...`)
|
|
|
+ if (this.messageModel) {
|
|
|
+ this.messageBuffer.forEach(async message => {
|
|
|
+ try {
|
|
|
+ if (this.messageModel) {
|
|
|
+ await this.messageModel.create(message);
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error transferring message to MongoDB:', error);
|
|
|
}
|
|
|
- } catch (error) {
|
|
|
- console.error('Error transferring message to MongoDB:', error);
|
|
|
- }
|
|
|
- })
|
|
|
- this.messageBuffer = []; // Clear local buffer after transferring
|
|
|
- }
|
|
|
+ })
|
|
|
+ this.messageBuffer = []; // Clear local buffer after transferring
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
// Additional methods as required...
|