Skip to content

Instantly share code, notes, and snippets.

@ypcode
Created March 7, 2020 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypcode/6df9abb91f66ef093a6e9517c97da4ca to your computer and use it in GitHub Desktop.
Save ypcode/6df9abb91f66ef093a6e9517c97da4ca to your computer and use it in GitHub Desktop.
import { AzureFunction, Context } from "@azure/functions"
import { isNearlyExpired, getExpirationDateTimeISOString } from "../helpers/date-helper";
import { updateSubscription, getSubscription } from "../helpers/subscriptions-helper";
import { setupNewSubscription } from "../helpers/subscription-setup";
import { configure, SETUP_EXPIRATION_DELAY_IN_DAYS } from "../helpers/configure";
const queueTriggeredEnsureSubscription: AzureFunction = async function (context: Context, subscriptionId: string): Promise<void> {
context.log(`Ensuring subscription ${subscriptionId}...`);
configure(context);
const foundSubscription = await getSubscription(subscriptionId, context.log);
if (foundSubscription) {
context.log("A subscription has been found !");
context.log(`Resource : ${foundSubscription.resource}`);
context.log(`Change type : ${foundSubscription.changeType}`);
context.log(`Expires on : ${foundSubscription.expirationDateTime}`);
// At this point, if we realize the subscription is expiring soon, we update it with a new maximum delay starting right now !
if (isNearlyExpired(foundSubscription.expirationDateTime)) {
const expirationDateTime = getExpirationDateTimeISOString(SETUP_EXPIRATION_DELAY_IN_DAYS);
context.log(`Will update the expiration to ${expirationDateTime}`);
await updateSubscription({
id: foundSubscription.id,
expirationDateTime
},
context.log);
context.log("Subscription updated.");
} else {
context.log("The subscription will not expire soon...");
}
} else {
context.log(`No subscription has been found with id ${subscriptionId}...`);
// If subscription is not found, it might have been deleted in the meantime for some reason
// In order to keep the whole system stable, we recreate a new subscription here
await setupNewSubscription(context.log);
}
};
export default queueTriggeredEnsureSubscription;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment