Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run webhook migrations in parallel for AvaTax #1639

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-jokes-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-avatax": patch
---

Run webhook migrations in parallel.
88 changes: 45 additions & 43 deletions apps/avatax/scripts/run-webhooks-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,51 @@ const runMigrations = async () => {
process.exit(1);
});

for (const saleorEnv of saleorCloudEnv) {
const { saleorApiUrl, token } = saleorEnv;

logger.info(`Migrating webhooks for ${saleorApiUrl}`);

const client = createInstrumentedGraphqlClient({
saleorApiUrl: saleorApiUrl,
token: token,
});

const runner = new WebhookMigrationRunner({
dryRun,
logger,
client,
saleorApiUrl,
getManifests: async ({ appDetails }) => {
const webhooks = appDetails.webhooks;

if (!webhooks?.length) {
logger.warn("The environment does not have any webhooks, skipping");
return [];
}

const enabled = webhooks.some((w) => w.isActive);

const targetUrl = appDetails.appUrl;

if (!targetUrl?.length) {
logger.error("App has no defined appUrl, skipping");
return [];
}

const baseUrl = new URL(targetUrl).origin;

// All webhooks in this application are turned on or off. If any of them is enabled, we enable all of them.
return appWebhooks.map((w) => ({ ...w.getWebhookManifest(baseUrl), isActive: enabled }));
},
});

await runner.migrate().catch((error) => {
Sentry.captureException(error);
});
}
await Promise.allSettled(
saleorCloudEnv.map(async (saleorEnv) => {
const { saleorApiUrl, token } = saleorEnv;

logger.info(`Migrating webhooks for ${saleorApiUrl}`);

const client = createInstrumentedGraphqlClient({
saleorApiUrl: saleorApiUrl,
token: token,
});

const runner = new WebhookMigrationRunner({
dryRun,
logger,
client,
saleorApiUrl,
getManifests: async ({ appDetails }) => {
const webhooks = appDetails.webhooks;

if (!webhooks?.length) {
logger.warn("The environment does not have any webhooks, skipping");
return [];
}

const enabled = webhooks.some((w) => w.isActive);

const targetUrl = appDetails.appUrl;

if (!targetUrl?.length) {
logger.error("App has no defined appUrl, skipping");
return [];
}

const baseUrl = new URL(targetUrl).origin;

// All webhooks in this application are turned on or off. If any of them is enabled, we enable all of them.
return appWebhooks.map((w) => ({ ...w.getWebhookManifest(baseUrl), enabled }));
},
});

await runner.migrate().catch((error) => {
Sentry.captureException(error);
});
}),
);
};

runMigrations();
Expand Down
Loading