Skip to content
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

Add paused sandboxes to the list of running sandboxes #264

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3648bbd
added sandbox_started_at field to snaphots
mishushakov Jan 29, 2025
0f32b7e
added paused sandboxes to the list of sandboxes and state filtering
mishushakov Jan 29, 2025
43ec9c0
fixes a panic on aliases, updates sandbox_get method
mishushakov Jan 29, 2025
351bad6
added "all" state query parameter
mishushakov Jan 29, 2025
6c087c7
moved some code around for prettyness
mishushakov Jan 29, 2025
5576498
return empty list on no sandboxes instead of error
mishushakov Jan 31, 2025
b9a9fd5
refactored list_snapshots (wip)
mishushakov Jan 31, 2025
5c8a41e
query refactor (wip)
mishushakov Jan 31, 2025
fba4230
simplified query
mishushakov Jan 31, 2025
fcb5725
changed query
mishushakov Feb 1, 2025
1444bab
fixes query to include all snapshots
mishushakov Feb 1, 2025
44c6148
zero out clientid for snapshots and remove all from query params
mishushakov Feb 1, 2025
7b91e46
renamed to avoid confusion
mishushakov Feb 3, 2025
46d5cd6
renamed RunningSandbox > ListedSandbox
mishushakov Feb 4, 2025
17c4117
return error early if sandbox team does not match auth
mishushakov Feb 4, 2025
c08cd0f
make sandbox list query less wasteful when option param is given
mishushakov Feb 4, 2025
c465572
resolved conflicts with other branch
mishushakov Feb 5, 2025
f427051
Merge branch 'main' into add-paused-sandboxes-to-the-list-of-running-…
mishushakov Feb 5, 2025
399686d
postmerge
mishushakov Feb 5, 2025
fb571ce
implemented some of the @jakubno suggestions
mishushakov Feb 6, 2025
94ca02c
madesandbox_started_at nillable
mishushakov Feb 6, 2025
e5d64b5
Add migration
jakubno Feb 8, 2025
15f5570
renamed instance > sandbox
mishushakov Feb 8, 2025
f3fd4c9
made startedAt nullable
mishushakov Feb 8, 2025
2903259
type improvements for sandbox list
mishushakov Feb 8, 2025
82663a2
logging errors in sandbox apis
mishushakov Feb 8, 2025
8cafa93
made state query param an array
mishushakov Feb 10, 2025
5545200
updated openapi spec
mishushakov Feb 10, 2025
128ff4f
undone nillability for sandbox_started_at
mishushakov Feb 10, 2025
7349956
set sandbox started at to the time sandbox was unpaused
mishushakov Feb 11, 2025
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
8 changes: 8 additions & 0 deletions packages/api/internal/api/api.gen.go

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

109 changes: 55 additions & 54 deletions packages/api/internal/api/spec.gen.go

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

82 changes: 53 additions & 29 deletions packages/api/internal/api/types.gen.go

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

80 changes: 56 additions & 24 deletions packages/api/internal/handlers/sandbox_get.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't used anywhere yet, right? Because it would be backward incompatible

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What line are you mentioning? StartedAt is now nullable (but required) - it is used in the CLI and Dashboard

Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,57 @@ func (a *APIStore) GetSandboxesSandboxID(c *gin.Context, id string) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the event above? 🙏🏻

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not seeing the line you're talking about?

sandboxId := strings.Split(id, "-")[0]

// Try to get the running instance first
info, err := a.orchestrator.GetInstance(ctx, sandboxId)
if err != nil {
c.JSON(http.StatusNotFound, fmt.Sprintf("instance \"%s\" doesn't exist or you don't have access to it", id))
return
}

