/
sandbox_refresh.go
54 lines (42 loc) · 1.38 KB
/
sandbox_refresh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package handlers
import (
"fmt"
"net/http"
"time"
"github.com/e2b-dev/infra/packages/api/internal/api"
"github.com/e2b-dev/infra/packages/api/internal/cache/instance"
"github.com/e2b-dev/infra/packages/api/internal/utils"
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
"github.com/gin-gonic/gin"
)
func (a *APIStore) PostSandboxesSandboxIDRefreshes(
c *gin.Context,
sandboxID string,
) {
ctx := c.Request.Context()
sandboxID = utils.ShortID(sandboxID)
var duration time.Duration
body, err := utils.ParseBody[api.PostSandboxesSandboxIDRefreshesJSONBody](ctx, c)
if err != nil {
a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err))
errMsg := fmt.Errorf("error when parsing request: %w", err)
telemetry.ReportCriticalError(ctx, errMsg)
return
}
if body.Duration == nil {
duration = instance.InstanceExpiration
} else {
duration = time.Duration(*body.Duration) * time.Second
}
if duration < instance.InstanceExpiration {
duration = instance.InstanceExpiration
}
err = a.instanceCache.KeepAliveFor(sandboxID, duration)
if err != nil {
errMsg := fmt.Errorf("error when refreshing instance: %w", err)
telemetry.ReportCriticalError(ctx, errMsg)
a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("Error refreshing sandbox - sandbox '%s' was not found", sandboxID))
return
}
c.Status(http.StatusNoContent)
}