Skip to content

Commit

Permalink
Second attempt at fixing 304s
Browse files Browse the repository at this point in the history
  • Loading branch information
scinos committed Jul 16, 2021
1 parent 337fe7a commit b1f8dd1
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,28 +665,23 @@ export async function createContainer( imageName: ImageName, env: RunEnv ) {
*/
export async function reviveContainer( containerInfo: ContainerInfo ) {
const containerName = getContainerName( containerInfo );
const container = docker.getContainer( containerInfo.Id );

// Try to start the container. Due concurrent requests the container
// maybe alredy be starting at this point anyway.
try {
// Wait for it if the container is alerady starting
if (containerInfo.State === "restarting") {
// Refresh container info until it is not 'restarting' anymore with a throttle of 1s
while ( containerInfo.State==="restarting" ) {
await new Promise((resolve) => setTimeout(resolve, 1000))
await refreshContainers();
containerInfo = findContainer( {id: containerInfo.Id});
}
return containerInfo;
}

const container = docker.getContainer( containerInfo.Id );
await container.start();
l.log( { containerName }, `Successfully started container` );
await refreshContainers();

// This returns the same containerInfo object, but updated
return findContainer( { id: container.id } );
} catch ( error ) {
error.message = `Failed reviving ${containerInfo.State} container ${ containerName }: ${error.message}`;
throw error;
} catch(error) {
if (error.statusCode === 304) {
l.log( { containerName }, `Container already started` );
} else {
error.message = `Failed reviving ${containerInfo.State} container ${ containerName }: ${error.message}`;
throw error;
}
}

// Refresh intenerl info about container and return the updated info for this container
await refreshContainers();
return findContainer( { id: container.id } );
}

0 comments on commit b1f8dd1

Please sign in to comment.