Skip to content

Commit

Permalink
feat: Added gatekeeper status interval to config (#500)
Browse files Browse the repository at this point in the history
* Added KC_GATEKEEPER_STATUS_INTERVAL

* Refactored checkDIDs

* Fixed unit test
  • Loading branch information
macterra authored Dec 20, 2024
1 parent db35216 commit 4dc6fb5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
- KC_GATEKEEPER_DB=${KC_GATEKEEPER_DB}
- KC_GATEKEEPER_REGISTRIES=${KC_GATEKEEPER_REGISTRIES}
- KC_GATEKEEPER_GC_INTERVAL=${KC_GATEKEEPER_GC_INTERVAL}
- KC_GATEKEEPER_STATUS_INTERVAL=${KC_GATEKEEPER_STATUS_INTERVAL}
- KC_MONGODB_URL=mongodb://mongodb:27017
- KC_REDIS_URL=redis://redis:6379
volumes:
Expand Down
13 changes: 12 additions & 1 deletion packages/gatekeeper/src/gatekeeper-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export async function checkDIDs(options = {}) {

const total = dids.length;
let n = 0;
let agents = 0;
let assets = 0;
let confirmed = 0;
let unconfirmed = 0;
let ephemeral = 0;
Expand All @@ -140,6 +142,14 @@ export async function checkDIDs(options = {}) {
console.log(`resolved ${n}/${total} ${did} OK`);
}

if (doc.mdip.type === 'agent') {
agents += 1;
}

if (doc.mdip.type === 'asset') {
assets += 1;
}

if (doc.didDocumentMetadata.confirmed) {
confirmed += 1;
}
Expand All @@ -165,7 +175,8 @@ export async function checkDIDs(options = {}) {
}
}

return { total, confirmed, unconfirmed, ephemeral, invalid, byRegistry, byVersion };
const byType = { agents, assets, confirmed, unconfirmed, ephemeral, invalid };
return { total, byType, byRegistry, byVersion };
}

export async function initRegistries(csvRegistries) {
Expand Down
1 change: 1 addition & 0 deletions sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ KC_GATEKEEPER_DB=json-cache
KC_GATEKEEPER_REGISTRIES=hyperswarm,TBTC,TFTC
KC_GATEKEEPER_PORT=4224
KC_GATEKEEPER_GC_INTERVAL=60
KC_GATEKEEPER_STATUS_INTERVAL=5

# Keymaster
KC_ENCRYPTED_PASSPHRASE=
Expand Down
1 change: 1 addition & 0 deletions services/gatekeeper/server/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const config = {
db: process.env.KC_GATEKEEPER_DB || 'redis',
registries: process.env.KC_GATEKEEPER_REGISTRIES,
gcInterval: process.env.KC_GATEKEEPER_GC_INTERVAL ? parseInt(process.env.KC_GATEKEEPER_GC_INTERVAL) : 15,
statusInterval: process.env.KC_GATEKEEPER_STATUS_INTERVAL ? parseInt(process.env.KC_GATEKEEPER_STATUS_INTERVAL) : 5,
};

export default config;
16 changes: 10 additions & 6 deletions services/gatekeeper/server/src/gatekeeper-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,14 @@ async function reportStatus() {
console.log(`DID Database (${config.db}):`);
console.log(` Total: ${status.dids.total}`);

console.log(` By type:`);
console.log(` Agents: ${status.dids.byType.agents}`);
console.log(` Assets: ${status.dids.byType.assets}`);
console.log(` Confirmed: ${status.dids.byType.confirmed}`);
console.log(` Unconfirmed: ${status.dids.byType.unconfirmed}`);
console.log(` Ephemeral: ${status.dids.byType.ephemeral}`);
console.log(` Invalid: ${status.dids.byType.invalid}`);

console.log(` By registry:`);
const registries = Object.keys(status.dids.byRegistry).sort();
for (let registry of registries) {
Expand All @@ -310,10 +318,6 @@ async function reportStatus() {
}
console.log(` 6+: ${status.dids.total - count}`);

console.log(` Confirmed: ${status.dids.confirmed}`);
console.log(` Unconfirmed: ${status.dids.unconfirmed}`);
console.log(` Ephemeral: ${status.dids.ephemeral}`);
console.log(` Invalid: ${status.dids.invalid}`);

console.log(`Memory Usage Report:`);
console.log(` RSS: ${formatBytes(status.memoryUsage.rss)} (Resident Set Size - total memory allocated for the process)`);
Expand Down Expand Up @@ -384,9 +388,9 @@ function formatBytes(bytes) {
}

async function main() {
console.log(`Starting Gatekeeper with a db (${config.db}) check...`);
console.log(`Starting KeychainMDIP Gatekeeper with a db (${config.db}) check...`);
await reportStatus();
setInterval(reportStatus, 60 * 1000);
setInterval(reportStatus, config.statusInterval * 60 * 1000);

console.log(`Starting DID garbage collection in ${config.gcInterval} minutes`);
setTimeout(gcLoop, config.gcInterval * 60 * 1000);
Expand Down
20 changes: 11 additions & 9 deletions tests/gatekeeper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2625,9 +2625,10 @@ describe('checkDIDs', () => {
const check = await gatekeeper.checkDIDs({ chatty: true });

expect(check.total).toBe(2);
expect(check.confirmed).toBe(2);
expect(check.ephemeral).toBe(1);
expect(check.invalid).toBe(0);
expect(check.byType.agents).toBe(1);
expect(check.byType.assets).toBe(1);
expect(check.byType.ephemeral).toBe(1);
expect(check.byType.invalid).toBe(0);
expect(check.byRegistry['local']).toBe(2);
expect(check.byVersion[1]).toBe(2);
});
Expand All @@ -2649,10 +2650,12 @@ describe('checkDIDs', () => {

expect(ok).toBe(true);
expect(check.total).toBe(2);
expect(check.confirmed).toBe(1);
expect(check.unconfirmed).toBe(1);
expect(check.ephemeral).toBe(0);
expect(check.invalid).toBe(0);
expect(check.byType.agents).toBe(1);
expect(check.byType.assets).toBe(1);
expect(check.byType.confirmed).toBe(1);
expect(check.byType.unconfirmed).toBe(1);
expect(check.byType.ephemeral).toBe(0);
expect(check.byType.invalid).toBe(0);
expect(check.byRegistry['hyperswarm']).toBe(2);
expect(check.byVersion[1]).toBe(1);
expect(check.byVersion[2]).toBe(1);
Expand All @@ -2673,8 +2676,7 @@ describe('checkDIDs', () => {
const check = await gatekeeper.checkDIDs({ chatty: true, dids });

expect(check.total).toBe(3);
expect(check.ephemeral).toBe(0);
expect(check.invalid).toBe(1);
expect(check.byType.invalid).toBe(1);
});

it('should reset a corrupted db', async () => {
Expand Down

0 comments on commit 4dc6fb5

Please sign in to comment.