1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-12 01:17:04 +02:00

feat: remove inactive applications (#9835)

This commit is contained in:
Mateusz Kwasniewski 2025-04-24 15:17:47 +02:00 committed by GitHub
parent bffaab5560
commit d24bcff404
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 69 additions and 1 deletions

View File

@ -449,4 +449,16 @@ export default class ClientApplicationsStore
}));
}
};
async removeInactiveApplications(): Promise<number> {
const rows = await this.db(TABLE)
.whereRaw("seen_at < now() - interval '30 days'")
.del();
if (rows > 0) {
this.logger.debug(`Deleted ${rows} applications`);
}
return rows;
}
}

View File

@ -298,6 +298,13 @@ export default class ClientInstanceService {
return this.clientInstanceStore.removeInstancesOlderThanTwoDays();
}
async removeInactiveApplications(): Promise<number> {
if (this.flagResolver.isEnabled('removeInactiveApplications')) {
return this.clientApplicationsStore.removeInactiveApplications();
}
return 0;
}
async getOutdatedSdks(): Promise<OutdatedSdksSchema['sdks']> {
const sdkApps = await this.clientInstanceStore.groupApplicationsBySdk();

View File

@ -78,6 +78,14 @@ export const scheduleServices = async (
'removeInstancesOlderThanTwoDays',
);
schedulerService.schedule(
clientInstanceService.removeInactiveApplications.bind(
clientInstanceService,
),
hoursToMilliseconds(24),
'removeInactiveApplications',
);
schedulerService.schedule(
clientInstanceService.bulkAdd.bind(clientInstanceService),
secondsToMilliseconds(5),

View File

@ -69,7 +69,8 @@ export type IFlagKey =
| 'newStrategyDropdown'
| 'flagsOverviewSearch'
| 'flagsReleaseManagementUI'
| 'cleanupReminder';
| 'cleanupReminder'
| 'removeInactiveApplications';
export type IFlags = Partial<{ [key in IFlagKey]: boolean | Variant }>;
@ -330,6 +331,10 @@ const flags: IFlags = {
process.env.UNLEASH_EXPERIMENTAL_CLEANUP_REMINDER,
false,
),
removeInactiveApplications: parseEnvVarBoolean(
process.env.UNLEASH_EXPERIMENTAL_REMOVE_INACTIVE_APPLICATIONS,
false,
),
};
export const defaultExperimentalOptions: IExperimentalOptions = {

View File

@ -44,4 +44,5 @@ export interface IClientApplicationsStore
getUnannounced(): Promise<IClientApplication[]>;
setUnannouncedToAnnounced(): Promise<IClientApplication[]>;
getApplicationOverview(appName: string): Promise<IApplicationOverview>;
removeInactiveApplications(): Promise<number>;
}

View File

@ -6,6 +6,7 @@ import type {
IUnleashStores,
} from '../../../lib/types';
import type { IClientApplication } from '../../../lib/types/stores/client-applications-store';
import { subDays } from 'date-fns';
let db: ITestDb;
let stores: IUnleashStores;
@ -175,3 +176,33 @@ test('Multi row merge also works', async () => {
expect(s!.icon).toBe('red');
});
});
test('Remove inactive applications', async () => {
const clientRegistration = {
appName: faker.internet.domainName(),
instanceId: faker.datatype.uuid(),
strategies: ['default'],
started: Date.now(),
interval: faker.datatype.number(),
sdkVersion: '3.11.2',
icon: '',
description: faker.company.catchPhrase(),
color: faker.internet.color(),
};
await clientApplicationsStore.upsert({
...clientRegistration,
lastSeen: subDays(Date.now(), 29),
});
const noRemovedItems =
await clientApplicationsStore.removeInactiveApplications();
expect(noRemovedItems).toBe(0);
await clientApplicationsStore.upsert({
...clientRegistration,
lastSeen: subDays(Date.now(), 30),
});
const removedItems =
await clientApplicationsStore.removeInactiveApplications();
expect(removedItems).toBe(1);
});

View File

@ -82,4 +82,8 @@ export default class FakeClientApplicationsStore
getApplicationOverview(appName: string): Promise<IApplicationOverview> {
throw new Error('Method not implemented.');
}
async removeInactiveApplications(): Promise<number> {
return 0;
}
}