-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ref-imp): added a lib to fetch operation queue size for monitoring
- Loading branch information
1 parent
dc9f1d7
commit 3956109
Showing
12 changed files
with
125 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,36 @@ | ||
import IEventEmitter from './interfaces/IEventEmitter'; | ||
import LogColor from './LogColor'; | ||
import Logger from './Logger'; | ||
|
||
/** | ||
* Event emitter used in Sidetree. | ||
* Intended to be machine readable for triggering custom handlers. | ||
*/ | ||
export default class EventEmitter { | ||
// Default to basic console log. | ||
private static singleton: IEventEmitter = { | ||
emit: async (eventCode, eventData?) => { | ||
if (eventData === undefined) { | ||
console.log(LogColor.lightBlue(`Event emitted: ${LogColor.green(eventCode)}`)); | ||
} else { | ||
console.log(LogColor.lightBlue(`Event emitted: ${LogColor.green(eventCode)}: ${JSON.stringify(eventData)}`)); | ||
} | ||
} | ||
}; | ||
private static customEvenEmitter: IEventEmitter; | ||
|
||
/** | ||
* Overrides the default event emitter if given. | ||
* Initializes with custom event emitter if given. | ||
*/ | ||
static initialize (customEventEmitter?: IEventEmitter) { | ||
if (customEventEmitter !== undefined) { | ||
EventEmitter.singleton = customEventEmitter; | ||
EventEmitter.customEvenEmitter = customEventEmitter; | ||
} | ||
} | ||
|
||
/** | ||
* Emits an event. | ||
*/ | ||
public static async emit (eventName: string, eventData?: {[property: string]: any}): Promise<void> { | ||
await EventEmitter.singleton.emit(eventName, eventData); | ||
public static async emit (eventCode: string, eventData?: {[property: string]: any}): Promise<void> { | ||
// Always log the event using the logger. | ||
if (eventData === undefined) { | ||
Logger.info(LogColor.lightBlue(`Event emitted: ${LogColor.green(eventCode)}`)); | ||
} else { | ||
Logger.info(LogColor.lightBlue(`Event emitted: ${LogColor.green(eventCode)}: ${JSON.stringify(eventData)}`)); | ||
} | ||
|
||
if (EventEmitter.customEvenEmitter !== undefined) { | ||
await EventEmitter.customEvenEmitter.emit(eventCode, eventData); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Config from './models/Config'; | ||
import MongoDbOperationQueue from './versions/latest/MongoDbOperationQueue'; | ||
|
||
/** | ||
* An class to monitor the Core. | ||
* NOTE: this class is completely decoupled from Core, Core does not depend on this class at all for it to function. | ||
*/ | ||
export default class Monitor { | ||
|
||
private operationQueue: MongoDbOperationQueue; | ||
|
||
public constructor () { | ||
this.operationQueue = new MongoDbOperationQueue(); | ||
} | ||
|
||
public async initialize (config: Config) { | ||
this.operationQueue.initialize(config.mongoDbConnectionString, config.databaseName); | ||
} | ||
|
||
/** | ||
* Gets the size of the operation queue. | ||
*/ | ||
public async getOperationQueueSize (): Promise<number> { | ||
const queueSize = await this.operationQueue.getSize(); | ||
return queueSize; | ||
} | ||
} |
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
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
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,17 @@ | ||
import { SidetreeMonitor } from '../../lib'; | ||
|
||
describe('Monitor', async () => { | ||
const testConfig = require('../json/config-test.json'); | ||
|
||
describe('getOperationQueueSize()', async () => { | ||
it('should get operation queue size correctly', async () => { | ||
const monitor = new SidetreeMonitor(); | ||
spyOn((monitor as any).operationQueue, 'initialize'); | ||
spyOn((monitor as any).operationQueue, 'getSize').and.returnValue(Promise.resolve(300)); | ||
|
||
monitor.initialize(testConfig); | ||
const queueSize = await monitor.getOperationQueueSize(); | ||
expect(queueSize).toEqual(300); | ||
}); | ||
}); | ||
}); |
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