Skip to content

Commit

Permalink
Shuffle sites to destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Mar 20, 2024
1 parent 04e364e commit e9ef72e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion jest-helpers/generateNewDashboardClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const baseConfigOptions: Partial<ClientConfigOptions> = {
fetchFn,
};

function shuffleArray<T>(source: T[]) {
export function shuffleArray<T>(source: T[]) {
const array = [...source];

for (let i = array.length - 1; i > 0; i--) {
Expand Down
37 changes: 21 additions & 16 deletions jest-helpers/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ConcurrentPromiseQueue } from 'concurrent-promise-queue';
import 'dotenv/config';
import { ApiError } from '../packages/dashboard-client';
import { generateNewDashboardClient } from './generateNewDashboardClient';
import {
generateNewDashboardClient,
shuffleArray,
} from './generateNewDashboardClient';

function isOldEnough(isoDatetime: string) {
const date = new Date(isoDatetime);
Expand Down Expand Up @@ -37,25 +40,27 @@ export default async () => {
console.log(`Deleting ${siteIds.length} projects...`);

const queue = new ConcurrentPromiseQueue({
maxNumberOfConcurrentPromises: 10,
maxNumberOfConcurrentPromises: 20,
});

await Promise.all(
siteIds.map((id) =>
queue.addPromise(async () => {
try {
await client.sites.destroy(id);
} catch (e) {
if (e instanceof ApiError && e.findError('NOT_FOUND')) {
// Other processes might have already deleted the project
return;
}
shuffleArray(
siteIds.map((id) =>
queue.addPromise(async () => {
try {
await client.sites.destroy(id);
} catch (e) {
if (e instanceof ApiError && e.findError('NOT_FOUND')) {
// Other processes might have already deleted the project
return;
}

throw e;
} finally {
process.stdout.write('.');
}
}),
throw e;
} finally {
process.stdout.write('.');
}
}),
),
),
);
};

0 comments on commit e9ef72e

Please sign in to comment.