Skip to content

Commit 9a97b88

Browse files
committed
Added handling for the size constraints.
When the size reaches 80% of the 200KB size for the message, executes the "onTimeout" function and returns the PROGRESS event.
1 parent a9a0fe5 commit 9a97b88

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

src/common/event-size-monitor.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { EventData } from '../types/extraction';
2+
import { ErrorRecord } from '../types/common';
3+
4+
const MAX_EVENT_SIZE = 200_000;
5+
const SIZE_LIMIT_THRESHOLD = Math.floor(MAX_EVENT_SIZE * 0.8); // 160_000 bytes
6+
7+
/**
8+
* Get the JSON serialized size of event data in bytes
9+
*/
10+
export function getEventDataSize(data: EventData | undefined): number {
11+
if (!data) return 0;
12+
return JSON.stringify(data).length;
13+
}
14+
15+
/**
16+
* Check if event data exceeds the 80% threshold (160KB)
17+
*/
18+
export function shouldTriggerSizeLimit(data: EventData | undefined): boolean {
19+
return getEventDataSize(data) > SIZE_LIMIT_THRESHOLD;
20+
}
21+
22+
/**
23+
* Truncate error message to max length (default 1000 chars)
24+
*/
25+
export function truncateErrorMessage(
26+
error: ErrorRecord | undefined,
27+
maxLength: number = 1000
28+
): ErrorRecord | undefined {
29+
if (!error) return undefined;
30+
31+
return {
32+
message: error.message.substring(0, maxLength)
33+
};
34+
}
35+
36+
/**
37+
* Prune event data by truncating error messages
38+
* Always applied before serialization
39+
*/
40+
export function pruneEventData(data: EventData | undefined): EventData | undefined {
41+
if (!data) return data;
42+
43+
return {
44+
...data,
45+
error: truncateErrorMessage(data.error),
46+
};
47+
}
48+
49+
/**
50+
* Log detailed warning when size limit is detected
51+
*/
52+
export function logSizeLimitWarning(size: number, triggerType: 'onUpload' | 'onEmit'): void {
53+
const percentage = (size / MAX_EVENT_SIZE) * 100;
54+
const detailsString = triggerType === 'onUpload'
55+
? 'during data collection. Emitting progress event and stopping further processing.'
56+
: 'during emit. Error messages truncated.';
57+
58+
console.warn(
59+
`[SIZE_LIMIT] Event data size ${size} bytes (${percentage.toFixed(1)}% of ${MAX_EVENT_SIZE} limit) detected ${detailsString}`
60+
);
61+
}
62+
63+
export { MAX_EVENT_SIZE, SIZE_LIMIT_THRESHOLD };

src/workers/process-task.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ export function processTask<ConnectorState>({
4848
}
4949
});
5050
await task({ adapter });
51+
52+
// If size limit was triggered during task, call onTimeout for cleanup
53+
if (adapter.isTimeout) {
54+
console.log(
55+
'[SIZE_LIMIT] Size limit detected during data collection. Executing onTimeout function for cleanup.'
56+
);
57+
await onTimeout({ adapter });
58+
}
59+
5160
process.exit(0);
5261
}
5362
} catch (error) {

src/workers/worker-adapter.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ import { Uploader } from '../uploader/uploader';
4545
import { serializeError } from '../logger/logger';
4646
import { SyncMapperRecordStatus } from '../mappers/mappers.interface';
4747
import { AttachmentsStreamingPool } from '../attachments-streaming/attachments-streaming-pool';
48+
import {
49+
pruneEventData,
50+
logSizeLimitWarning,
51+
SIZE_LIMIT_THRESHOLD,
52+
} from '../common/event-size-monitor';
4853

4954
const MAX_MESSAGE_LENGTH: number = 200_000;
5055

@@ -161,10 +166,17 @@ export class WorkerAdapter<ConnectorState> {
161166
);
162167
}
163168

164-
if(this.currentLength + newLength > MAX_MESSAGE_LENGTH) {
165-
// TODO: We need to call the adapter's `onTimeout` here and `emit` the Progress event.
166-
// We might have to run the `uploadAllRepos` as well.
169+
this.currentLength += newLength;
170+
171+
// Check for size limit (80% of 200KB = 160KB threshold)
172+
if (this.currentLength > SIZE_LIMIT_THRESHOLD && !this.hasWorkerEmitted) {
173+
logSizeLimitWarning(this.currentLength, 'onUpload');
174+
175+
// Set timeout flag to trigger onTimeout cleanup after task completes
167176
this.handleTimeout();
177+
178+
// Emit progress event to save state and continue on next iteration
179+
void this.emit(ExtractorEventType.ExtractionDataProgress);
168180
}
169181
},
170182
options: this.options,
@@ -257,11 +269,14 @@ export class WorkerAdapter<ConnectorState> {
257269
}
258270

259271
try {
272+
// Always prune error messages to 1000 chars before emit
273+
const prunedData = pruneEventData(data);
274+
260275
await emit({
261276
eventType: newEventType,
262277
event: this.event,
263278
data: {
264-
...data,
279+
...prunedData,
265280
...(ALLOWED_EXTRACTION_EVENT_TYPES.includes(
266281
this.event.payload.event_type
267282
)

0 commit comments

Comments
 (0)