Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/frontend/src/ide/heart-beat.ts
2501 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { DisposableCollection, Disposable } from "@gitpod/gitpod-protocol/lib/util/disposable";
8
import { FrontendDashboardServiceClient } from "../shared/frontend-dashboard-service";
9
10
let lastActivity = 0;
11
const updateLastActivitiy = () => {
12
lastActivity = new Date().getTime();
13
};
14
export const track = (w: Window) => {
15
w.document.addEventListener("mousemove", updateLastActivitiy, { capture: true });
16
w.document.addEventListener("keydown", updateLastActivitiy, { capture: true });
17
};
18
19
let toCancel: DisposableCollection | undefined;
20
export function schedule(frontendDashboardServiceClient: FrontendDashboardServiceClient): void {
21
if (toCancel) {
22
return;
23
}
24
toCancel = new DisposableCollection();
25
const sendHeartBeat = async (wasClosed?: true) => {
26
try {
27
frontendDashboardServiceClient.activeHeartbeat(); // wasClosed
28
if (wasClosed) {
29
frontendDashboardServiceClient.trackEvent({
30
event: "ide_close_signal",
31
properties: {
32
clientKind: "supervisor-frontend",
33
},
34
});
35
}
36
} catch (err) {
37
console.error("Failed to send hearbeat:", err);
38
}
39
};
40
sendHeartBeat();
41
42
let activityInterval = 30000;
43
const intervalHandle = setInterval(() => {
44
// add an additional random value between 5 and 15 seconds
45
const randomInterval = Math.floor(Math.random() * (15000 - 5000 + 1)) + 5000;
46
if (lastActivity + activityInterval + randomInterval < new Date().getTime()) {
47
// no activity, no heartbeat
48
return;
49
}
50
sendHeartBeat();
51
}, activityInterval);
52
toCancel.push(Disposable.create(() => clearInterval(intervalHandle)));
53
}
54
55
export const cancel = () => {
56
if (toCancel) {
57
toCancel.dispose();
58
toCancel = undefined;
59
}
60
};
61
62