Skip to content

Commit

Permalink
10553: Refactor worker and add logs;
Browse files Browse the repository at this point in the history
  • Loading branch information
John Cruz committed Dec 6, 2024
1 parent 7ca4559 commit af4ba88
Showing 1 changed file with 57 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ export const queueEmailUpdateAssociatedCasesWorker = async (
{ user }: { user: RawUser | RawPractitioner },
authorizedUser: AuthUser,
): Promise<void> => {
const docketNumbersAssociatedWithUser = await applicationContext
const docketNumbersByUser = await applicationContext
.getPersistenceGateway()
.getDocketNumbersByUser({
applicationContext,
userId: user.userId,
});

if (!docketNumbersAssociatedWithUser.length) {
if (!docketNumbersByUser.length) {
console.log('*** USER DOES NOT HAVE ANY CASES TO UPDATE');
await disableIsUserUpdatingFlag({ applicationContext, user });
return;
}

console.log(`*** QUEUING WORKERS TO UPDATE (${docketNumbersByUser.length})`);
await applicationContext
.getUseCases()
.queueUpdateAssociatedCasesWorker(
Expand All @@ -60,30 +62,60 @@ export const queueEmailUpdateAssociatedCasesWorker = async (
authorizedUser,
);

try {
const expectedUpdatedCaseCount = (
await applicationContext.getPersistenceGateway().getDocketNumbersByUser({
applicationContext,
userId: user.userId,
})
).length;
console.log('*** STARTING TO CHECK COUNT');
await waitUntilAllExpectedCasesAreUpdatedWithEmail({
applicationContext,
userEmail: user.email!,
})
.catch(error =>
console.error(`ERROR CHECKING COUNT OF UPDATED CASES -> ${error}`),
)
.finally(async () => {
await disableIsUserUpdatingFlag({ applicationContext, user });
});
};

let checkCount = true;
while (checkCount) {
await applicationContext.getUtilities().sleep(1500);
const actualUpdatedCaseCount = await applicationContext
.getPersistenceGateway()
.getCasesByEmailTotal({
applicationContext,
email: user.email!,
});
const WAIT_TIMEOUT = 2000;
const MAX_WAITTIME_IN_MINUTES = 14;
const MAX_ITERATIONS = Math.floor(
(MAX_WAITTIME_IN_MINUTES * 60 * 1000) / WAIT_TIMEOUT,
);

if (actualUpdatedCaseCount >= expectedUpdatedCaseCount)
checkCount = false;
}
async function waitUntilAllExpectedCasesAreUpdatedWithEmail({
applicationContext,
iteration = 0,
userEmail,
}: {
applicationContext: ServerApplicationContext;
iteration?: number;
userEmail: string;
}): Promise<void> {
const docketNumbersByUser = await applicationContext
.getPersistenceGateway()
.getDocketNumbersByUser({
applicationContext,
userId: userEmail,
});
const expectedCount = docketNumbersByUser.length;

await disableIsUserUpdatingFlag({ applicationContext, user });
} catch (e) {
await disableIsUserUpdatingFlag({ applicationContext, user });
const actualCount = await applicationContext
.getPersistenceGateway()
.getCasesByEmailTotal({
applicationContext,
email: userEmail,
});

if (actualCount >= expectedCount) return;

if (iteration % 10 === 0) {
console.log('*** Expected Count: ', expectedCount);
console.log('*** Actual Count: ', actualCount);
}
};

if (iteration >= MAX_ITERATIONS) return;
return waitUntilAllExpectedCasesAreUpdatedWithEmail({
applicationContext,
iteration: iteration + 1,
userEmail,
});
}

0 comments on commit af4ba88

Please sign in to comment.