buffer.service.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // bufferService.ts
  2. import {
  3. BehaviorSubject,
  4. Observable,
  5. Subject,
  6. from,
  7. map,
  8. switchMap,
  9. } from "rxjs";
  10. import mongoose, { Connection, Model, Document } from "mongoose";
  11. import {
  12. ConnectionState,
  13. Message,
  14. MessageLog,
  15. } from "../interfaces/general.interface";
  16. export class BufferService {
  17. private messageStream: Subject<Message>;
  18. private connectionState: BehaviorSubject<ConnectionState>;
  19. private messageBuffer: Message[] = [];
  20. private messageModel: Model<Message> | undefined;
  21. private readonly dbUrl: string = process.env.MONGO as string;
  22. constructor(
  23. messageFromApp: Subject<Message>,
  24. connectionStateSubject: BehaviorSubject<ConnectionState>,
  25. dbName: string
  26. ) {
  27. this.messageStream = messageFromApp;
  28. this.connectionState = connectionStateSubject;
  29. this.setupSubscriptions(); // Note: The handle buffer will push the data in local array before pushing to mongo via initial check up model
  30. // this.initializeDatabaseConnection(dbName)
  31. // .then((connection: mongoose.Connection) => {
  32. // const grpcMessageSchema = require("../models/message.schema");
  33. // this.messageModel = connection.model<Message>(
  34. // "Message",
  35. // grpcMessageSchema
  36. // );
  37. // this.transferLocalBufferToMongoDB(); // transfer all data from local array into mongodb after the mongo setup is complete
  38. // })
  39. // .catch((error) => {
  40. // console.error("Database initialization failed:", error);
  41. // // Implement retry logic or additional error handling here. Perhaps retry logic in the future...
  42. // });
  43. }
  44. // to be exposed to acquire the messages
  45. public getMessages(): Observable<Message> {
  46. return this.messageStream as Observable<Message>;
  47. }
  48. private setupSubscriptions(): void {
  49. this.messageStream.subscribe({
  50. next: (message: Message) => this.handleIncomingMessage(message),
  51. error: (err) =>
  52. console.error("Error in messageToBePublished subject:", err),
  53. complete: () =>
  54. console.log("messageToBePublished subscription completed"),
  55. });
  56. this.connectionState.subscribe({
  57. next: (state: ConnectionState) => this.handleConnectionStateChanges(state),
  58. error: (err) => console.error("Error in connectionState subject:", err),
  59. complete: () => console.log("connectionState subscription completed"),
  60. });
  61. }
  62. private async initializeDatabaseConnection(
  63. dbName: string
  64. ): Promise<Connection> {
  65. try {
  66. console.log(`${this.dbUrl}${dbName}`);
  67. const connection: mongoose.Connection = await mongoose.createConnection(
  68. `${this.dbUrl}${dbName}`
  69. );
  70. console.log(`Connected to ${this.dbUrl}${dbName}`);
  71. return connection;
  72. } catch (error) {
  73. console.error("Error connecting to MongoDB:", error);
  74. throw error;
  75. }
  76. }
  77. private handleIncomingMessage(message: Message): void {
  78. if (this.connectionState.getValue().status === "BUFFER") {
  79. this.bufferMessage(message);
  80. }
  81. if (this.connectionState.getValue().status === "DIRECT_PUBLISH") {
  82. /* Note: Since the main outGoingMessage is being published irregardless
  83. of the connection state, so there's no need to do anything aside from
  84. releasing buffered messages which will be handled by handleConnectionStateChange */
  85. // additional logic here
  86. }
  87. }
  88. private handleConnectionStateChanges(state: ConnectionState): void {
  89. console.log(this.connectionState.getValue().status);
  90. if (state.status === "BUFFER") {
  91. if (state.payload && typeof state.payload !== "string") {
  92. this.bufferMessage(state.payload); // Buffer the last message immediately
  93. }
  94. }
  95. if (state.status === "DIRECT_PUBLISH") {
  96. // Relese the messages by inserting them into the outgoing Messages together.
  97. this.releaseBufferedMessages(this.messageStream);
  98. }
  99. }
  100. private async bufferMessage(message: Message): Promise<void> {
  101. if (this.messageModel) {
  102. try {
  103. // const newMessage = new this.messageModel(message);
  104. await this.messageModel.create(message);
  105. this.messageModel.countDocuments({}).then((count) => {
  106. console.log(`Message${(message.message as MessageLog).appData.msgId} saved to MongoDB buffer. There is ${count} messages in datatbase at the moment.`);
  107. });
  108. } catch (error) {
  109. console.error("Error saving message to MongoDB:", error);
  110. // Implement retry logic or additional error handling here
  111. }
  112. } else {
  113. this.messageBuffer.push(message); // Fallback to local buffer if model is not defined
  114. console.log(`pushing ${(message.message as MessageLog).appData.msgId} into local array buffer.... There is now ${this.messageBuffer.length} messages`);
  115. }
  116. }
  117. private releaseBufferedMessages(
  118. messageFromBuffer: Subject<Message>
  119. ): Promise<boolean> {
  120. return new Promise((resolve, reject) => {
  121. if (this.messageModel) {
  122. this.messageModel.countDocuments({}).then((count) => {
  123. console.log(`There is ${count} messages in database buffer at the moment. Releasing them....`);
  124. });
  125. const stream = this.messageModel.find().cursor();
  126. stream.on("data", async (message) => {
  127. // Process each message individually`
  128. messageFromBuffer.next(message);
  129. });
  130. stream.on("error", (error) => {
  131. console.error("Error streaming messages from MongoDB:", error);
  132. reject(error);
  133. });
  134. stream.on("end", async () => {
  135. // Delete the data once it has been streamed
  136. try {
  137. if (this.messageModel) {
  138. await this.messageModel.deleteMany({});
  139. console.log("Data in Mongo deleted successfully.");
  140. } else {
  141. console.log(`Message Mongoose Model is not intiated properly...`);
  142. }
  143. } catch (err) {
  144. console.error("Error deleting data:", err);
  145. }
  146. resolve(true);
  147. });
  148. }
  149. if (!this.messageModel) {
  150. // If MongoDB model is not defined, use the local buffer
  151. console.log(`Releasing buffer Message: currently there is ${this.messageBuffer.length} messages to be released`);
  152. this.messageBuffer.forEach((message) =>
  153. this.messageStream.next(message)
  154. );
  155. this.messageBuffer.length = 0; // Clear the local buffer after transferring
  156. if (this.messageBuffer.length < 1) {
  157. resolve(true);
  158. } else {
  159. reject(`Somehow the array is not emptied. This should not happen`);
  160. }
  161. }
  162. });
  163. }
  164. public getStateObservable(): BehaviorSubject<ConnectionState> {
  165. return this.connectionState;
  166. }
  167. private async transferLocalBufferToMongoDB(): Promise<void> {
  168. return new Promise((resolve, reject) => {
  169. console.log(`Transferring local array buffered Message: currently there is ${this.messageBuffer.length}. Transferring to database...`);
  170. if (this.messageModel) {
  171. let locallyBufferedMessage: Observable<Message> = from(this.messageBuffer);
  172. locallyBufferedMessage.subscribe({
  173. next: async (message: Message) => {
  174. try {
  175. if (this.messageModel) {
  176. await this.messageModel.create(message);
  177. console.log(`Transferring ${(message.message as MessageLog).appData.msgId} into database.`);
  178. }
  179. } catch (error) {
  180. console.error("Error transferring message to MongoDB:", error);
  181. }
  182. },
  183. error: (err) => console.error(err),
  184. complete: () => {
  185. if (this.messageModel) {
  186. this.messageModel.countDocuments({}).then((count) => {
  187. console.log(`Local buffered message transfer completed. There is a total of ${count} messages in database at the moment.`)
  188. this.messageBuffer = [] // Clear local buffer after transferring
  189. });
  190. }
  191. },
  192. });
  193. }
  194. });
  195. }
  196. // Additional methods as required...
  197. }