Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TES-4800 allow exposing waitForFlush capability #15

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Contributors
- Coralogix Ltd. <info@coralogix.com> `@coralogix <https://github.com/coralogix>`_
- Yoni Farin <farin99@gmail.com> `@farin99 <https://github.com/farin99>`_
- Amnon Shahar <amnon@coralogix.com> `@amnons77 <https://github.com/amnons77>`_
- Eldar Aliiev <eldar@coralogix.com> `@EldarAliiev <https://github.com/EldarAliiev>`_
- Eldar Aliiev <eldar@coralogix.com> `@EldarAliiev <https://github.com/EldarAliiev>`_
- Benjamin Gruenbaum <benji@testim.io> `@benjamingr <https://github.com/benjamingr>`_
8 changes: 8 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ export declare class CoralogixLogger {
* @public
*/
addLog(log: Log): void;
/**
* @method waitForFlush
* @description waits for all the currently pending to be written to the Coralogix backend
* @memberOf LoggerManager
* @public
* @returns {Promise} returns a promise that settles when all the pending logs have been written
*/
waitForFlush(): Promise<void>;
}
/**
* @class CoralogixCentralLogger
Expand Down
10 changes: 10 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ var CoralogixLogger = (function () {
log.category = log.category || this.category;
CoralogixLogger.loggerManager.addLogline(log);
};
/**
* @method waitForFlush
* @description waits for all the currently pending to be written to the Coralogix backend
* @memberOf LoggerManager
* @public
* @returns {Promise} returns a promise that settles when all the pending logs have been written
*/
CoralogixLogger.prototype.waitForFlush = function () {
return CoralogixLogger.loggerManager.waitForFlush();
};
return CoralogixLogger;
}());
/**
Expand Down
22 changes: 22 additions & 0 deletions dist/logger.manager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,32 @@ export declare class LoggerManager {
* @private
*/
private flush$;
/**
* @member {Promise<void>} flushed
* @memberOf LoggerManager
* @description internal state for when the logs were flushed
* @private
*/
private flushedPromise;
/**
* @member {Promise<void>} () => void
* @memberOf LoggerManager
* @description a resolver for when the flush promise is fulfilled
* @private
*/
private flushPromiseFulfilled;
/**
* @description Initialize new instance of LoggerManager object
*/
constructor();
/**
* @method waitForFlush
* @description waits for all the currently pending to be written to the Coralogix backend
* @memberOf LoggerManager
* @public
* @returns {Promise} returns a promise that settles when all the pending logs have been written
*/
waitForFlush(): Promise<void>;
/**
* @method addLogline
* @description Add log line to logger manager queue
Expand Down
23 changes: 23 additions & 0 deletions dist/logger.manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ var LoggerManager = (function () {
.retryWhen(function (errors$) { return _this.retryObservable(errors$); })
.finally(function () { return _this.cleanAfterSend(buffer); }); }); // clean buffer and start timer
}
/**
* @method waitForFlush
* @description waits for all the currently pending to be written to the Coralogix backend
* @memberOf LoggerManager
* @public
* @returns {Promise} returns a promise that settles when all the pending logs have been written
*/
LoggerManager.prototype.waitForFlush = function () {
var _this = this;
this.flush();
if (!this.logStreamSubscription) {
return Promise.resolve(); // there are no pending logs
}
if (!this.flushedPromise) {
this.flushedPromise = new Promise(function (resolve) {
_this.flushPromiseFulfilled = resolve;
});
}
return this.flushedPromise;
};
/**
* @method addLogline
* @description Add log line to logger manager queue
Expand Down Expand Up @@ -138,6 +158,9 @@ var LoggerManager = (function () {
if (this.bufferSize <= 0 && this.logStreamSubscription) {
this.logStreamSubscription.unsubscribe();
this.logStreamSubscription = null;
if (this.flushPromiseFulfilled) {
this.flushPromiseFulfilled();
}
}
debug_logger_1.DebugLogger.d("clean completed");
this.pauser$.next(false);
Expand Down
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ export class CoralogixLogger {
log.category = log.category || this.category;
CoralogixLogger.loggerManager.addLogline(log);
}

/**
* @method waitForFlush
* @description waits for all the currently pending to be written to the Coralogix backend
* @memberOf LoggerManager
* @public
* @returns {Promise} returns a promise that settles when all the pending logs have been written
*/
public waitForFlush(): Promise<void> {
return CoralogixLogger.loggerManager.waitForFlush();
}
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/logger.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ export class LoggerManager {
*/
private flush$: Subject<any> = new Subject<any>();

/**
* @member {Promise<void>} flushed
* @memberOf LoggerManager
* @description internal state for when the logs were flushed
* @private
*/
private flushedPromise: Promise<void>
/**
* @member {Promise<void>} () => void
* @memberOf LoggerManager
* @description a resolver for when the flush promise is fulfilled
* @private
*/
private flushPromiseFulfilled: () => void;


/**
* @description Initialize new instance of LoggerManager object
*/
Expand All @@ -118,6 +134,25 @@ export class LoggerManager {
.finally(() => this.cleanAfterSend(buffer))); // clean buffer and start timer
}

/**
* @method waitForFlush
* @description waits for all the currently pending to be written to the Coralogix backend
* @memberOf LoggerManager
* @public
* @returns {Promise} returns a promise that settles when all the pending logs have been written
*/
public waitForFlush(): Promise<void> {
this.flush();
if (!this.logStreamSubscription) {
return Promise.resolve(); // there are no pending logs
}
if (!this.flushedPromise) { // there are pending logs - wait for them
this.flushedPromise = new Promise<void>((resolve) => {
this.flushPromiseFulfilled = resolve;
});
}
return this.flushedPromise;
}
/**
* @method addLogline
* @description Add log line to logger manager queue
Expand Down Expand Up @@ -194,6 +229,9 @@ export class LoggerManager {
if (this.bufferSize <= 0 && this.logStreamSubscription) {
this.logStreamSubscription.unsubscribe();
this.logStreamSubscription = null;
if (this.flushPromiseFulfilled) {
this.flushPromiseFulfilled();
}
}
DebugLogger.d("clean completed");
this.pauser$.next(false);
Expand Down
6 changes: 3 additions & 3 deletions src/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ class Tester{
this.logger.addLog(tmp);
}


waitForFlush(){
return this.logger.waitForFlush();
}
}

const tester = new Tester();


37 changes: 36 additions & 1 deletion test/logger.manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,41 @@ describe('LoggerManager', function () {
});
});


describe('#waitForFlush()', function () {
it('should wait for logs to be sent', function (done) {
var logger_manager_instance = new logger_manager.LoggerManager();
var subject = new rxjs.Subject();
logger_manager_instance.sendBulk = function() {
return subject;
};

logger_manager_instance.addLogline(
new log_entry.Log({
threadId: '1234',
severity: log_entry.Severity.debug,
category: 'CORALOGIX',
className: 'Class name',
methodName: 'Method name',
text: 'Test message',
})
);

var waitForFlush = logger_manager_instance.waitForFlush();
var flushed = false;
waitForFlush.then(function () {
flushed = true;
done(); // we want to make sure the promise fulfills
});
setTimeout(function () {
assert.notEqual(flushed, true, 'does not say logs are flushed when they are not');
subject.next({ body: {}, response: {}});
subject.complete();
}, 0);

});
});

describe('#cleanAfterSend()', function () {
it('should clean logs buffer after sent to Coralogix', function () {
var logger_manager_instance = new logger_manager.LoggerManager();
Expand All @@ -102,4 +137,4 @@ describe('LoggerManager', function () {
]);
});
});
});
});