-
Notifications
You must be signed in to change notification settings - Fork 5.9k
code-server heartbeat logic #1274
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
Comments
A little dig on #1115 will let you know how it works. |
@sr229 I had a look at this. However I am not able to gather under what conditions is the heartbeat function called.
|
We don't have the ability to send that serverside. Even then, @microsoft is more than concerned for this rather than us. |
If there have been any HTTP requests in the past minute or if there are
any active web sockets then the user is considered active.
An active web socket is one that is connected even if there is no
activity on it. So this includes idle users that just have the tab open
(even if it's in the background).
If the user closes the tab or their network connection dies then the
heartbeat will stop until they connect again.
There's currently no way to detect if the user is idle. I was thinking
about capturing idle data on the client-side (probably a timer based on
mouse and keyboard activity) then sending that through the socket to the
server but it's not implemented yet. Maybe we could show an "are you
still there?" message as part of that.
|
Form my experience, while the browser has codeserver window open and the application is running (some browsers will stop the application) then the heartbeat file will update. In other words while the user has it open. We terminate the docker environment code-server is running in, when the user had not performed a control operation (we have such operations touch heart beat file to simply matters) , or heartbeat has not been updated in more than 2 hours. |
@code-asher Awesome! Thank's for your response. I am happy to contribute a PR for if you could let me know where to get started from. Thanks :) |
We don't need to send anything to the server, just disconnect the WebSocket if the user has been idle for an excessive amount time. Opening a new issue. |
Hi @code-asher - are you still looking for a feature for the "are you still there message"? (since we use it today to clean up resources - pods in kubernetes) This would be really useful to us and if the community also wants it I would be happy to work on this. |
Yeah, I'd be open to merging a feature like that. I think we initially decided not to because users will usually disconnect naturally (close the tab, close the browser, computer goes to sleep, I think even in some cases if the browser tab isn't focused it'll close the websocket), but I'm not sure how reliable that all is so a dedicated feature might be helpful. |
On start up I usually just try a curl request, and see if I get a 500 or a login redirection, looping until I get a response before reporting to the user they can proceed. |
@code-asher any suggestions on approach. Here's what I am thinking:
Any other suggestions/approach that you can share? |
I don't think we have anything specifically for user activity so we'd need to add something. My basic idea is:
The first three steps can all be done in |
@code-asher For user idle detection, there's a Idle Detection API in chrome: https://web.dev/idle-detection/ Maybe you can looking at some polyfills etc. |
Thanks for the tip! This looks like exactly what we'd need.
|
Is there any chance that you are exposing the api which updates the heartbeat file? I want to see the timestamp of heartbeat file for my usecase. Do you have any api for this? |
Basically I have a shell script which loops over each of the users currently running a docker instance with code-server. Basically I get a list of users, and how long they have been 'idle', as well as other information, like on which 'node' there docker instance is running, and what prepared docker image they are running (for different software development environments). From this I do the following.
These stats are logged into system logs (remote) so that I can now generate some usage graphs (splunk), or run other scripts to report on numbers of unique users using the system per day. So while the scripts actions are fairly simple (loop of logged in users reported 'idleness' from heartbeat files), it does quite a lot with that information. Getting a time from a heartbeat file (perl gets file times in seconds since time perl was run)... # Constant
INFINITE=99999999
file_timestamp() { # get the timestamp of a file.
local log="$1"
if [ -e "$log" ]; then
perl -e '
$last=(stat( "'"$log"'" ))[9];
print $^T - $last, " ", $last, "\n";
'
else
echo $INFINITE 0
fi
}
while read user image; do
#...
read delta time <<<$(file_timestamp "path_to_users_heartbeatfile")
#...
done < <( docker service ls --format "{{.Name}} {{.Image}}" ) SO I call I hope this helps... |
Update.. Added code to my idle monitor/timeout script, to pull web requests from the docker 'ingress' server for the user container. This lets me capture user interaction with docker container for things like, the 'apache webserver' or any 'NodeJS' servers the user is developing. But more importantly it works for jupyter notebook, who's idle reporting API does not seem to capture file editing, only notebook/kernel running. However web monitoring does not work completely for idle monitoring code-server. Looks as if code server holds a web connection open, at least for things like a CLI terminal, and as no web requests are being finished while user is using the code-server terminal. As such checking the code-server heartbeat is a MUST, as part of testing for idle timeout for the users docker service. |
I want to ask if there is such a situation: I start a very long task (such as ai-related training) through the code-server's termial, and the computer may go to sleep. Within a minute I have no http requests made, and the websocket may also disconnect, does that code-server stop? |
Continue to ask a question: Are the heartbeat and automatic stop functions only suitable for serverless? I understand that the heartbeat is to automatically stop the node service when the code-server has not been used for a long time, and tell the container service in some way that the current pod can be destroyed, so as to save costs. However, if I deploy the code-server on a server (which will not stop), do I actually not use the heartbeat function? If I don't have to worry about memory consumption for the time being. Because whether the code-server is stopped or not, my server is always running and charging. |
code-server itself will never stop on its own. Some folks have implemented external services that monitor the heartbeat and stop code-server, but code-server has no automatic stop of its own. However, code-server will kill disconnected terminals after a time, so your long-running process will be killed if left disconnected too long. You may want to run it in something like tmux or screen. If you are not worried about resource consumption, I would say there is no reason to stop code-server. |
Description
I am running code-server with multi-tenancy and would like to know when to kill the users containers after a period of inactivity. I am using the heartbeat file that code-server touch's every minute for this.
I would like to know on what basis does code-server decides to touch the heartbeat, i.e what are the conditions that code-server decide as activity and inactivity.
The text was updated successfully, but these errors were encountered: