Skip to content

Filter query: has-agent connecting, connected, disconnected, timeout #5145

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 30 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Renamings
  • Loading branch information
mtojek committed Nov 23, 2022
commit 81d9fef82b1e950fbfa460ec5c0f52738dbe55de
2 changes: 1 addition & 1 deletion coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ func (q *fakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
hasAgentMatched = !wa.FirstConnectedAt.Valid
case "disconnected":
hasAgentMatched = (wa.DisconnectedAt.Valid && wa.DisconnectedAt.Time.After(wa.LastConnectedAt.Time)) ||
(wa.LastConnectedAt.Valid && wa.LastConnectedAt.Time.Add(time.Duration(arg.AgentInactiveDisconnectTimeout)*time.Second).Before(database.Now()))
(wa.LastConnectedAt.Valid && wa.LastConnectedAt.Time.Add(time.Duration(arg.AgentInactiveDisconnectTimeoutSeconds)*time.Second).Before(database.Now()))
case "timeout":
hasAgentMatched = !wa.FirstConnectedAt.Valid &&
wa.CreatedAt.Add(time.Duration(wa.ConnectionTimeoutSeconds)*time.Second).Before(database.Now())
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
pq.Array(arg.TemplateIds),
arg.Name,
arg.HasAgent,
arg.AgentInactiveDisconnectTimeout,
arg.AgentInactiveDisconnectTimeoutSeconds,
arg.Offset,
arg.Limit,
)
Expand Down
52 changes: 26 additions & 26 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ LEFT JOIN LATERAL (
provisioner_jobs.canceled_at,
provisioner_jobs.completed_at,
provisioner_jobs.error,
workspace_agents.created_at,
workspace_agents.disconnected_at,
workspace_agents.first_connected_at,
workspace_agents.last_connected_at,
workspace_agents.connection_timeout_seconds
workspace_agents.created_at AS agent_created_at,
workspace_agents.disconnected_at AS agent_disconnected_at,
workspace_agents.first_connected_at AS agent_first_connected_at,
workspace_agents.last_connected_at AS agent_last_connected_at,
workspace_agents.connection_timeout_seconds AS agent_connection_timeout_seconds
FROM
workspace_builds
LEFT JOIN
Expand All @@ -75,8 +75,8 @@ LEFT JOIN LATERAL (
workspace_builds.workspace_id = workspaces.id
ORDER BY
build_number DESC,
workspace_agents.last_connected_at ASC,
workspace_agents.first_connected_at ASC
agent_last_connected_at ASC,
agent_first_connected_at ASC
LIMIT
1
) latest_build ON TRUE
Expand Down Expand Up @@ -185,19 +185,19 @@ WHERE
latest_build.transition = 'start'::workspace_transition
AND CASE
WHEN @has_agent = 'timeout' THEN
latest_build.first_connected_at IS NULL AND (latest_build.created_at + latest_build.connection_timeout_seconds * INTERVAL '1 second' < NOW())
latest_build.agent_first_connected_at IS NULL AND (latest_build.agent_created_at + latest_build.agent_connection_timeout_seconds * INTERVAL '1 second' < NOW())
WHEN @has_agent = 'connecting' THEN
latest_build.first_connected_at IS NULL
latest_build.agent_first_connected_at IS NULL
WHEN @has_agent = 'disconnected' THEN
(
latest_build.disconnected_at IS NOT NULL AND
latest_build.disconnected_at > latest_build.last_connected_at
latest_build.agent_disconnected_at IS NOT NULL AND
latest_build.agent_disconnected_at > latest_build.agent_last_connected_at
) OR (
latest_build.last_connected_at IS NOT NULL AND
latest_build.last_connected_at + INTERVAL '1 second' * @agent_inactive_disconnect_timeout :: bigint < NOW()
latest_build.agent_last_connected_at IS NOT NULL AND
latest_build.agent_last_connected_at + INTERVAL '1 second' * @agent_inactive_disconnect_timeout_seconds :: bigint < NOW()
)
WHEN @has_agent = 'connected' THEN
latest_build.last_connected_at IS NOT NULL
latest_build.agent_last_connected_at IS NOT NULL
ELSE true
END
ELSE true
Expand Down
2 changes: 1 addition & 1 deletion coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ func validWorkspaceSchedule(s *string) (sql.NullString, error) {
// It also can return the list of validation errors to return to the api.
func workspaceSearchQuery(query string, page codersdk.Pagination, agentInactiveDisconnectTimeout time.Duration) (database.GetWorkspacesParams, []codersdk.ValidationError) {
filter := database.GetWorkspacesParams{
AgentInactiveDisconnectTimeout: int64(agentInactiveDisconnectTimeout.Seconds()),
AgentInactiveDisconnectTimeoutSeconds: int64(agentInactiveDisconnectTimeout.Seconds()),

Offset: int32(page.Offset),
Limit: int32(page.Limit),
Expand Down
2 changes: 1 addition & 1 deletion coderd/workspaces_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,6 @@ func TestSearchWorkspace(t *testing.T) {
timeout := 1337 * time.Second
values, errs := workspaceSearchQuery(query, codersdk.Pagination{}, timeout)
require.Empty(t, errs)
require.Equal(t, int64(timeout.Seconds()), values.AgentInactiveDisconnectTimeout)
require.Equal(t, int64(timeout.Seconds()), values.AgentInactiveDisconnectTimeoutSeconds)
})
}