1+ import { exec } from "../dependencies.ts" ;
2+ export async function canAllocateStorage ( requestedMb : number ) {
3+ const STORAGE_PATH = "/mnt/storage" ;
4+ const SAFETY_BUFFER_MB = 200 ; // keep buffer for system + docker
5+
6+ 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+ } ;
28+ } catch ( err ) {
29+ console . log ( `Error during memory check volume` ) ;
30+ return {
31+ can_allocate : false ,
32+ available_mb : 0 ,
33+ requested_mb : requestedMb ,
34+ reason : "Failed to check disk space" ,
35+ } ;
36+ }
37+ }
0 commit comments