Skip to content

Commit

Permalink
fix: issues with reclaim, max items and log debugging data
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumarrs committed Nov 27, 2024
1 parent 378078d commit 75f828b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const NativeDestinationQueue = (): ExtensionPlugin => ({
},
storeManager,
MEMORY_STORAGE,
logger
);

// TODO: This seems to not work as expected. Need to investigate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,12 @@ class RetryQueue implements IQueue<QueueItemData> {
let queue =
(this.getStorageEntry(QueueStatuses.QUEUE) as Nullable<QueueItem<QueueItemData>[]>) ?? [];

queue = queue.slice(-(this.maxItems - 1));
if (this.maxItems > 1) {
queue = queue.slice(-(this.maxItems - 1));
} else {
queue = [];

Check warning on line 373 in packages/analytics-js-plugins/src/utilities/retryQueue/RetryQueue.ts

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-plugins/src/utilities/retryQueue/RetryQueue.ts#L373

Added line #L373 was not covered by tests
}

queue.push(curEntry);
queue = queue.sort(sortByTime);

Expand Down Expand Up @@ -542,8 +547,15 @@ class RetryQueue implements IQueue<QueueItemData> {
this.processQueueCb(el.item, el.done, el.attemptNumber, this.maxAttempts, willBeRetried);
} catch (err) {
// drop the event from in progress queue as we're unable to process it
el.done();
// el.done();
this.logger?.error(RETRY_QUEUE_PROCESS_ERROR(RETRY_QUEUE), err);
this.logger?.error('Debugging data dump starts');
this.logger?.error('Queue item', el);
this.logger?.error('RetryQueue Instance', this);
this.logger?.error('Primary Queue', this.getStorageEntry(QueueStatuses.QUEUE));
this.logger?.error('In-Progress Queue', this.getStorageEntry(QueueStatuses.IN_PROGRESS));
this.logger?.error('RudderStack Globals', (globalThis as typeof window).RudderStackGlobals);
this.logger?.error('Debugging data dump ends');
}
});

Expand Down Expand Up @@ -586,9 +598,9 @@ class RetryQueue implements IQueue<QueueItemData> {
validKeys: QueueStatuses,
type: LOCAL_STORAGE,
});
const our = {
queue: (this.getStorageEntry(QueueStatuses.QUEUE) ?? []) as QueueItem[],
};

const reclaimedQueueItems: QueueItem[] = [];

const their = {
inProgress: other.get(QueueStatuses.IN_PROGRESS) ?? {},
batchQueue: other.get(QueueStatuses.BATCH_QUEUE) ?? [],
Expand All @@ -611,7 +623,7 @@ class RetryQueue implements IQueue<QueueItemData> {
// and the new entries will have the type field set
const type = Array.isArray(el.item) ? BATCH_QUEUE_ITEM_TYPE : SINGLE_QUEUE_ITEM_TYPE;

our.queue.push({
reclaimedQueueItems.push({
item: el.item,
attemptNumber: el.attemptNumber + incrementAttemptNumberBy,
time: this.schedule.now(),
Expand Down Expand Up @@ -654,9 +666,15 @@ class RetryQueue implements IQueue<QueueItemData> {
// if the queue is abandoned, all the in-progress are failed. retry them immediately and increment the attempt#
addConcatQueue(their.inProgress, 1);

our.queue = our.queue.sort(sortByTime);
let ourQueue = (this.getStorageEntry(QueueStatuses.QUEUE) as QueueItem[]) ?? [];
const roomInQueue = Math.max(0, this.maxItems - ourQueue.length);
if (roomInQueue > 0) {
ourQueue.push(...reclaimedQueueItems.slice(0, roomInQueue));
}

ourQueue = ourQueue.sort(sortByTime);

this.setStorageEntry(QueueStatuses.QUEUE, our.queue);
this.setStorageEntry(QueueStatuses.QUEUE, ourQueue);

// remove all keys one by on next tick to avoid NS_ERROR_STORAGE_BUSY error
this.clearQueueEntries(other, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LOG_CONTEXT_SEPARATOR } from '../../shared-chunks/common';

const RETRY_QUEUE_PROCESS_ERROR = (context: string): string =>
`${context}${LOG_CONTEXT_SEPARATOR}Process function threw an error.`;
`${context}${LOG_CONTEXT_SEPARATOR}Process function threw an error while processing the queue item. The item is be dropped.`;

Check warning on line 4 in packages/analytics-js-plugins/src/utilities/retryQueue/logMessages.ts

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-plugins/src/utilities/retryQueue/logMessages.ts#L4

Added line #L4 was not covered by tests

const RETRY_QUEUE_ENTRY_REMOVE_ERROR = (context: string, entry: string, attempt: number): string =>
`${context}${LOG_CONTEXT_SEPARATOR}Failed to remove local storage entry "${entry}" (attempt: ${attempt}.`;
Expand Down

0 comments on commit 75f828b

Please sign in to comment.