@@ -46,23 +46,32 @@ export class DefaultEventQueue implements IEventQueue {
4646 }
4747
4848 public enqueue ( event :IEvent ) : void {
49- var config :Configuration = this . _config ; // Optmization for minifier.
49+ var config :Configuration = this . _config ; // Optimization for minifier.
5050 this . ensureQueueTimer ( ) ;
5151
5252 if ( this . areQueuedItemsDiscarded ( ) ) {
5353 config . log . info ( 'Queue items are currently being discarded. The event will not be queued.' ) ;
5454 return ;
5555 }
5656
57- var key = `${ this . queuePath ( ) } -${ new Date ( ) . toJSON ( ) } -${ Utils . randomNumber ( ) } ` ;
57+ var key = `ex-q -${ new Date ( ) . toJSON ( ) } -${ Utils . randomNumber ( ) } ` ;
5858 config . log . info ( `Enqueuing event: ${ key } type=${ event . type } ${ ! ! event . reference_id ? 'refid=' + event . reference_id : '' } ` ) ;
5959 config . storage . save ( key , event ) ;
6060 }
6161
6262 public process ( ) : void {
63+ function getEvents ( events :{ path :string , value :IEvent } [ ] ) :IEvent [ ] {
64+ var items :IEvent [ ] = [ ] ;
65+ for ( var index = 0 ; index < events . length ; index ++ ) {
66+ items . push ( events [ index ] . value ) ;
67+ }
68+
69+ return items ;
70+ }
71+
6372 const queueNotProcessed :string = 'The queue will not be processed.' ; // optimization for minifier.
64- var config :Configuration = this . _config ; // Optmization for minifier.
65- var log :ILog = config . log ; // Optmization for minifier.
73+ var config :Configuration = this . _config ; // Optimization for minifier.
74+ var log :ILog = config . log ; // Optimization for minifier.
6675
6776 this . ensureQueueTimer ( ) ;
6877
@@ -84,14 +93,14 @@ export class DefaultEventQueue implements IEventQueue {
8493 this . _processingQueue = true ;
8594
8695 try {
87- var events = config . storage . get ( this . queuePath ( ) , config . submissionBatchSize ) ;
96+ var events = config . storage . getList ( 'ex-q' , config . submissionBatchSize ) ;
8897 if ( ! events || events . length == 0 ) {
8998 this . _processingQueue = false ;
9099 return ;
91100 }
92101
93102 log . info ( `Sending ${ events . length } events to ${ config . serverUrl } .` ) ;
94- config . submissionClient . postEvents ( events , config , ( response :SubmissionResponse ) => {
103+ config . submissionClient . postEvents ( getEvents ( events ) , config , ( response :SubmissionResponse ) => {
95104 this . processSubmissionResponse ( response , events ) ;
96105 log . info ( 'Finished processing queue.' ) ;
97106 this . _processingQueue = false ;
@@ -103,21 +112,21 @@ export class DefaultEventQueue implements IEventQueue {
103112 }
104113 }
105114
106- private processSubmissionResponse ( response :SubmissionResponse , events :IEvent [ ] ) : void {
107- const noSubmission :string = 'The event will not be submitted.' ; // Optmization for minifier.
108- var config :Configuration = this . _config ; // Optmization for minifier.
109- var log :ILog = config . log ; // Optmization for minifier.
115+ private processSubmissionResponse ( response :SubmissionResponse , events :{ path : string , value : IEvent } [ ] ) : void {
116+ const noSubmission :string = 'The event will not be submitted.' ; // Optimization for minifier.
117+ var config :Configuration = this . _config ; // Optimization for minifier.
118+ var log :ILog = config . log ; // Optimization for minifier.
110119
111120 if ( response . success ) {
112121 log . info ( `Sent ${ events . length } events.` ) ;
122+ this . removeEvents ( events ) ;
113123 return ;
114124 }
115125
116126 if ( response . serviceUnavailable ) {
117127 // You are currently over your rate limit or the servers are under stress.
118128 log . error ( 'Server returned service unavailable.' ) ;
119129 this . suspendProcessing ( ) ;
120- this . requeueEvents ( events ) ;
121130 return ;
122131 }
123132
@@ -132,13 +141,15 @@ export class DefaultEventQueue implements IEventQueue {
132141 // The api key was suspended or could not be authorized.
133142 log . info ( `Unable to authenticate, please check your configuration. ${ noSubmission } ` ) ;
134143 this . suspendProcessing ( 15 ) ;
144+ this . removeEvents ( events ) ;
135145 return ;
136146 }
137147
138148 if ( response . notFound || response . badRequest ) {
139149 // The service end point could not be found.
140150 log . error ( `Error while trying to submit data: ${ response . message } ` ) ;
141151 this . suspendProcessing ( 60 * 4 ) ;
152+ this . removeEvents ( events ) ;
142153 return ;
143154 }
144155
@@ -147,9 +158,9 @@ export class DefaultEventQueue implements IEventQueue {
147158 if ( config . submissionBatchSize > 1 ) {
148159 log . error ( `${ message } Retrying with smaller batch size.` ) ;
149160 config . submissionBatchSize = Math . max ( 1 , Math . round ( config . submissionBatchSize / 1.5 ) ) ;
150- this . requeueEvents ( events ) ;
151161 } else {
152162 log . error ( `${ message } ${ noSubmission } ` ) ;
163+ this . removeEvents ( events ) ;
153164 }
154165
155166 return ;
@@ -158,7 +169,6 @@ export class DefaultEventQueue implements IEventQueue {
158169 if ( ! response . success ) {
159170 log . error ( `Error submitting events: ${ response . message } ` ) ;
160171 this . suspendProcessing ( ) ;
161- this . requeueEvents ( events ) ;
162172 }
163173 }
164174
@@ -175,7 +185,7 @@ export class DefaultEventQueue implements IEventQueue {
175185 }
176186
177187 public suspendProcessing ( durationInMinutes ?:number , discardFutureQueuedItems ?:boolean , clearQueue ?:boolean ) : void {
178- var config :Configuration = this . _config ; // Optmization for minifier.
188+ var config :Configuration = this . _config ; // Optimization for minifier.
179189
180190 if ( ! durationInMinutes || durationInMinutes <= 0 ) {
181191 durationInMinutes = 5 ;
@@ -188,21 +198,15 @@ export class DefaultEventQueue implements IEventQueue {
188198 this . _discardQueuedItemsUntil = new Date ( new Date ( ) . getTime ( ) + ( durationInMinutes * 60000 ) ) ;
189199 }
190200
191- if ( ! clearQueue ) {
192- return ;
201+ if ( clearQueue ) {
202+ // Account is over the limit and we want to ensure that the sample size being sent in will contain newer errors.
203+ this . removeEvents ( config . storage . getList ( 'ex-q' ) ) ;
193204 }
194-
195- // Account is over the limit and we want to ensure that the sample size being sent in will contain newer errors.
196- try {
197- config . storage . clear ( this . queuePath ( ) ) ;
198- } catch ( Exception ) { }
199205 }
200206
201- private requeueEvents ( events :IEvent [ ] ) : void {
202- this . _config . log . info ( `Requeuing ${ events . length } events.` ) ;
203-
204- for ( var index = 0 ; index < events . length ; index ++ ) {
205- this . enqueue ( events [ index ] ) ;
207+ private removeEvents ( events :{ path :string , value :IEvent } [ ] ) {
208+ for ( var index = 0 ; index < ( events || [ ] ) . length ; index ++ ) {
209+ this . _config . storage . remove ( events [ index ] . path ) ;
206210 }
207211 }
208212
@@ -213,8 +217,4 @@ export class DefaultEventQueue implements IEventQueue {
213217 private areQueuedItemsDiscarded ( ) : boolean {
214218 return this . _discardQueuedItemsUntil && this . _discardQueuedItemsUntil > new Date ( ) ;
215219 }
216-
217- private queuePath ( ) : string {
218- return 'ex-q' ;
219- }
220220}
0 commit comments