-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsubmitter.ts
More file actions
76 lines (68 loc) · 2.51 KB
/
Copy pathsubmitter.ts
File metadata and controls
76 lines (68 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { syncTaskFactory } from '../syncTask';
import { ISyncTask } from '../types';
import { IRecorderCacheSync } from '../../storages/types';
import { ILogger } from '../../logger/types';
import { SUBMITTERS_PUSH, SUBMITTERS_PUSH_FAILS, SUBMITTERS_PUSH_RETRY } from '../../logger/constants';
import { IResponse } from '../../services/types';
/**
* Base function to create submitters, such as ImpressionsSubmitter and EventsSubmitter
*/
export function submitterFactory<T>(
log: ILogger,
postClient: (body: string) => Promise<IResponse>,
sourceCache: IRecorderCacheSync<T>,
postRate: number,
fromCacheToPayload?: (cacheData: T) => any,
maxRetries: number = 0,
debugLogs?: boolean // true for telemetry submitters
): ISyncTask<[], void> {
const dataName = sourceCache.name;
let retries = 0;
let data: any;
function postData(): Promise<any> {
if (sourceCache.isEmpty() && !data) return Promise.resolve();
data = sourceCache.pop(data);
const dataCountMessage = typeof data.length === 'number' ? `${data.length} ${dataName}` : dataName;
log[debugLogs ? 'debug' : 'info'](SUBMITTERS_PUSH, [dataCountMessage]);
const jsonPayload = JSON.stringify(fromCacheToPayload ? fromCacheToPayload(data) : data);
if (!maxRetries) data = undefined;
return postClient(jsonPayload).then(() => {
retries = 0;
data = undefined;
}).catch(err => {
if (!maxRetries) {
log[debugLogs ? 'debug' : 'warn'](SUBMITTERS_PUSH_FAILS, [dataCountMessage, err]);
} else if (retries === maxRetries) {
retries = 0;
data = undefined;
log[debugLogs ? 'debug' : 'warn'](SUBMITTERS_PUSH_FAILS, [dataCountMessage, err]);
} else {
retries++;
log[debugLogs ? 'debug' : 'warn'](SUBMITTERS_PUSH_RETRY, [dataCountMessage, err]);
}
});
}
return syncTaskFactory(log, postData, postRate, dataName + ' submitter');
}
/**
* Decorates a provided submitter with a first execution window
*/
export function firstPushWindowDecorator(submitter: ISyncTask, firstPushWindow: number) {
let running = false;
let stopEventPublisherTimeout: ReturnType<typeof setTimeout>;
const originalStart = submitter.start;
submitter.start = () => {
running = true;
stopEventPublisherTimeout = setTimeout(originalStart, firstPushWindow);
};
const originalStop = submitter.stop;
submitter.stop = () => {
running = false;
clearTimeout(stopEventPublisherTimeout);
originalStop();
};
submitter.isRunning = () => {
return running;
};
return submitter;
}