@@ -12,6 +12,12 @@ import { getJsonFileContent, getLocalKeyValueStorePath } from '../../lib/utils.j
1212
1313const DEFAULT_INPUT_PATH = join ( getLocalKeyValueStorePath ( 'default' ) , 'INPUT.json' ) ;
1414
15+ interface ActorMemoryConfig {
16+ defaultMemoryMbytes ?: string ;
17+ minMemoryMbytes ?: number ;
18+ maxMemoryMbytes ?: number ;
19+ }
20+
1521/**
1622 * This command can be used to test dynamic memory calculation expressions
1723 * defined in actor.json or provided via command-line flag.
@@ -60,22 +66,15 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul
6066 } ;
6167
6268 async run ( ) {
63- const { input, defaultMemoryMbytes, ...runOptions } = this . flags ;
64-
65- let memoryExpression : string | undefined = defaultMemoryMbytes ;
66-
67- // If not provided via flag, try to load from actor.json
68- if ( ! memoryExpression ) {
69- memoryExpression = await this . getExpressionFromConfig ( ) ;
70- }
69+ const { input, memoryExpression, minMemory, maxMemory, runOptions } = await this . prepareMemoryArguments ( ) ;
7170
7271 if ( ! memoryExpression ) {
7372 throw new Error (
7473 `No memory-calculation expression found. Provide it via the --defaultMemoryMbytes flag or define defaultMemoryMbytes in actor.json.` ,
7574 ) ;
7675 }
7776
78- const inputPath = resolve ( process . cwd ( ) , this . flags . input ) ;
77+ const inputPath = resolve ( process . cwd ( ) , input ) ;
7978 const inputJson = getJsonFileContent ( inputPath ) ?? { } ;
8079
8180 info ( { message : `Evaluating memory expression: ${ memoryExpression } ` } ) ;
@@ -85,16 +84,48 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul
8584 input : inputJson ,
8685 runOptions,
8786 } ) ;
88- success ( { message : `Calculated memory: ${ result } MB` , stdout : true } ) ;
87+ const clampedResult = Math . min ( Math . max ( result , minMemory ) , maxMemory ) ;
88+
89+ success ( { message : `Calculated memory: ${ clampedResult } MB` , stdout : true } ) ;
8990 } catch ( err ) {
9091 error ( { message : `Memory calculation failed: ${ ( err as Error ) . message } ` } ) ;
9192 }
9293 }
9394
95+ /**
96+ * Determines the memory arguments to use.
97+ * If --defaultMemoryMbytes flag is set, use it (unlimited min/max).
98+ * Otherwise, load from actor.json.
99+ */
100+ private async prepareMemoryArguments ( ) {
101+ const { input, defaultMemoryMbytes, ...runOptions } = this . flags ;
102+
103+ let memoryExpression : string | undefined = defaultMemoryMbytes ;
104+ let minMemory = 0 ;
105+ let maxMemory = Infinity ;
106+
107+ // If not provided via flag, try to load from actor.json
108+ if ( ! memoryExpression ) {
109+ ( {
110+ defaultMemoryMbytes : memoryExpression ,
111+ minMemoryMbytes : minMemory = minMemory , // Fallback to minMemory(0) if undefined
112+ maxMemoryMbytes : maxMemory = maxMemory , // Fallback to maxMemory(Infinity) if undefined
113+ } = await this . getExpressionFromConfig ( ) ) ;
114+ }
115+
116+ return {
117+ memoryExpression,
118+ minMemory,
119+ maxMemory,
120+ input,
121+ runOptions,
122+ } ;
123+ }
124+
94125 /**
95126 * Helper to load the `defaultMemoryMbytes` expression from actor.json.
96127 */
97- private async getExpressionFromConfig ( ) : Promise < string | undefined > {
128+ private async getExpressionFromConfig ( ) : Promise < ActorMemoryConfig > {
98129 const cwd = process . cwd ( ) ;
99130 const localConfigResult = await useActorConfig ( { cwd } ) ;
100131
@@ -103,10 +134,14 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul
103134
104135 error ( { message : `${ message } ${ cause ? `\n ${ cause . message } ` : '' } ` } ) ;
105136 process . exitCode = CommandExitCodes . InvalidActorJson ;
106- return ;
137+ return { } ;
107138 }
108139
109140 const { config : localConfig } = localConfigResult . unwrap ( ) ;
110- return localConfig ?. defaultMemoryMbytes ?. toString ( ) ;
141+ return {
142+ defaultMemoryMbytes : localConfig ?. defaultMemoryMbytes ?. toString ( ) ,
143+ minMemoryMbytes : localConfig ?. minMemoryMbytes as number | undefined ,
144+ maxMemoryMbytes : localConfig ?. maxMemoryMbytes as number | undefined ,
145+ } ;
111146 }
112147}
0 commit comments