Skip to content

bug: last_used_at not updated when "activity" is sent via API /report-stats #11986

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

Closed
ericpaulsen opened this issue Feb 1, 2024 · 12 comments
Closed
Assignees
Labels
api Area: HTTP API cli Area: CLI

Comments

@ericpaulsen
Copy link
Member

customers are looking to send an activity "ping" to workspaces to keep them from auto-shutting off. we have the below API call documented, but this does not update the workspace last_used_at time (retrieved via /api/v2/users/{user}/workspace/{workspace-name}):

#!/bin/bash
# Send workspace activity as long as the job is still running

while true
do
  if pgrep -f "my_training_script.py" > /dev/null
  then
    curl -X POST "https://coder.example.com/api/v2/workspaceagents/me/report-stats" \
    -H "Coder-Session-Token: $CODER_AGENT_TOKEN" \
    -d '{
      "connection_count": 1
    }'

    # Sleep for 30 minutes (1800 seconds) if the job is running
    sleep 1800
  else
    # Sleep for 1 minute (60 seconds) if the job is not running
    sleep 60
  fi
done
@ericpaulsen ericpaulsen added api Area: HTTP API bug labels Feb 1, 2024
@johnstcn
Copy link
Member

johnstcn commented Feb 2, 2024

I think this behaviour is as expected, see

// An empty stat means it's just looking for the report interval.
if req.ConnectionsByProto == nil {
httpapi.Write(ctx, rw, http.StatusOK, agentsdk.StatsResponse{
ReportInterval: api.AgentStatsRefreshInterval,
})
return
}
. A possible fix here would be to add something like connections_by_proto: {"ssh": 1} to the request payload, but this would essentially mean inserting bogus agent stats as the rest of the fields would all be zero valued.

If the goal here is to extend the workspace autostop time, an alternative would be to use the putWorkspaceTTL endpoint directly, which is the endpoint that gets hit when you click the [+] button in the UI. Example:

curl -XPUT 'https://coder.example.com/api/v2/workspaces/{workspace_id}/extend' -H "Coder-Session-Token: $(cat ~/.config/coderv2/session)" --data-raw "{\"deadline\":\"$(date --utc -d '+1 hour' +%Y-%m-%dT%H:%M:%SZ)\"}"

Note that this endpoint requires a session token and not just an agent token.

@johnstcn johnstcn added cli Area: CLI and removed bug labels Feb 2, 2024
@johnstcn johnstcn self-assigned this Feb 2, 2024
@mattlqx
Copy link

mattlqx commented Feb 2, 2024

It's very desirable for the agent token to be able to bump the activity so that user login is not required to accomplish this, as actual usage is in a crontab.

Could the report-stats endpoint accept the PATCH method and then would mean only update the keys defined in the body? So that connections_by_proto: {"ssh": 1} would leave the other connections untouched?

@johnstcn
Copy link
Member

johnstcn commented Feb 2, 2024

@mattlqx You don't need a human to interact with the workspace in order for that to work.

You can retrieve a valid session token for the workspace owner using data.coder_workspace.XXX.owner_session_token (link). Similarly, if you need the workspace ID, this is accessible via data.coder_workspace.XXX.id.

Does that address your use case?

@mattlqx
Copy link

mattlqx commented Feb 2, 2024

That helps, I'll give it a shot.

@mattlqx
Copy link

mattlqx commented Feb 3, 2024

/api/v2/workspaces/{workspace_id}/extend could work but would require multiple API calls to get the current stop time to not set the value lower than what is already set. It also doesn't help track activity to get that Last Used display to be more accurate.

It would be much better if report-stats could accept partial updates.

@johnstcn
Copy link
Member

johnstcn commented Feb 6, 2024

@mattlqx Thanks, that's good feedback. We do have some plans to rework how our workspace scheduling (see: #11503)

@strike
Copy link
Contributor

strike commented Feb 12, 2024

I'm not sure if this is the right place to post, but I wanted to add it here.

We also need similar functionality for dynamically extending the workspace runtime during use. We have scenarios like:

  1. Direct external connections to the workspace, without using Tailscale.
  2. Running processes (by name).
  3. High CPU load.
    It would be great if these could be taken into account.

@johnstcn
Copy link
Member

Would the ability to specify a positive delta to /extend cover this use case?

e.g.

curl -XPUT 'https://coder.example.com/api/v2/workspaces/{workspace_id}/extend' \ 
  -H "Coder-Session-Token: $(cat ~/.config/coderv2/session)" \
  --data-raw "{\"delta\":\"1h23m45s"}"

@strike
Copy link
Contributor

strike commented Feb 13, 2024

I believe a simple endpoint that updates variable like last_human_used_at would be beneficial. Then, connect the shutdown timing to last_human_used_at.

It seems that basing shutdowns on user connections was not the best approach (PR).

@bamhm182
Copy link
Contributor

bamhm182 commented Feb 19, 2024

Just here to say that I too would like the ability to update the last_used_at timestamp using the Agent Token. My use case is that I have been using Coder to spin up GUI-based VMs, which use external coder_apps. The method in the docs shown in the original post would be ideal, but I can also confirm it doesn't work.

The following curl works to update the token, but it's suboptimal for the reasons listed above. I'm going to use it for now, but I'd love to have a proper way to do this.

curl -X POST "${CODER_URL}/api/v2/workspaceagents/me/report-stats" -H "Coder-Session-Token: ${CODER_AGENT_TOKEN}" -d '{"connections_by_proto":{"ssh":1},"session_count_ssh":1}'

@stirby stirby mentioned this issue Mar 1, 2024
24 tasks
@johnstcn
Copy link
Member

johnstcn commented Mar 21, 2024

#12659 introduces an API endpoint POST /api/v2/workspaces/{workspace_id}/usage which allows directly updating the last_used_at timestamp of a workspace. It's been merged to main and should end up in the next release cycle.

Edit on Sep 9 2024: As of Coder v2.13.0, the /usage endpoint does not prevent the workspace from being automatically stopped unless the workspace-usage experiment is enabled. This experimental behaviour will become the default in a future release. If you wish to prevent a given workspace from being automatically stopped, please use the /extend endpoint to update the deadline for the given workspace. The coder schedule override-stop command can also be used to accomplish this.

@johnstcn
Copy link
Member

johnstcn commented Apr 9, 2024

The above was included in v2.10.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Area: HTTP API cli Area: CLI
Projects
None yet
Development

No branches or pull requests

5 participants