@@ -46,23 +46,32 @@ export class DefaultEventQueue implements IEventQueue {
46
46
}
47
47
48
48
public enqueue ( event :IEvent ) : void {
49
- var config :Configuration = this . _config ; // Optmization for minifier.
49
+ var config :Configuration = this . _config ; // Optimization for minifier.
50
50
this . ensureQueueTimer ( ) ;
51
51
52
52
if ( this . areQueuedItemsDiscarded ( ) ) {
53
53
config . log . info ( 'Queue items are currently being discarded. The event will not be queued.' ) ;
54
54
return ;
55
55
}
56
56
57
- var key = `${ this . queuePath ( ) } -${ new Date ( ) . toJSON ( ) } -${ Utils . randomNumber ( ) } ` ;
57
+ var key = `ex-q -${ new Date ( ) . toJSON ( ) } -${ Utils . randomNumber ( ) } ` ;
58
58
config . log . info ( `Enqueuing event: ${ key } type=${ event . type } ${ ! ! event . reference_id ? 'refid=' + event . reference_id : '' } ` ) ;
59
59
config . storage . save ( key , event ) ;
60
60
}
61
61
62
62
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
+
63
72
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.
66
75
67
76
this . ensureQueueTimer ( ) ;
68
77
@@ -84,14 +93,14 @@ export class DefaultEventQueue implements IEventQueue {
84
93
this . _processingQueue = true ;
85
94
86
95
try {
87
- var events = config . storage . get ( this . queuePath ( ) , config . submissionBatchSize ) ;
96
+ var events = config . storage . getList ( 'ex-q' , config . submissionBatchSize ) ;
88
97
if ( ! events || events . length == 0 ) {
89
98
this . _processingQueue = false ;
90
99
return ;
91
100
}
92
101
93
102
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 ) => {
95
104
this . processSubmissionResponse ( response , events ) ;
96
105
log . info ( 'Finished processing queue.' ) ;
97
106
this . _processingQueue = false ;
@@ -103,21 +112,21 @@ export class DefaultEventQueue implements IEventQueue {
103
112
}
104
113
}
105
114
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.
110
119
111
120
if ( response . success ) {
112
121
log . info ( `Sent ${ events . length } events.` ) ;
122
+ this . removeEvents ( events ) ;
113
123
return ;
114
124
}
115
125
116
126
if ( response . serviceUnavailable ) {
117
127
// You are currently over your rate limit or the servers are under stress.
118
128
log . error ( 'Server returned service unavailable.' ) ;
119
129
this . suspendProcessing ( ) ;
120
- this . requeueEvents ( events ) ;
121
130
return ;
122
131
}
123
132
@@ -132,13 +141,15 @@ export class DefaultEventQueue implements IEventQueue {
132
141
// The api key was suspended or could not be authorized.
133
142
log . info ( `Unable to authenticate, please check your configuration. ${ noSubmission } ` ) ;
134
143
this . suspendProcessing ( 15 ) ;
144
+ this . removeEvents ( events ) ;
135
145
return ;
136
146
}
137
147
138
148
if ( response . notFound || response . badRequest ) {
139
149
// The service end point could not be found.
140
150
log . error ( `Error while trying to submit data: ${ response . message } ` ) ;
141
151
this . suspendProcessing ( 60 * 4 ) ;
152
+ this . removeEvents ( events ) ;
142
153
return ;
143
154
}
144
155
@@ -147,9 +158,9 @@ export class DefaultEventQueue implements IEventQueue {
147
158
if ( config . submissionBatchSize > 1 ) {
148
159
log . error ( `${ message } Retrying with smaller batch size.` ) ;
149
160
config . submissionBatchSize = Math . max ( 1 , Math . round ( config . submissionBatchSize / 1.5 ) ) ;
150
- this . requeueEvents ( events ) ;
151
161
} else {
152
162
log . error ( `${ message } ${ noSubmission } ` ) ;
163
+ this . removeEvents ( events ) ;
153
164
}
154
165
155
166
return ;
@@ -158,7 +169,6 @@ export class DefaultEventQueue implements IEventQueue {
158
169
if ( ! response . success ) {
159
170
log . error ( `Error submitting events: ${ response . message } ` ) ;
160
171
this . suspendProcessing ( ) ;
161
- this . requeueEvents ( events ) ;
162
172
}
163
173
}
164
174
@@ -175,7 +185,7 @@ export class DefaultEventQueue implements IEventQueue {
175
185
}
176
186
177
187
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.
179
189
180
190
if ( ! durationInMinutes || durationInMinutes <= 0 ) {
181
191
durationInMinutes = 5 ;
@@ -188,21 +198,15 @@ export class DefaultEventQueue implements IEventQueue {
188
198
this . _discardQueuedItemsUntil = new Date ( new Date ( ) . getTime ( ) + ( durationInMinutes * 60000 ) ) ;
189
199
}
190
200
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' ) ) ;
193
204
}
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 ) { }
199
205
}
200
206
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 ) ;
206
210
}
207
211
}
208
212
@@ -213,8 +217,4 @@ export class DefaultEventQueue implements IEventQueue {
213
217
private areQueuedItemsDiscarded ( ) : boolean {
214
218
return this . _discardQueuedItemsUntil && this . _discardQueuedItemsUntil > new Date ( ) ;
215
219
}
216
-
217
- private queuePath ( ) : string {
218
- return 'ex-q' ;
219
- }
220
220
}
0 commit comments