From fae942391a478a721c4df785d81d016f3afdd3eb Mon Sep 17 00:00:00 2001 From: YashSharma79 <@users.noreply.github.com> Date: Mon, 28 Jul 2025 23:33:20 +0000 Subject: [PATCH] timeout to instance start for pending instances --- src/api.ts | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/api.ts b/src/api.ts index df54aa1..9d3431f 100644 --- a/src/api.ts +++ b/src/api.ts @@ -125,6 +125,7 @@ interface InstanceStartOptions { metadata?: Record; ttlSeconds?: number; ttlAction?: "stop" | "pause"; + timeout?: number; } interface InstanceSnapshotOptions { @@ -1224,7 +1225,7 @@ class MorphCloudClient { }, start: async (options: InstanceStartOptions): Promise => { - const { snapshotId, metadata, ttlSeconds, ttlAction } = options; + const { snapshotId, metadata, ttlSeconds, ttlAction, timeout } = options; // Build query parameters const queryParams = { @@ -1244,7 +1245,36 @@ class MorphCloudClient { } const response = await this.POST("/instance", queryParams, body); - return new Instance(response, this); + const instance = new Instance(response, this); + + // Handle timeout parameter - automatic waiting logic + if (timeout !== undefined) { + try { + // Convert timeout=0 to undefined for indefinite wait (matches Python behavior) + const timeoutVal = timeout === 0 ? undefined : timeout; + await instance.waitUntilReady(timeoutVal); + } catch (error) { + // Log error and attempt cleanup + if (this.verbose) { + console.error(`Failed to start instance ${instance.id} with timeout: ${error}`); + } + + try { + await instance.stop(); + } catch (cleanupError) { + if (this.verbose) { + console.error(`Failed to cleanup instance ${instance.id}: ${cleanupError}`); + } + // Re-throw cleanup error if cleanup fails + throw cleanupError; + } + + // Re-throw original error if cleanup succeeds + throw error; + } + } + + return instance; }, get: async (options: InstanceGetOptions): Promise => {