Can sandbox.update reset session timeout to keep alive?
Doing this will reset a session timeout to the value I pass? I want to know if I can use it to keep a session alive as long as my user is watching. await sandbox.update({ timeout: SESSION_TIMEOUT_MS })
Here's what I found:
###There is no sandbox.update() method
The Sandbox class does not have an update() method, so await sandbox.update({ timeout: SESSION_TIMEOUT_MS }) won't work.
###What you want is sandbox.extendTimeout(duration)
The correct API is extendTimeout(duration), and it works differently than you might expect:
-
It extends the timeout by the given duration — it does not reset/set it to a fixed value.
-
The
durationparameter is additive. From the docs and example in the source:const sandbox = await Sandbox.create({ timeout: ms('10m') }); // Extends timeout by 5 minutes, to a total of 15 minutes. await sandbox.extendTimeout(ms('5m')); -
Under the hood, it calls
POST /v1/sandboxes/{sandboxId}/extend-timeoutwith{ duration }in the body (source). -
There is a maximum execution timeout capped by your plan, so you can't extend indefinitely.
###To keep a session alive while a user is watching
You could set up a periodic interval that calls extendTimeout with a smaller increment:
const KEEP_ALIVE_INTERVAL = 5 * 60 * 1000; // 5 minutes
const EXTEND_BY = 5 * 60 * 1000; // 5 minutes
const interval = setInterval(async () => {
await sandbox.extendTimeout(EXTEND_BY);
}, KEEP_ALIVE_INTERVAL);
// When the user leaves:
clearInterval(interval);
Just keep in mind this is bounded by your plan's maximum execution timeout — you can extend the lifetime of a sandbox, but only up to that ceiling.
END OF POST