|
1 | | -import {exec } from "../dependencies.ts"; |
| 1 | +import { exec } from "../dependencies.ts"; |
| 2 | + |
| 3 | +const STORAGE_TIMEOUT_MS = 5000; |
| 4 | + |
| 5 | +let storagePipeLock: Promise<void> = Promise.resolve(); |
| 6 | +async function withStoragePipeLock<T>(operation: () => Promise<T>): Promise<T> { |
| 7 | + const previousLock = storagePipeLock; |
| 8 | + let releaseLock!: () => void; |
| 9 | + storagePipeLock = new Promise<void>((resolve) => { |
| 10 | + releaseLock = resolve; |
| 11 | + }); |
| 12 | + await previousLock; |
| 13 | + try { |
| 14 | + return await operation(); |
| 15 | + } finally { |
| 16 | + releaseLock(); |
| 17 | + } |
| 18 | +} |
2 | 19 | export async function canAllocateStorage(requestedMb: number) { |
3 | 20 | const STORAGE_PATH = "/mnt/storage"; |
4 | 21 | const SAFETY_BUFFER_MB = 200; // keep buffer for system + docker |
5 | 22 |
|
6 | 23 | try { |
7 | | - const responseProcess = new Deno.Command("sh", { |
8 | | - args: ["-c", "cat /hostpipe/output_pipe"], |
9 | | - }).output(); // don't await yet, just start it |
10 | | - await exec(`bash -c "echo 'RESPOND::df ${STORAGE_PATH} --output=avail' > /hostpipe/pipe"`); |
11 | | - const response=await responseProcess; |
12 | | - const output = new TextDecoder().decode(response.stdout).trim().split("\n"); |
13 | | - const availableKb = parseInt(output[1].trim()); |
14 | | - if (isNaN(availableKb)) { |
15 | | - throw new Error(`Unexpected df output: ${output}`); |
16 | | - } |
17 | | - const availableMb = Math.floor(availableKb / 1024); |
18 | | - const usableMb = availableMb - SAFETY_BUFFER_MB; |
19 | | - const canAllocate = usableMb >= requestedMb; |
20 | | - console.log(`can allocate ${canAllocate} memory`); |
21 | | - console.log(`Available memory is ${availableMb} requested is ${requestedMb}`); |
22 | | - return { |
23 | | - can_allocate: canAllocate, |
24 | | - available_mb: usableMb, |
25 | | - requested_mb: requestedMb, |
26 | | - reason: canAllocate ? null : "Not enough disk space", |
27 | | - }; |
| 24 | + return await withStoragePipeLock(async () => { |
| 25 | + const abortController = new AbortController(); |
| 26 | + const timeoutId = setTimeout(() => abortController.abort(), STORAGE_TIMEOUT_MS); |
| 27 | + try { |
| 28 | + const responseProcess = new Deno.Command("sh", { |
| 29 | + args: ["-c", "cat /hostpipe/output_pipe"], |
| 30 | + }).output(); // don't await yet, just start it |
| 31 | + await exec(`bash -c "echo 'RESPOND::df ${STORAGE_PATH} --output=avail' > /hostpipe/pipe"`); |
| 32 | + const response = await responseProcess; |
| 33 | + const output = new TextDecoder().decode(response.stdout).trim().split("\n"); |
| 34 | + const availableKb = parseInt(output[1].trim()); |
| 35 | + if (isNaN(availableKb)) { |
| 36 | + throw new Error(`Unexpected df output: ${output}`); |
| 37 | + } |
| 38 | + const availableMb = Math.floor(availableKb / 1024); |
| 39 | + const usableMb = availableMb - SAFETY_BUFFER_MB; |
| 40 | + const canAllocate = usableMb >= requestedMb; |
| 41 | + console.log(`can allocate ${canAllocate} memory`); |
| 42 | + console.log(`Available memory is ${availableMb} requested is ${requestedMb}`); |
| 43 | + return { |
| 44 | + can_allocate: canAllocate, |
| 45 | + available_mb: usableMb, |
| 46 | + requested_mb: requestedMb, |
| 47 | + reason: canAllocate ? null : "Not enough disk space", |
| 48 | + }; |
| 49 | + } finally { |
| 50 | + clearTimeout(timeoutId); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
28 | 54 | } catch (err) { |
29 | 55 | console.log(`Error during memory check volume`); |
30 | 56 | return { |
|
0 commit comments