@@ -8,6 +8,8 @@ import { createClient } from "redis";
8
8
9
9
dotenv . config ( ) ;
10
10
11
+ const REDIS_DATACAP_ADDRESSES_SET = "datacap-addresses" ;
12
+
11
13
( async ( ) => {
12
14
while ( true ) {
13
15
const client = await createClient ( {
@@ -34,6 +36,7 @@ dotenv.config();
34
36
await Delay ( 1000 ) ;
35
37
}
36
38
}
39
+ let addresses : string [ ] = [ ] ;
37
40
for ( let request of approvedRequests ) {
38
41
if ( request . address ) {
39
42
const response = await axios . post ( "https://api.node.glif.io/" , {
@@ -42,26 +45,56 @@ dotenv.config();
42
45
params : [ `${ request . address } ` , null ] ,
43
46
id : `${ request . id } ` ,
44
47
} ) ;
45
- console . log ( response . data ) ;
46
48
let allocation = Number ( response . data . result ) ?? 0 ;
47
- if (
48
- Number ( await client . hGet ( request . address , "allocation" ) ) !==
49
- allocation
50
- ) {
49
+ let cachedAllocation = Number (
50
+ await client . hGet ( request . address , "allocation" )
51
+ ) ;
52
+ if ( cachedAllocation !== allocation ) {
51
53
await client . hSet ( request . address , {
52
54
allocation,
53
55
date : Date . now ( ) as number ,
54
56
} ) ;
55
- const value = await client . hGetAll ( request . address ) ;
56
- console . log ( value ) ;
57
+ console . log (
58
+ "Allocation updated for:" ,
59
+ request . address ,
60
+ " - before:" ,
61
+ cachedAllocation / 1024 ** 3 ,
62
+ "GB" ,
63
+ " - after:" ,
64
+ allocation / 1024 ** 3 ,
65
+ "GB" ,
66
+ "diff:" ,
67
+ ( allocation - cachedAllocation ) / 1024 ** 3 ,
68
+ "GB"
69
+ ) ;
57
70
} else {
58
71
console . log ( request . address , "- No change in allocation" ) ;
59
72
}
60
-
61
- // TODO: Check if application is stale
73
+ addresses . push ( request . address ) ;
62
74
}
63
75
}
76
+ // Update the list of addresses in redis
77
+ await client . sAdd ( REDIS_DATACAP_ADDRESSES_SET , addresses ) ;
78
+
79
+ // check for stale allocations
80
+ addresses = await client . sMembers ( REDIS_DATACAP_ADDRESSES_SET ) ;
81
+ let staleThreshold = Number ( process . env . ALLOCATION_STALE_THRESHOLD_DAYS ) ;
82
+ for ( let address of addresses ) {
83
+ let entry : { allocation : number ; date : number ; stale ?: string | null } =
84
+ await client . hGetAll ( address ) . then ( ( res ) => {
85
+ return {
86
+ allocation : Number ( res . allocation ) ,
87
+ date : Number ( res . date ) ,
88
+ stale : res . stale ,
89
+ } ;
90
+ } ) ;
91
+ if ( entry . stale ) continue ;
64
92
93
+ if ( Date . now ( ) - entry . date > staleThreshold * 24 * 60 * 60 * 1000 ) {
94
+ await client . hSet ( address , { stale : 1 } ) ;
95
+ console . log ( "Stale allocation removed for:" , address ) ;
96
+ }
97
+ }
65
98
await client . disconnect ( ) ;
66
99
await Delay ( 1000 * 60 * 5 ) ;
67
100
}
0 commit comments