-
Notifications
You must be signed in to change notification settings - Fork 15
Add Heartbeat #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8e790bc
WIP: heartbeat
jeanp413 3f4f41d
Fixes
jeanp413 de87296
Fix
jeanp413 02ddc51
TODO: Clean up this
jeanp413 84dd919
Fix
jeanp413 0dd2b15
:lipstick:
jeanp413 c67351b
Feedback
jeanp413 4bd0a46
:lipstick:
jeanp413 4740630
Check if workspace is running before sending heartbeat
jeanp413 bdc8d2c
More feedback
jeanp413 2165d28
Add check for gitpod-remote version
jeanp413 cb87d52
:broom:
jeanp413 ffa578d
Use `__gitpod.cancelGitpodRemoteHeartbeat` command
jeanp413 80666de
Merge remote-tracking branch 'origin/master' into jp/heartbeat
jeanp413 232ea39
Merge remote-tracking branch 'origin/master' into jp/heartbeat
jeanp413 dfa0aca
Fix
jeanp413 0cad7fb
Add small delay
jeanp413 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Gitpod. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { Disposable } from './common/dispose'; | ||
import Log from './common/logger'; | ||
import { withServerApi } from './internalApi'; | ||
import TelemetryReporter from './telemetryReporter'; | ||
|
||
export class HeartbeatManager extends Disposable { | ||
|
||
static HEARTBEAT_INTERVAL = 30000; | ||
|
||
private lastActivity = new Date().getTime(); | ||
private isWorkspaceRunning = true; | ||
private heartBeatHandle: NodeJS.Timer | undefined; | ||
|
||
constructor( | ||
readonly gitpodHost: string, | ||
readonly workspaceId: string, | ||
readonly instanceId: string, | ||
private readonly accessToken: string, | ||
private readonly logger: Log, | ||
private readonly telemetry: TelemetryReporter | ||
) { | ||
super(); | ||
|
||
this._register(vscode.window.onDidChangeActiveTextEditor(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidChangeVisibleTextEditors(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidChangeTextEditorSelection(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidChangeTextEditorVisibleRanges(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidChangeTextEditorOptions(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidChangeTextEditorViewColumn(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidChangeActiveTerminal(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidOpenTerminal(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidCloseTerminal(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidChangeTerminalState(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidChangeWindowState(this.updateLastActivitiy, this)); | ||
this._register(vscode.window.onDidChangeActiveColorTheme(this.updateLastActivitiy, this)); | ||
this._register(vscode.authentication.onDidChangeSessions(this.updateLastActivitiy, this)); | ||
this._register(vscode.debug.onDidChangeActiveDebugSession(this.updateLastActivitiy, this)); | ||
this._register(vscode.debug.onDidStartDebugSession(this.updateLastActivitiy, this)); | ||
this._register(vscode.debug.onDidReceiveDebugSessionCustomEvent(this.updateLastActivitiy, this)); | ||
this._register(vscode.debug.onDidTerminateDebugSession(this.updateLastActivitiy, this)); | ||
this._register(vscode.debug.onDidChangeBreakpoints(this.updateLastActivitiy, this)); | ||
this._register(vscode.extensions.onDidChange(this.updateLastActivitiy, this)); | ||
this._register(vscode.languages.onDidChangeDiagnostics(this.updateLastActivitiy, this)); | ||
this._register(vscode.tasks.onDidStartTask(this.updateLastActivitiy, this)); | ||
this._register(vscode.tasks.onDidStartTaskProcess(this.updateLastActivitiy, this)); | ||
this._register(vscode.tasks.onDidEndTask(this.updateLastActivitiy, this)); | ||
this._register(vscode.tasks.onDidEndTaskProcess(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidChangeWorkspaceFolders(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidOpenTextDocument(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidCloseTextDocument(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidChangeTextDocument(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidSaveTextDocument(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidChangeNotebookDocument(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidSaveNotebookDocument(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidOpenNotebookDocument(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidCloseNotebookDocument(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onWillCreateFiles(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidCreateFiles(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onWillDeleteFiles(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidDeleteFiles(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onWillRenameFiles(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidRenameFiles(this.updateLastActivitiy, this)); | ||
this._register(vscode.workspace.onDidChangeConfiguration(this.updateLastActivitiy, this)); | ||
this._register(vscode.languages.registerHoverProvider('*', { | ||
provideHover: () => { | ||
this.updateLastActivitiy(); | ||
return null; | ||
} | ||
})); | ||
|
||
this.logger.trace(`Heartbeat manager for workspace ${workspaceId} (${instanceId}) - ${gitpodHost} started`); | ||
|
||
// Start heatbeating interval | ||
this.sendHeartBeat(); | ||
this.heartBeatHandle = setInterval(() => { | ||
jeanp413 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Add an additional random value between 5 and 15 seconds. See https://github.com/gitpod-io/gitpod/pull/5613 | ||
const randomInterval = Math.floor(Math.random() * (15000 - 5000 + 1)) + 5000; | ||
if (this.lastActivity + HeartbeatManager.HEARTBEAT_INTERVAL + randomInterval < new Date().getTime()) { | ||
// no activity, no heartbeat | ||
return; | ||
} | ||
|
||
this.sendHeartBeat(); | ||
}, HeartbeatManager.HEARTBEAT_INTERVAL); | ||
} | ||
|
||
private updateLastActivitiy() { | ||
this.lastActivity = new Date().getTime(); | ||
} | ||
|
||
private async sendHeartBeat(wasClosed?: true) { | ||
const suffix = wasClosed ? 'closed heartbeat' : 'heartbeat'; | ||
try { | ||
await withServerApi(this.accessToken, this.gitpodHost, async service => { | ||
const workspaceInfo = await service.server.getWorkspace(this.workspaceId); | ||
this.isWorkspaceRunning = workspaceInfo.latestInstance?.status?.phase === 'running' && workspaceInfo.latestInstance?.id === this.instanceId; | ||
if (this.isWorkspaceRunning) { | ||
await service.server.sendHeartBeat({ instanceId: this.instanceId, wasClosed }); | ||
if (wasClosed) { | ||
this.telemetry.sendTelemetryEvent('ide_close_signal', { workspaceId: this.workspaceId, instanceId: this.instanceId, gitpodHost: this.gitpodHost, clientKind: 'vscode' }); | ||
this.logger.trace('send ' + suffix); | ||
} | ||
} else { | ||
this.stopHeartbeat(); | ||
} | ||
}, this.logger); | ||
} catch (err) { | ||
this.logger.error(`failed to send ${suffix}:`, err); | ||
} | ||
} | ||
|
||
private stopHeartbeat() { | ||
if (this.heartBeatHandle) { | ||
clearInterval(this.heartBeatHandle); | ||
this.heartBeatHandle = undefined; | ||
} | ||
} | ||
|
||
public override async dispose(): Promise<void> { | ||
this.stopHeartbeat(); | ||
if (this.isWorkspaceRunning) { | ||
await this.sendHeartBeat(true); | ||
} | ||
super.dispose(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.