1- import commandLineArgs from 'command-line-args'
2-
31import {
42 MEMBER_ORG_STINT_CHANGES_DATES_PREFIX ,
53 MEMBER_ORG_STINT_CHANGES_QUEUE ,
@@ -11,71 +9,28 @@ import { REDIS_CONFIG } from '@/conf'
119
1210const log = getServiceLogger ( )
1311
14- const options = [
15- {
16- name : 'confirm' ,
17- alias : 'c' ,
18- type : Boolean ,
19- description : 'Actually delete old hash keys. Defaults to dry-run.' ,
20- } ,
21- {
22- name : 'count' ,
23- type : Number ,
24- defaultValue : 500 ,
25- description : 'SCAN count hint.' ,
26- } ,
27- ]
28-
29- const parameters = commandLineArgs ( options )
30-
31- function memberIdFromDatesKey ( key : string ) : string {
32- return key . slice ( `${ MEMBER_ORG_STINT_CHANGES_DATES_PREFIX } :` . length )
33- }
34-
3512setImmediate ( async ( ) => {
36- const dryRun = ! parameters . confirm
37- const scanCount = parameters . count
3813 const redis = await getRedisClient ( REDIS_CONFIG , true )
39- const pattern = `${ MEMBER_ORG_STINT_CHANGES_DATES_PREFIX } :*`
40-
41- let scanned = 0
42- let hashKeys = 0
43- let deleted = 0
44- let cursor = 0
45-
46- log . info ( { dryRun, pattern, scanCount } , 'Removing old member organization stint hash keys.' )
14+ const prefixes = [ MEMBER_ORG_STINT_CHANGES_QUEUE , `${ MEMBER_ORG_STINT_CHANGES_DATES_PREFIX } :*` ]
4715
4816 try {
49- do {
50- const result = await redis . scan ( cursor , {
51- MATCH : pattern ,
52- COUNT : scanCount ,
53- } )
54-
55- cursor = Number ( result . cursor )
56- scanned += result . keys . length
17+ for ( const pattern of prefixes ) {
18+ let cursor = 0
19+ log . info ( { pattern } , 'Nuking keys matching pattern.' )
5720
58- for ( const key of result . keys ) {
59- const type = await redis . type ( key )
60- if ( type === 'hash' ) {
61- hashKeys ++
62- const memberId = memberIdFromDatesKey ( key )
21+ do {
22+ const result = await redis . scan ( cursor , { MATCH : pattern , COUNT : 1000 } )
23+ cursor = Number ( result . cursor )
6324
64- if ( dryRun ) {
65- log . info ( { key, memberId } , 'Would remove old hash key and queue member.' )
66- } else {
67- await redis . multi ( ) . del ( key ) . sRem ( MEMBER_ORG_STINT_CHANGES_QUEUE , memberId ) . exec ( )
68-
69- deleted ++
70- }
25+ if ( result . keys . length > 0 ) {
26+ await redis . del ( result . keys )
27+ log . info ( { count : result . keys . length } , 'Deleted batch of keys.' )
7128 }
72- }
73- } while ( cursor !== 0 )
29+ } while ( cursor !== 0 )
30+ }
7431
75- log . info ( { dryRun , scanned , hashKeys , deleted } , 'Finished removing old hash keys .')
32+ log . info ( 'Cleanup complete .')
7633 } finally {
7734 await stopClient ( redis )
7835 }
79-
80- process . exit ( 0 )
8136} )
0 commit comments