@@ -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 ( ! / ^ a p p _ [ 0 - 9 A - Z a - 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+ }
0 commit comments