Skip to content

Commit 7731dc3

Browse files
authored
fix(e2e): delete PLAPI apps older than one hour (#9682)
1 parent 88a965c commit 7731dc3

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎.changeset/calm-dogs-smile.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

‎.github/workflows/e2e-cleanups.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
- name: Run cleanup
3939
run: pnpm test:integration:cleanup
4040
env:
41+
E2E_CLEANUP_STALE_APPLICATIONS: '1'
4142
CLERK_PLATFORM_API_KEY: ${{ secrets.CLERK_PLATFORM_API_KEY }}
4243
INTEGRATION_INSTANCE_KEYS: ${{ secrets.INTEGRATION_INSTANCE_KEYS }}
4344
INTEGRATION_STAGING_INSTANCE_KEYS: ${{ secrets.INTEGRATION_STAGING_INSTANCE_KEYS }}

‎integration/cleanup/cleanup.setup.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,16 @@ setup('cleanup instances ', async () => {
243243
const applications = await listApplications(constants.CLERK_PLATFORM_API_KEY);
244244
console.log(`Found ${applications.length} Platform API applications.`);
245245

246-
if (applicationRunMarker) {
246+
if (constants.E2E_CLEANUP_STALE_APPLICATIONS || applicationRunMarker) {
247+
const oneHourAgo = Date.now() - 60 * 60 * 1000;
247248
const applicationNameSuffix = `-${applicationRunMarker}`;
248-
const applicationsToDelete = applications.filter(application =>
249-
application.name.endsWith(applicationNameSuffix),
250-
);
249+
const applicationsToDelete = applications.filter(application => {
250+
if (!constants.E2E_CLEANUP_STALE_APPLICATIONS) {
251+
return application.name.endsWith(applicationNameSuffix);
252+
}
253+
const createdAt = getApplicationCreatedAt(application.application_id);
254+
return application.name.startsWith('e2e-') && createdAt !== undefined && createdAt < oneHourAgo;
255+
});
251256

252257
for (const application of applicationsToDelete) {
253258
try {
@@ -318,3 +323,21 @@ function batchElements<T>(objects: T[], batchSize = 5): T[][] {
318323
}
319324
return batches;
320325
}
326+
327+
function getApplicationCreatedAt(applicationId: string): number | undefined {
328+
if (!/^app_[0-9A-Za-z]{27}$/.test(applicationId)) {
329+
return;
330+
}
331+
332+
const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
333+
let value = 0n;
334+
for (const character of applicationId.slice(4)) {
335+
value = value * 62n + BigInt(alphabet.indexOf(character));
336+
}
337+
if (value >= 1n << 160n) {
338+
return;
339+
}
340+
341+
const ksuidEpochSeconds = 1_400_000_000;
342+
return (Number(value >> 128n) + ksuidEpochSeconds) * 1000;
343+
}

‎integration/constants.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export const constants = {
8888
INTEGRATION_INSTANCE_KEYS: process.env.INTEGRATION_INSTANCE_KEYS,
8989
INTEGRATION_STAGING_INSTANCE_KEYS: process.env.INTEGRATION_STAGING_INSTANCE_KEYS,
9090
INTEGRATION_TEST_RUN_KEY: process.env.INTEGRATION_TEST_RUN_KEY,
91+
E2E_CLEANUP_STALE_APPLICATIONS: process.env.E2E_CLEANUP_STALE_APPLICATIONS === '1',
9192
CLERK_PLATFORM_API_KEY: process.env.CLERK_PLATFORM_API_KEY,
9293
} as const;
9394

0 commit comments

Comments
 (0)