Skip to content

Commit

Permalink
[ServiceBus] improve auto lock renew logging when stopping (Azure#24561)
Browse files Browse the repository at this point in the history
* [sb] improve auto lock renew logging

previously the two cases share same log message, which can be confusing. This PR
separate them to have different log messages.

* Move negative cases first

to improve readability.  Having them at the end makes it hard to relate them
with their corresponding conditions

* Format
  • Loading branch information
jeremymeng authored Jan 26, 2023
1 parent 05174d3 commit f266acd
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions sdk/servicebus/service-bus/src/core/autoLockRenewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,33 @@ export class LockRenewer {
}' is locked until ${bMessage.lockedUntilUtc!.toString()}.`
);
const totalAutoLockRenewDuration = Date.now() + this._maxAutoRenewDurationInMs;
const totalAutoLockRenewDurationDate = new Date(totalAutoLockRenewDuration);
logger.verbose(
`${logPrefix} Total autolockrenew duration for message with id '${
bMessage.messageId
}' is: ${new Date(totalAutoLockRenewDuration).toString()}`
}' is: ${totalAutoLockRenewDurationDate.toString()}`
);

const autoRenewLockTask = (): void => {
const renewalNeededToMaintainLock =
// if the lock expires _after_ our max auto-renew duration there's no reason to
// spin up an auto-renewer - it's already held for the duration.
new Date(totalAutoLockRenewDuration) > bMessage.lockedUntilUtc!;
totalAutoLockRenewDurationDate > bMessage.lockedUntilUtc!;

// once we've exceeded the max amount of time we'll renew we can stop.
const haventExceededMaxLockRenewalTime = Date.now() < totalAutoLockRenewDuration;

if (renewalNeededToMaintainLock && haventExceededMaxLockRenewalTime) {
if (!renewalNeededToMaintainLock) {
logger.verbose(
`${logPrefix} Autolockrenew not needed as message's lockedUntilUtc ${bMessage.lockedUntilUtc} is after the total autolockrenew duration ${totalAutoLockRenewDurationDate} for message with messageId '${bMessage.messageId}'. Hence we will stop the autoLockRenewTask.`
);
this.stop(linkEntity, bMessage);
} else if (Date.now() >= totalAutoLockRenewDuration) {
// once we've exceeded the max amount of time we'll renew we can stop.
logger.verbose(
`${logPrefix} Current time ${new Date()} exceeds the total autolockrenew duration ${totalAutoLockRenewDurationDate} for message with messageId '${
bMessage.messageId
}'. Hence we will stop the autoLockRenewTask.`
);
this.stop(linkEntity, bMessage);
} else {
if (linkMessageMap.has(bMessage.messageId as string)) {
// TODO: We can run into problems with clock skew between the client and the server.
// It would be better to calculate the duration based on the "lockDuration" property
Expand Down Expand Up @@ -212,16 +223,6 @@ export class LockRenewer {
`${logPrefix} Looks like the message lock renew timer has already been cleared for message with id '${bMessage.messageId}'.`
);
}
} else {
logger.verbose(
`${logPrefix} Current time ${new Date()} exceeds the total autolockrenew duration ${new Date(
totalAutoLockRenewDuration
)} for message with messageId '${
bMessage.messageId
}'. Hence we will stop the autoLockRenewTask.`
);

this.stop(linkEntity, bMessage);
}
};

Expand Down

0 comments on commit f266acd

Please sign in to comment.