Skip to content

Commit 09ea5a0

Browse files
authoredFeb 22, 2024··
Merge pull request #12 from kacperzuk-neti/feat/redis
feat: allocation caching & stale check
2 parents 0a71528 + 32d5e0c commit 09ea5a0

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed
 

‎.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ OWNER=filecoin-project
33
REPO=filecoin-plus-large-datasets
44
COMMENT_AUTHOR=large-datacap-requests[bot]
55
REDIS_URL=redis://localhost:6379/0
6+
ALLOCATION_STALE_THRESHOLD_DAYS=14

‎App.ts

+42-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { createClient } from "redis";
88

99
dotenv.config();
1010

11+
const REDIS_DATACAP_ADDRESSES_SET = "datacap-addresses";
12+
1113
(async () => {
1214
while (true) {
1315
const client = await createClient({
@@ -34,6 +36,7 @@ dotenv.config();
3436
await Delay(1000);
3537
}
3638
}
39+
let addresses: string[] = [];
3740
for (let request of approvedRequests) {
3841
if (request.address) {
3942
const response = await axios.post("https://api.node.glif.io/", {
@@ -42,26 +45,56 @@ dotenv.config();
4245
params: [`${request.address}`, null],
4346
id: `${request.id}`,
4447
});
45-
console.log(response.data);
4648
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) {
5153
await client.hSet(request.address, {
5254
allocation,
5355
date: Date.now() as number,
5456
});
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+
);
5770
} else {
5871
console.log(request.address, "- No change in allocation");
5972
}
60-
61-
// TODO: Check if application is stale
73+
addresses.push(request.address);
6274
}
6375
}
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;
6492

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+
}
6598
await client.disconnect();
6699
await Delay(1000 * 60 * 5);
67100
}

‎issueProcessor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export const processIssue = async (
4141
}
4242
}
4343
}
44-
console.log(dca);
4544
}
45+
console.log(dca);
4646
return dca;
4747
} catch (e) {
4848
console.error(e);

0 commit comments

Comments
 (0)
Please sign in to comment.