Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
feat: add active debuggee support (#1121)
Browse files Browse the repository at this point in the history
* wip: add timestamps to register call

* fix: update the names of the timestamp fields

* wip: working on tests; there are issues

* wip: restructured and tests are passing

* wip: registration complete

* feat: add active debuggee support

* fix: relax the test constraints for marking active debuggees

* address PR feedback
  • Loading branch information
mctavish committed Dec 9, 2022
1 parent 42ee4a7 commit a421509
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 86 deletions.
64 changes: 54 additions & 10 deletions src/agent/firebase-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class FirebaseController implements Controller {
debuggeeId?: string;
bpRef?: firebase.database.Reference;

markActiveInterval: ReturnType<typeof setInterval> | undefined;
markActivePeriodMsec: number = 60 * 60 * 1000; // 1 hour in ms.

/**
* Connects to the Firebase database.
*
Expand Down Expand Up @@ -135,7 +138,10 @@ export class FirebaseController implements Controller {
}

/**
* Register to the API (implementation)
* Register to the API (implementation).
*
* Writes an initial record to the database if it is not yet present.
* Otherwise only updates the last active timestamp.
*
* @param {!function(?Error,Object=)} callback
* @private
Expand Down Expand Up @@ -168,15 +174,29 @@ export class FirebaseController implements Controller {
this.debuggeeId = `d-${debuggeeHash.substring(0, 8)}`;
debuggee.id = this.debuggeeId;

const debuggeeRef = this.db.ref(`cdbg/debuggees/${this.debuggeeId}`);
debuggeeRef.set(debuggee as {}, err => {
if (err) {
callback(err);
} else {
const agentId = 'unsupported';
callback(null, {debuggee, agentId});
}
});
const agentId = 'unsupported';
// Test presence using the registration time. This moves less data.
const presenceRef = this.db.ref(
`cdbg/debuggees/${this.debuggeeId}/registrationTimeUnixMsec`
);
presenceRef
.get()
.then(presenceSnapshot => {
if (presenceSnapshot.exists()) {
return this.markDebuggeeActive();
} else {
const ref = this.db.ref(`cdbg/debuggees/${this.debuggeeId}`);
return ref.set({
registrationTimeUnixMsec: {'.sv': 'timestamp'},
lastUpdateTimeUnixMsec: {'.sv': 'timestamp'},
...debuggee,
});
}
})
.then(
() => callback(null, {debuggee, agentId}),
err => callback(err)
);
}

/**
Expand Down Expand Up @@ -300,6 +320,26 @@ export class FirebaseController implements Controller {
callback(e, []);
}
);

this.startMarkingDebuggeeActive();
}

startMarkingDebuggeeActive() {
debuglog(`starting to mark every ${this.markActivePeriodMsec} ms`);
this.markActiveInterval = setInterval(() => {
this.markDebuggeeActive();
}, this.markActivePeriodMsec);
}

/**
* Marks a debuggee as active by prompting the server to update the
* lastUpdateTimeUnixMsec to server time.
*/
async markDebuggeeActive(): Promise<void> {
const ref = this.db.ref(
`cdbg/debuggees/${this.debuggeeId}/lastUpdateTimeUnixMsec`
);
await ref.set({'.sv': 'timestamp'});
}

stop(): void {
Expand All @@ -312,5 +352,9 @@ export class FirebaseController implements Controller {
} catch (err) {
debuglog(`failed to tear down firebase app: ${err})`);
}
if (this.markActiveInterval) {
clearInterval(this.markActiveInterval);
this.markActiveInterval = undefined;
}
}
}

0 comments on commit a421509

Please sign in to comment.