if *info.TeamID != team.ID {
c.JSON(http.StatusNotFound, fmt.Sprintf("instance \"%s\" doesn't exist or you don't have access to it", id))
if err == nil {
// Check if instance belongs to the team
if *info.TeamID != team.ID {
c.JSON(http.StatusNotFound, fmt.Sprintf("instance \"%s\" doesn't exist or you don't have access to it", id))
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
return
}

// Instance exists and belongs to the team - return running sandbox info
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
build, err := a.db.Client.EnvBuild.Query().Where(envbuild.ID(*info.BuildID)).First(ctx)
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
telemetry.ReportCriticalError(ctx, err)
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
c.JSON(http.StatusInternalServerError, fmt.Sprintf("Error getting build for instance %s", id))
return
}

cpuCount := int32(-1)
memoryMB := int32(-1)

if build != nil {
cpuCount = int32(build.Vcpu)
memoryMB = int32(build.RAMMB)
}

instance := api.ListedSandbox{
ClientID: info.Instance.ClientID,
TemplateID: info.Instance.TemplateID,
Alias: info.Instance.Alias,
SandboxID: info.Instance.SandboxID,
StartedAt: info.StartTime,
CpuCount: cpuCount,
MemoryMB: memoryMB,
EndAt: info.EndTime,
State: "running",
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
}

if info.Metadata != nil {
meta := api.SandboxMetadata(info.Metadata)
instance.Metadata = &meta
}

c.JSON(http.StatusOK, instance)
return
}

a.posthog.IdentifyAnalyticsTeam(team.ID.String(), team.Name)
properties := a.posthog.GetPackageToPosthogProperties(&c.Request.Header)
a.posthog.CreateAnalyticsTeamEvent(team.ID.String(), "get running instance", properties)

build, err := a.db.Client.EnvBuild.Query().Where(envbuild.ID(*info.BuildID)).First(ctx)
// If instance not found try to get the latest snapshot
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
snapshot, build, err := a.db.GetLastSnapshot(ctx, sandboxId, team.ID)
if err != nil {
telemetry.ReportCriticalError(ctx, err)
c.JSON(http.StatusInternalServerError, fmt.Sprintf("Error getting build for instance %s", id))

fmt.Println(err)
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
c.JSON(http.StatusNotFound, fmt.Sprintf("instance or snapshot \"%s\" doesn't exist or you don't have access to it", id))
return
}

Expand All @@ -55,19 +86,20 @@ func (a *APIStore) GetSandboxesSandboxID(c *gin.Context, id string) {
cpuCount = int32(build.Vcpu)
}

instance := api.RunningSandbox{
ClientID: info.Instance.ClientID,
TemplateID: info.Instance.TemplateID,
Alias: info.Instance.Alias,
SandboxID: info.Instance.SandboxID,
StartedAt: info.StartTime,
// optional
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
instance := api.ListedSandbox{
ClientID: "00000000",
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
TemplateID: snapshot.EnvID,
SandboxID: snapshot.SandboxID,
StartedAt: snapshot.SandboxStartedAt,
CpuCount: cpuCount,
MemoryMB: memoryMB,
EndAt: info.EndTime,
EndAt: snapshot.CreatedAt,
State: "paused",
mishushakov marked this conversation as resolved.
Show resolved Hide resolved
}

if info.Metadata != nil {
meta := api.SandboxMetadata(info.Metadata)
if snapshot.Metadata != nil {
meta := api.SandboxMetadata(snapshot.Metadata)
instance.Metadata = &meta
}

Expand Down
1 change: 1 addition & 0 deletions packages/api/internal/handlers/sandbox_pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (a *APIStore) PostSandboxesSandboxIDPause(c *gin.Context, sandboxID api.San
snapshotConfig := &db.SnapshotInfo{
BaseTemplateID: sbx.Instance.TemplateID,
SandboxID: sandboxID,
SandboxStartedAt: sbx.StartTime,
VCPU: sbx.VCpu,
RAMMB: sbx.RamMB,
TotalDiskSizeMB: sbx.TotalDiskSizeMB,
Expand Down
Loading