|
@@ -2,13 +2,13 @@ import * as _ from 'lodash'
|
|
|
import * as fs from 'fs'
|
|
|
import mongoose, { Model, Schema } from 'mongoose';
|
|
|
import { Observable, Subject, Subscription, from } from 'rxjs'
|
|
|
-import { ColorCode, MessageLog, ReportStatus } from '../interfaces/general.interface'
|
|
|
+import { ColorCode, ReportStatus } from '../interfaces/general.interface'
|
|
|
require('dotenv').config();
|
|
|
|
|
|
// Implement status chain refactoring
|
|
|
-export class FisErrorHandlingService {
|
|
|
+export class FisRetransmissionService {
|
|
|
private mongoUrl: string = process.env.MONGO + 'emergencyStorage'
|
|
|
- private bufferedStorage: MessageLog[] = []
|
|
|
+ private bufferedStorage: any[] = []
|
|
|
private mongoConnection: any
|
|
|
private messageModel: any
|
|
|
private maximumBufferLength: number = parseInt(process.env.MaxBufferLoad as string) // right now just put as 15
|
|
@@ -19,8 +19,8 @@ export class FisErrorHandlingService {
|
|
|
}
|
|
|
|
|
|
// Main function that intercepts outgoing messages by communicating || intepreting report status from grpc connection as indicator
|
|
|
- public handleMessage(messageToBePublished: Subject<MessageLog>, statusReport: Subject<ReportStatus>): Subject<MessageLog> {
|
|
|
- let releaseMessageSubject: Subject<MessageLog> = new Subject() // A return value
|
|
|
+ public handleMessage(messageToBePublished: Subject<any>, statusReport: Subject<ReportStatus>): Subject<any> {
|
|
|
+ let releaseMessageSubject: Subject<any> = new Subject() // A return value
|
|
|
// Using the concept of toggling to improve the eficacy of subscription control && data flow
|
|
|
let messageReleaseSubscription: Subscription | null = null
|
|
|
let messageBufferSubscription: Subscription | null = null
|
|
@@ -44,7 +44,7 @@ export class FisErrorHandlingService {
|
|
|
if (!messageReleaseSubscription) status = -1
|
|
|
}
|
|
|
if (status === 1) {
|
|
|
- this.releaseMessageFromLocalBuffer(this.bufferedStorage).then((resObs: Observable<MessageLog>) => {
|
|
|
+ this.releaseMessageFromLocalBuffer(this.bufferedStorage).then((resObs: Observable<any>) => {
|
|
|
resObs.subscribe({
|
|
|
next: message => releaseMessageSubject.next(message),
|
|
|
error: err => console.error(err),
|
|
@@ -59,7 +59,7 @@ export class FisErrorHandlingService {
|
|
|
})
|
|
|
}
|
|
|
if (status === 1) {
|
|
|
- this.releaseMessageFromMongoStorage().then((resObs: Subject<MessageLog>) => {
|
|
|
+ this.releaseMessageFromMongoStorage().then((resObs: Subject<any>) => {
|
|
|
resObs.subscribe({
|
|
|
next: message => releaseMessageSubject.next(message),
|
|
|
error: err => console.error(err),
|
|
@@ -77,7 +77,7 @@ export class FisErrorHandlingService {
|
|
|
}
|
|
|
if (report.code == ColorCode.YELLOW) {
|
|
|
if (report.payload) {
|
|
|
- console.log(`Rebuffering ${report.payload.appData?.msgId} into buffer...`)
|
|
|
+ console.log(`Rebuffering ${report.payload.message?.appData?.msgId} into buffer...`)
|
|
|
this.bufferedStorage.push(report.payload)
|
|
|
}
|
|
|
console.log(`Connection status report && ${report.message ?? 'No Message'}`)
|
|
@@ -109,7 +109,7 @@ export class FisErrorHandlingService {
|
|
|
if (messageBufferSubscription) status = -1
|
|
|
}
|
|
|
if (status === 1) {
|
|
|
- this.transferBufferedMessagseToMongoStorage(this.bufferedStorage, messageBufferSubscription).then((res: MessageLog[]) => {
|
|
|
+ this.transferBufferedMessageToMongoStorage(this.bufferedStorage, messageBufferSubscription).then((res: any[]) => {
|
|
|
if (res.length !== this.bufferedStorage.length || this.bufferedStorage.length > 0) status = -1 // this promise function should return an empty array
|
|
|
})
|
|
|
}
|
|
@@ -132,10 +132,10 @@ export class FisErrorHandlingService {
|
|
|
console.log(`Buffer length exceeds limit imposed!!!`)
|
|
|
let report: ReportStatus = {
|
|
|
code: ColorCode.RED,
|
|
|
- message: `Buffer is exceeding limit. Initiate storage transfer to designated database. `
|
|
|
+ message: `Buffer is exceeding limit. Initiate storage transfer to designated database.`,
|
|
|
+ from: `Error Handling Service`
|
|
|
}
|
|
|
statusReport.next(report)
|
|
|
-
|
|
|
}
|
|
|
})
|
|
|
}
|
|
@@ -144,8 +144,8 @@ export class FisErrorHandlingService {
|
|
|
private activateReleaseSubscription(messageReleaseSubscription, messageToBePublished, releaseMessageSubject): Subscription | null {
|
|
|
if (!messageReleaseSubscription) {
|
|
|
messageReleaseSubscription = messageToBePublished.subscribe({
|
|
|
- next: (message: MessageLog) => {
|
|
|
- console.log(`Releasing ${message.appData.msgId}...`);
|
|
|
+ next: (message: any) => {
|
|
|
+ console.log(`Releasing ${message.message.appData.msgId}...`);
|
|
|
releaseMessageSubject.next(message);
|
|
|
},
|
|
|
error: (err) => console.error(err),
|
|
@@ -171,11 +171,11 @@ export class FisErrorHandlingService {
|
|
|
}
|
|
|
|
|
|
// Begin to push the incoming messages into local instantarray
|
|
|
- private activateBufferSubscription(bufferStorage: MessageLog[], messageBufferSubscription: Subscription | null, messageToBePublished: Subject<any>): Subscription | null {
|
|
|
+ private activateBufferSubscription(bufferStorage: any[], messageBufferSubscription: Subscription | null, messageToBePublished: Subject<any>): Subscription | null {
|
|
|
if (!messageBufferSubscription) {
|
|
|
messageBufferSubscription = messageToBePublished.subscribe({
|
|
|
- next: (message: MessageLog) => {
|
|
|
- console.log(`Buffering ${message.appData.msgId}... Local array length: ${bufferStorage.length}`);
|
|
|
+ next: (message: any) => {
|
|
|
+ console.log(`Buffering ${message.message.appData.msgId}... Local array length: ${bufferStorage.length}`);
|
|
|
bufferStorage.push(message)
|
|
|
},
|
|
|
error: (err) => console.error(err),
|
|
@@ -204,8 +204,8 @@ export class FisErrorHandlingService {
|
|
|
private activateMongoStreamSubscription(messageStreamToMongo: Subscription | null, messageToBePublished: Subject<any>): Subscription | null {
|
|
|
if (!messageStreamToMongo) {
|
|
|
messageStreamToMongo = messageToBePublished.subscribe({
|
|
|
- next: (message: MessageLog) => {
|
|
|
- console.log(`Saving ${message.appData.msgId}...`);
|
|
|
+ next: (message: any) => {
|
|
|
+ console.log(`Saving ${message.message.appData.msgId}...`);
|
|
|
this.saveToMongo(message)
|
|
|
},
|
|
|
error: (err) => console.error(err),
|
|
@@ -231,22 +231,22 @@ export class FisErrorHandlingService {
|
|
|
}
|
|
|
|
|
|
// Store in json file in this project folder. To be enabled in future
|
|
|
- private async transferMessageToLocalStorage(message: Subject<MessageLog>): Promise<void> {
|
|
|
- let localArray: MessageLog[] = this.bufferedStorage
|
|
|
+ private async transferMessageToLocalStorage(message: Subject<any>): Promise<void> {
|
|
|
+ let localArray: any[] = this.bufferedStorage
|
|
|
let filename = `localstorage.json`;
|
|
|
|
|
|
while (localArray.length > 0) {
|
|
|
let objectToWrite = this.bufferedStorage[0];
|
|
|
await writeMessage(objectToWrite, filename)
|
|
|
}
|
|
|
- message.subscribe((message: MessageLog) => {
|
|
|
+ message.subscribe((message: any) => {
|
|
|
writeMessage(message, filename)
|
|
|
})
|
|
|
|
|
|
if (localArray.length < 1) this.bufferedStorage = localArray
|
|
|
console.log('Local Array is empty. Finished transferring to files.')
|
|
|
|
|
|
- async function writeMessage(message: MessageLog, filename: string) {
|
|
|
+ async function writeMessage(message: any, filename: string) {
|
|
|
try {
|
|
|
let stringifiedMessage = JSON.stringify(message);
|
|
|
await fs.promises.appendFile(filename, stringifiedMessage + "\r\n")
|
|
@@ -259,11 +259,11 @@ export class FisErrorHandlingService {
|
|
|
}
|
|
|
|
|
|
// To be used by mongoStreamSubscription to perform the saving execution
|
|
|
- private async saveToMongo(message: MessageLog): Promise<boolean> {
|
|
|
+ private async saveToMongo(message: any): Promise<boolean> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
// let messageModel: Model<any> = this.mongoConnection.model('Message', require('../models/message.schema'))
|
|
|
this.messageModel.create(message).then(() => {
|
|
|
- console.log(`Saved MessageID ${message.appData.msgId} into ${this.mongoUrl}`);
|
|
|
+ console.log(`Saved MessageID ${message.message.appData.msgId} into ${this.mongoUrl}`);
|
|
|
resolve(true)
|
|
|
}).catch((err) => {
|
|
|
console.log(`MongoSaveError: ${err.message}`)
|
|
@@ -273,13 +273,13 @@ export class FisErrorHandlingService {
|
|
|
}
|
|
|
|
|
|
// As the name implies, transder all the messages from the local instance into mongoStorage. Local instance should be emptied after transfer is completed
|
|
|
- private async transferBufferedMessagseToMongoStorage(bufferedMessage: MessageLog[], messageBufferSubscription): Promise<MessageLog[]> {
|
|
|
+ private async transferBufferedMessageToMongoStorage(bufferedMessage: any[], messageBufferSubscription): Promise<any[]> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- let bufferedStorage: Observable<MessageLog> = from(bufferedMessage)
|
|
|
+ let bufferedStorage: Observable<any> = from(bufferedMessage)
|
|
|
bufferedStorage.subscribe({
|
|
|
- next: (message: MessageLog) => {
|
|
|
+ next: (message: any) => {
|
|
|
this.saveToMongo(message).then((res) => {
|
|
|
- console.log(`Message ${message.appData.msgId} saved successfully...`)
|
|
|
+ console.log(`Message ${message.message.appData.msgId} saved successfully...`)
|
|
|
}).catch((err) => console.error(err))
|
|
|
},
|
|
|
error: (error) => {
|
|
@@ -298,12 +298,12 @@ export class FisErrorHandlingService {
|
|
|
}
|
|
|
|
|
|
// Transfer stored messages from the local instance back into the stream to be returned to the caller.
|
|
|
- private async releaseMessageFromLocalBuffer(bufferedStorage: MessageLog[]): Promise<Observable<MessageLog>> {
|
|
|
+ private async releaseMessageFromLocalBuffer(bufferedStorage: any[]): Promise<Observable<any>> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
if (bufferedStorage.length > 1) {
|
|
|
let caseVariable = this.bufferedStorage.length > 1;
|
|
|
console.log(`Releasing data from local buffer instance. There ${caseVariable ? "is" : "are"} ${this.bufferedStorage.length} messages...`);
|
|
|
- let returnArrayObs: Observable<MessageLog> = from(bufferedStorage)
|
|
|
+ let returnArrayObs: Observable<any> = from(bufferedStorage)
|
|
|
resolve(returnArrayObs)
|
|
|
} else {
|
|
|
let message = `There is no data in stored in local instance`
|
|
@@ -313,9 +313,9 @@ export class FisErrorHandlingService {
|
|
|
}
|
|
|
|
|
|
// Transder all the stored messages in designated mongo databases. It should be empty after all the data has been transferred.
|
|
|
- private async releaseMessageFromMongoStorage(): Promise<Subject<MessageLog>> {
|
|
|
+ private async releaseMessageFromMongoStorage(): Promise<Subject<any>> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- let dataSubject: Subject<MessageLog> = new Subject()
|
|
|
+ let dataSubject: Subject<any> = new Subject()
|
|
|
this.extractAllMessages(dataSubject)
|
|
|
resolve(dataSubject)
|
|
|
})
|