refactor: Streamline Docker service management and error handling

- Removed unnecessary console logging in the mechanizeDockerContainer function to enhance code clarity.
- Simplified error handling by directly creating a new service if the existing one is not found, improving the deployment logic.
- Updated the buildNixpacks function to ensure container cleanup is attempted without additional error handling, streamlining the process.
This commit is contained in:
Mauricio Siu 2025-03-23 04:09:06 -06:00
parent 4b3e0805a4
commit 707463f973
2 changed files with 3 additions and 35 deletions

View File

@ -113,10 +113,6 @@ export const getBuildCommand = (
export const mechanizeDockerContainer = async (
application: ApplicationNested,
) => {
console.log(
`Starting to mechanize Docker container for ${application.appName}`,
);
const {
appName,
env,
@ -197,10 +193,8 @@ export const mechanizeDockerContainer = async (
};
try {
console.log(`Attempting to find existing service: ${appName}`);
const service = docker.getService(appName);
const inspect = await service.inspect();
console.log(`Found existing service, updating: ${appName}`);
await service.update({
version: Number.parseInt(inspect.Version.Index),
@ -210,22 +204,8 @@ export const mechanizeDockerContainer = async (
ForceUpdate: inspect.Spec.TaskTemplate.ForceUpdate + 1,
},
});
console.log(`Service updated successfully: ${appName}`);
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
console.log(`Service not found or error: ${errorMessage}`);
console.log(`Creating new service: ${appName}`);
try {
await docker.createService(settings);
console.log(`Service created successfully: ${appName}`);
} catch (createError: unknown) {
const createErrorMessage =
createError instanceof Error ? createError.message : "Unknown error";
console.error(`Failed to create service: ${createErrorMessage}`);
throw createError;
}
} catch (_error: unknown) {
await docker.createService(settings);
}
};

View File

@ -85,19 +85,7 @@ export const buildNixpacks = async (
}
return true;
} catch (e) {
// Only try to remove the container if it might exist
try {
await spawnAsync("docker", ["rm", buildContainerId], writeToStream);
} catch (rmError) {
// Ignore errors from container removal
const errorMessage =
rmError instanceof Error
? rmError.message
: "Unknown container cleanup error";
// Just log it but don't let it cause another error
writeToStream(`Container cleanup attempt: ${errorMessage}\n`);
}
await spawnAsync("docker", ["rm", buildContainerId], writeToStream);
throw e;
}