runcloud 0.2.247 → 0.2.249

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api.js CHANGED
@@ -92,6 +92,11 @@ export class ApiClient {
92
92
  this.token = token;
93
93
  }
94
94
  async request(method, path, body, options = {}) {
95
+ const res = await this.send(method, path, body, options);
96
+ const contentType = res.headers.get('content-type') ?? '';
97
+ return contentType.includes('application/json') ? res.json() : res;
98
+ }
99
+ async send(method, path, body, options = {}) {
95
100
  let res;
96
101
  try {
97
102
  res = await fetch(`${this.baseUrl}${path}`, {
@@ -113,8 +118,7 @@ export class ApiClient {
113
118
  const { detail, ...context } = parsedError(await res.text());
114
119
  throw new ApiError(res.status, detail, path, context);
115
120
  }
116
- const contentType = res.headers.get('content-type') ?? '';
117
- return contentType.includes('application/json') ? res.json() : res;
121
+ return res;
118
122
  }
119
123
  get(path, options = {}) {
120
124
  return this.request('GET', path, undefined, options);
@@ -126,6 +130,11 @@ export class ApiClient {
126
130
  post(path, body, options = {}) {
127
131
  return this.request('POST', path, body, options);
128
132
  }
133
+ async postWithStatus(path, body, options = {}) {
134
+ const res = await this.send('POST', path, body, options);
135
+ const contentType = res.headers.get('content-type') ?? '';
136
+ return { status: res.status, body: contentType.includes('application/json') ? await res.json() : undefined };
137
+ }
129
138
  async postWithHeaders(path, body, headers) {
130
139
  const res = await fetch(`${this.baseUrl}${path}`, {
131
140
  method: 'POST',
@@ -16,6 +16,8 @@ const DEFAULT_BOX_CPU_CORES = 2;
16
16
  const DEFAULT_BOX_MEMORY_MIB = 24 * 1024;
17
17
  const SANDBOX_POLL_INTERVAL_MS = 1_000;
18
18
  const SANDBOX_WAIT_TIMEOUT_MS = 20 * 60 * 1_000;
19
+ const SANDBOX_RESUME_POLL_MS = 5_000;
20
+ const SANDBOX_RESUME_REQUEST_TIMEOUT_MS = 60_000;
19
21
  const READY_STATES = new Set(['running']);
20
22
  const FAILED_STATES = new Set(['interrupted', 'stopped', 'destroyed']);
21
23
  const METRIC_RANGES = new Set(['1h', '6h', '24h', '7d', '30d', 'all']);
@@ -47,6 +49,39 @@ function sandboxStatusMessage(state) {
47
49
  return 'Restoring sandbox';
48
50
  return `Sandbox is ${state.replaceAll('_', ' ')}`;
49
51
  }
52
+ export async function resumeSandbox(api, id, opts = {}) {
53
+ const intervalMs = opts.intervalMs ?? SANDBOX_RESUME_POLL_MS;
54
+ const deadline = Date.now() + (opts.timeoutMs ?? SANDBOX_WAIT_TIMEOUT_MS);
55
+ for (;;) {
56
+ let record;
57
+ let moving = false;
58
+ try {
59
+ const left = Math.max(1, deadline - Date.now());
60
+ const answer = await api.postWithStatus(`/run-cloud/sandboxes/${encodeURIComponent(id)}/resume`, { wait: false }, { signal: AbortSignal.timeout(Math.min(left, SANDBOX_RESUME_REQUEST_TIMEOUT_MS)) });
61
+ record = answer.body;
62
+ moving = answer.status === 202;
63
+ }
64
+ catch (err) {
65
+ const transient = !(err instanceof ApiError) || err.status === 408 || err.status === 429 || err.status >= 500;
66
+ if (!transient || Date.now() > deadline)
67
+ throw err;
68
+ }
69
+ if (record) {
70
+ const state = record.state ?? 'unknown';
71
+ opts.onState?.(state);
72
+ if (!moving && READY_STATES.has(state))
73
+ return record;
74
+ if (FAILED_STATES.has(state)) {
75
+ const reason = typeof record.last_error === 'string' && record.last_error ? `: ${record.last_error}` : '';
76
+ throw new Error(`sandbox ${id} entered ${state.replaceAll('_', ' ')} state${reason}`);
77
+ }
78
+ }
79
+ if (Date.now() > deadline) {
80
+ throw new Error(`sandbox ${id} did not resume in time; run.cloud may still be moving it, so resume it again`);
81
+ }
82
+ await new Promise((done) => setTimeout(done, intervalMs));
83
+ }
84
+ }
50
85
  export async function pollSandboxReady(api, id, opts = {}) {
51
86
  const intervalMs = opts.intervalMs ?? SANDBOX_POLL_INTERVAL_MS;
52
87
  const timeoutMs = opts.timeoutMs ?? SANDBOX_WAIT_TIMEOUT_MS;
@@ -104,12 +139,9 @@ export async function prepareSandboxShell(api, id, status = new StatusLine()) {
104
139
  let record = (await api.get(`/run-cloud/sandboxes/${encodeURIComponent(id)}`));
105
140
  if (record.state === 'paused' || record.state === 'archived') {
106
141
  status.update(`Resuming ${id}`);
107
- record = (await api.post(`/run-cloud/sandboxes/${encodeURIComponent(id)}/resume`));
108
- if (record.state !== 'running') {
109
- record = await pollSandboxReady(api, id, {
110
- onState: (state) => status.update(sandboxStatusMessage(state)),
111
- });
112
- }
142
+ record = await resumeSandbox(api, id, {
143
+ onState: (state) => status.update(sandboxStatusMessage(state)),
144
+ });
113
145
  status.succeed(`Resumed ${id}`);
114
146
  }
115
147
  if (record.state !== 'running') {
@@ -403,7 +435,7 @@ export function registerSandbox(program) {
403
435
  .option('--cpu-limit <cores>', 'CPU the sandbox may burst to above --cpu when its host has room; CPU above --cpu is billed as burst', parseCpuLimitCores)
404
436
  .option('--memory <mb>', 'memory allocation in MiB', parseMemoryMb)
405
437
  .option('--memory-limit <mb>', 'memory the sandbox may use above --memory when its host has room, in MiB; memory above --memory is billed as used', parseMemoryLimitMb)
406
- .option('--hardware-acceleration', 'run on a host with a GPU, with the hardware-accelerated image (OpenGL and Vulkan); must be enabled for the account; not with --image')
438
+ .option('--hardware-acceleration', 'run on a host with a GPU, with the hardware-accelerated image (OpenGL and Vulkan); must be enabled for the account; not with --image; default size 2 cores and 4096 MiB (with --expose, the box size)')
407
439
  .option('--disk <gb>', 'root disk capacity in GiB', parseDiskGb)
408
440
  .option('--idle-pause <seconds>', 'pause after inactivity; 0 keeps it running', parseIdlePauseSeconds)
409
441
  .option('--timeout <seconds>', 'maximum sandbox lifetime; defaults to 300, or 0 for persistent sandboxes', parseTimeoutSeconds)
@@ -708,7 +740,10 @@ export function registerSandbox(program) {
708
740
  .alias(lifecycle.alias)
709
741
  .description(lifecycle.description)
710
742
  .argument('<id>', 'sandbox id')).action((id, opts) => run(async () => {
711
- const record = await runCloudApi().post(`/run-cloud/sandboxes/${encodeURIComponent(id)}/${lifecycle.name}`);
743
+ const api = runCloudApi();
744
+ const record = lifecycle.name === 'resume'
745
+ ? await resumeSandbox(api, id)
746
+ : await api.post(`/run-cloud/sandboxes/${encodeURIComponent(id)}/${lifecycle.name}`);
712
747
  if (opts.json)
713
748
  printJson(record);
714
749
  else
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = '0.2.247';
1
+ export const CLI_VERSION = '0.2.249';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "runcloud",
3
- "version": "0.2.247",
3
+ "version": "0.2.249",
4
4
  "description": "Create and control run.cloud remote mobile simulators and cloud sandboxes",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
Binary file
Binary file