1- var util = require ( 'util' ) ,
2- api = require ( './api' ) ,
1+ var api = require ( './api' ) ,
32 CONFIG = require ( '../config/config' ) ,
43 helpers = require ( './helpers' ) ,
54 error = require ( './error' ) ,
6- exc = require ( './exception' ) ,
75
86 storage = [ ] ,
97
@@ -12,13 +10,14 @@ var util = require('util'),
1210
1311 // hash that contains all the errors and their number logged during the current minute
1412 errorStorage = { } ,
15- // flag indicating if messages are being sent right now
16- flag = false ,
13+ sendingLogsInProgress = false ,
1714 // delayed sendLogs call
1815 timeout ,
1916
2017
21- // handler for sending logs
18+ /* handler for sending logs, accepts callback function and boolean variable
19+ indicating if we should run process.exit() after the callback
20+ */
2221 sendLogs = function ( cb , shutdown ) {
2322 var length = storage . length , // number of the messages in the iteration
2423 chunk = Math . min ( length , CONFIG . MSG . MAX_BATCH_SIZE ) ,
@@ -32,19 +31,17 @@ var util = require('util'),
3231 length -= chunk ;
3332 api . lastApiError = 0 ; // reset the last HTTP error
3433
35- console . log ( chunk , ' sent' ) ;
36-
3734 if ( length ) {
3835 chunk = Math . min ( length , CONFIG . MSG . MAX_BATCH_SIZE ) ;
3936 data = storage . slice ( 0 , chunk ) ;
40- api . methods . postLogs ( data , success ) ;
37+ api . methods . postLogs ( data , success , shutdown ) ;
4138 } else {
4239 // full batch is sent, set timeout for the next request
4340 if ( cb ) {
44- // if exception is caught in Express run next middleware
41+ // if exception is caught run next middleware or check if we need to shutdown the server
4542 cb ( ) ;
4643 } else {
47- flag = false ;
44+ sendingLogsInProgress = false ;
4845 timeout = setTimeout ( sendLogs , delay ) ;
4946 }
5047 }
@@ -59,11 +56,11 @@ var util = require('util'),
5956 if ( data . length && CONFIG . APIKEY ) {
6057 // queue has messages and the app is authorized - set the flag, post data
6158 api . methods . postLogs ( data , success , shutdown ) ;
62- flag = true ;
59+ sendingLogsInProgress = true ;
6360 } else {
6461 // queue is empty or app is not authorized, set timeout for the next request, switch the flag
6562 timeout = setTimeout ( sendLogs , delay ) ;
66- flag = false ;
63+ sendingLogsInProgress = false ;
6764 }
6865 } ,
6966 /**
@@ -73,7 +70,7 @@ var util = require('util'),
7370 checkErrorLimitMessage = function checkErrorLimitMessage ( ex ) {
7471 var d = new Date ( ) ,
7572 min = d . getFullYear ( ) . toString ( ) + d . getMonth ( ) . toString ( ) + d . getDate ( ) . toString ( )
76- + d . getHours ( ) . toString ( ) + d . getMinutes ( ) . toString ( ) ;
73+ + d . getHours ( ) . toString ( ) + d . getMinutes ( ) . toString ( ) ,
7774 key = ex . Error . Message + ex . Error . ErrorType + ex . Error . SourceMethod ;
7875
7976 if ( ! ex ) {
@@ -101,7 +98,7 @@ module.exports.methods = {
10198 push : function push ( level , msg , meta , req ) {
10299 var err = new Error ( ) ,
103100 getStack = error . getStackTraceItem ( err ) ,
104- rec = {
101+ messageObject = {
105102 Msg : msg ,
106103 Level : level . toUpperCase ( ) ,
107104 EpochMs : Date . now ( ) ,
@@ -115,42 +112,40 @@ module.exports.methods = {
115112 if ( level . toLowerCase ( ) === 'error' ) {
116113 data = helpers . parseMeta ( meta , true ) ;
117114 if ( data . ex ) {
118- rec . Ex = error . formatEx ( data . ex , req ) ;
115+ messageObject . Ex = error . formatEx ( data . ex , req ) ;
119116 }
120117 } else {
121118 data = helpers . parseMeta ( meta ) ;
122119 }
123120
124121 if ( data . result !== '{}' ) {
125- rec . Data = data . result ;
122+ messageObject . Data = data . result ;
126123 }
127124 }
128125
129- if ( level . toLowerCase ( ) === 'error' && ! rec . Ex ) {
130- rec . Ex = error . formatEx ( err , null , msg ) ;
126+ if ( level . toLowerCase ( ) === 'error' && ! messageObject . Ex ) {
127+ messageObject . Ex = error . formatEx ( err , null , msg ) ;
131128 }
132129
133- if ( ( level . toLowerCase ( ) === 'error' && checkErrorLimitMessage ( rec . Ex ) ) || level . toLowerCase ( ) !== 'error' ) {
134- storage . push ( rec ) ;
135- console . log ( 'logged ' , msg ) ;
130+ if ( ( level . toLowerCase ( ) === 'error' && checkErrorLimitMessage ( messageObject . Ex ) ) || level . toLowerCase ( ) !== 'error' ) {
131+ storage . push ( messageObject ) ;
136132 // remove the earliest message from the queue if message cap is exceeded
137133 if ( storage . length === CONFIG . MSG . QUEUE_CAP ) {
138134 storage . shift ( ) ;
139135 }
140- }
136+ }
141137 } ,
142138
143- /* check the queue after IdentifyApp call is done
139+ /* start sending logs after IdentifyApp call is done
144140 */
145- send : function send ( ) {
141+ start : function start ( ) {
146142 timeout = setTimeout ( sendLogs , delay ) ;
147143 } ,
148- // push exception to the queue and send all the data
149- stop : function stop ( err , req , cb ) {
150- console . log ( 'stopped' ) ;
144+ // reset current delay schedule, push exception to the queue, send data and run the callback
145+ sendException : function sendException ( err , req , cb ) {
151146 var check = function check ( ) {
152147
153- if ( flag ) {
148+ if ( sendingLogsInProgress ) {
154149 setTimeout ( check , 500 ) ;
155150 } else {
156151 sendLogs ( cb , true ) ;
0 commit comments