|
1 | 1 | import { createServer } from "node:http";
|
| 2 | +import fs from "node:fs/promises"; |
2 | 3 | import { $, type ExecaChildProcess } from "execa";
|
3 | 4 | import { nanoid } from "nanoid";
|
4 | 5 | import { Server } from "socket.io";
|
@@ -62,6 +63,36 @@ function isExecaChildProcess(maybeExeca: unknown): maybeExeca is Awaited<ExecaCh
|
62 | 63 | return typeof maybeExeca === "object" && maybeExeca !== null && "escapedCommand" in maybeExeca;
|
63 | 64 | }
|
64 | 65 |
|
| 66 | +async function getFileSize(filePath: string): Promise<number> { |
| 67 | + try { |
| 68 | + const stats = await fs.stat(filePath); |
| 69 | + return stats.size; |
| 70 | + } catch (error) { |
| 71 | + console.error("Error getting file size:", error); |
| 72 | + return -1; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async function getParsedFileSize(filePath: string) { |
| 77 | + const sizeInBytes = await getFileSize(filePath); |
| 78 | + |
| 79 | + let message = `Size in bytes: ${sizeInBytes}`; |
| 80 | + |
| 81 | + if (sizeInBytes > 1024 * 1024) { |
| 82 | + const sizeInMB = (sizeInBytes / 1024 / 1024).toFixed(2); |
| 83 | + message = `Size in MB (rounded): ${sizeInMB}`; |
| 84 | + } else if (sizeInBytes > 1024) { |
| 85 | + const sizeInKB = (sizeInBytes / 1024).toFixed(2); |
| 86 | + message = `Size in KB (rounded): ${sizeInKB}`; |
| 87 | + } |
| 88 | + |
| 89 | + return { |
| 90 | + path: filePath, |
| 91 | + sizeInBytes, |
| 92 | + message, |
| 93 | + }; |
| 94 | +} |
| 95 | + |
65 | 96 | class Checkpointer {
|
66 | 97 | #initialized = false;
|
67 | 98 | #canCheckpoint = false;
|
@@ -314,6 +345,10 @@ class Checkpointer {
|
314 | 345 | this.#logger.debug(await $$`crictl checkpoint --export=${exportLocation} ${containerId}`);
|
315 | 346 | const postCheckpoint = performance.now();
|
316 | 347 |
|
| 348 | + // Print checkpoint size |
| 349 | + const size = await getParsedFileSize(exportLocation); |
| 350 | + this.#logger.log("checkpoint archive created", { size, options }); |
| 351 | + |
317 | 352 | // Create image from checkpoint
|
318 | 353 | const container = this.#logger.debug(await $$`buildah from scratch`);
|
319 | 354 | const postFrom = performance.now();
|
|
0 commit comments