Skip to content

Commit

Permalink
Run webhook migrations in parallel for AvaTax (#1639)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofzuraw authored Oct 29, 2024
1 parent 427c0e6 commit ed729b6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 43 deletions.
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

0 comments on commit ed729b6

Please sign in to comment.