Skip to content

Commit a6ded54

Browse files
authored
fix: Report status for empty db (#503)
* Include DID stats only when available * keymaster waits for gatekeeper to sync
1 parent 8f9ec02 commit a6ded54

2 files changed

Lines changed: 49 additions & 34 deletions

File tree

services/gatekeeper/server/src/gatekeeper-api.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -295,29 +295,30 @@ async function reportStatus() {
295295
console.log(`DID Database (${config.db}):`);
296296
console.log(` Total: ${status.dids.total}`);
297297

298-
console.log(` By type:`);
299-
console.log(` Agents: ${status.dids.byType.agents}`);
300-
console.log(` Assets: ${status.dids.byType.assets}`);
301-
console.log(` Confirmed: ${status.dids.byType.confirmed}`);
302-
console.log(` Unconfirmed: ${status.dids.byType.unconfirmed}`);
303-
console.log(` Ephemeral: ${status.dids.byType.ephemeral}`);
304-
console.log(` Invalid: ${status.dids.byType.invalid}`);
305-
306-
console.log(` By registry:`);
307-
const registries = Object.keys(status.dids.byRegistry).sort();
308-
for (let registry of registries) {
309-
console.log(` ${registry}: ${status.dids.byRegistry[registry]}`);
310-
}
298+
if (status.dids.total > 0) {
299+
console.log(` By type:`);
300+
console.log(` Agents: ${status.dids.byType.agents}`);
301+
console.log(` Assets: ${status.dids.byType.assets}`);
302+
console.log(` Confirmed: ${status.dids.byType.confirmed}`);
303+
console.log(` Unconfirmed: ${status.dids.byType.unconfirmed}`);
304+
console.log(` Ephemeral: ${status.dids.byType.ephemeral}`);
305+
console.log(` Invalid: ${status.dids.byType.invalid}`);
306+
307+
console.log(` By registry:`);
308+
const registries = Object.keys(status.dids.byRegistry).sort();
309+
for (let registry of registries) {
310+
console.log(` ${registry}: ${status.dids.byRegistry[registry]}`);
311+
}
311312

312-
console.log(` By version:`);
313-
let count = 0;
314-
for (let version of [1, 2, 3, 4, 5]) {
315-
const num = status.dids.byVersion[version];
316-
console.log(` ${version}: ${num}`);
317-
count += num;
313+
console.log(` By version:`);
314+
let count = 0;
315+
for (let version of [1, 2, 3, 4, 5]) {
316+
const num = status.dids.byVersion[version] || 0;
317+
console.log(` ${version}: ${num}`);
318+
count += num;
319+
}
320+
console.log(` 6+: ${status.dids.total - count}`);
318321
}
319-
console.log(` 6+: ${status.dids.total - count}`);
320-
321322

322323
console.log(`Memory Usage Report:`);
323324
console.log(` RSS: ${formatBytes(status.memoryUsage.rss)} (Resident Set Size - total memory allocated for the process)`);

services/keymaster/server/src/keymaster-api.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,31 @@ process.on('unhandledRejection', (reason, promise) => {
681681
//console.error('Unhandled rejection caught');
682682
});
683683

684+
async function waitForCurrentId() {
685+
let isReady = false;
686+
const currentId = await keymaster.getCurrentId();
687+
688+
if (!currentId) {
689+
return;
690+
}
691+
692+
while (!isReady) {
693+
try {
694+
console.log(`Resolving current ID: ${currentId}`);
695+
const doc = await keymaster.resolveDID(currentId);
696+
console.log(JSON.stringify(doc, null, 4));
697+
isReady = true;
698+
}
699+
catch {
700+
console.log(`Waiting for gatekeeper to sync...`);
701+
}
702+
703+
if (!isReady) {
704+
await new Promise(resolve => setTimeout(resolve, 10000));
705+
}
706+
}
707+
}
708+
684709
const port = config.keymasterPort;
685710

686711
app.listen(port, async () => {
@@ -707,17 +732,6 @@ app.listen(port, async () => {
707732
await keymaster.start({ gatekeeper, wallet, cipher });
708733
console.log(`keymaster server running on port ${port}`);
709734

710-
try {
711-
const currentId = await keymaster.getCurrentId();
712-
713-
if (currentId) {
714-
console.log(`current ID: ${currentId}`);
715-
const doc = await keymaster.resolveId();
716-
console.log(JSON.stringify(doc, null, 4));
717-
}
718-
serverReady = true;
719-
}
720-
catch (error) {
721-
console.log(error);
722-
}
735+
await waitForCurrentId();
736+
serverReady = true;
723737
});

0 commit comments

Comments
 (0)