Skip to content

Commit

Permalink
fix(perf): wait for application session instead of queuing to resilie…
Browse files Browse the repository at this point in the history
…nt queue
  • Loading branch information
uladkasach committed Jul 25, 2023
1 parent 425b8af commit 92405eb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/logic/capture/captureObservationEvent.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import { createQueueWithResilientRemoteConsumer } from 'simple-in-memory-queue';

import { AppUsageEvent, AppUsageEventSource } from '../../domain/AppUsageEvent';
import { waitForApplicationOfSession } from '../session/getApplicationOfSession';
import { captureAppUsageEvent } from './captureAppUsageEvent';

const observationCaptureQueue = createQueueWithResilientRemoteConsumer<{
type: string;
details: AppUsageEvent['details'];
}>({
consumer: async ({ item }) => {
await captureAppUsageEvent({
source: AppUsageEventSource.OBSERVATION,
type: item.type,
details: item.details,
});
},
threshold: {
concurrency: 1,
retry: 5,
pause: 10,
},
delay: {
retry: 300,
},
});

/**
* enables programmatically capturing an observation that will be emitted as an app usage event
*
Expand All @@ -43,5 +21,10 @@ export const captureObservationEvent = async ({
details: AppUsageEvent['details'];
}) => {
if (typeof window === 'undefined') return; // do nothing on serverside, since no installation-uuid available
await observationCaptureQueue.push({ type, details });
await waitForApplicationOfSession();
await captureAppUsageEvent({
source: AppUsageEventSource.OBSERVATION,
type,
details,
});
};
10 changes: 10 additions & 0 deletions src/logic/session/getApplicationOfSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ export const getApplicationOfSession = (): AppUsageApplication => {
if (!application) throw new Error('application not set yet');
return application;
};

export const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
export const waitForApplicationOfSession = async () => {
for (let attempt = 0; attempt < 5; attempt++) {
if (application) return application;
await sleep(300);
}
throw new Error('application wait attempts exceeded');
};

0 comments on commit 92405eb

Please sign in to comment.