-
Notifications
You must be signed in to change notification settings - Fork 47
fix: calculate run memory min/max memory clamping #986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,12 @@ import { getJsonFileContent, getLocalKeyValueStorePath } from '../../lib/utils.j | |
|
|
||
| const DEFAULT_INPUT_PATH = join(getLocalKeyValueStorePath('default'), 'INPUT.json'); | ||
|
|
||
| interface ActorMemoryConfig { | ||
| defaultMemoryMbytes?: string; | ||
| minMemoryMbytes?: number; | ||
| maxMemoryMbytes?: number; | ||
| } | ||
|
|
||
| /** | ||
| * This command can be used to test dynamic memory calculation expressions | ||
| * defined in actor.json or provided via command-line flag. | ||
|
|
@@ -63,10 +69,19 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul | |
| const { input, defaultMemoryMbytes, ...runOptions } = this.flags; | ||
|
|
||
| let memoryExpression: string | undefined = defaultMemoryMbytes; | ||
| let minMemory = 0; | ||
| let maxMemory = Infinity; | ||
|
|
||
| // If not provided via flag, try to load from actor.json | ||
| if (!memoryExpression) { | ||
| memoryExpression = await this.getExpressionFromConfig(); | ||
| const { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (nit) You should be able to do: let memoryExpression: string | undefined = defaultMemoryMbytes;
let minMemoryMbytes = 0;
let maxMemoryMbytes = Infinity;
if (!memoryExpression) {
({ defaultMemoryMbytes: memoryExpression, minMemoryMbytes, maxMemoryMbytes } = await this.getExpressionFromConfig());
}I'm not sure about the exact syntax but it should be possible to destruct into variables defined via |
||
| defaultMemoryMbytes: defaultMemoryMbytesFromConfig, | ||
| minMemoryMbytes = minMemory, | ||
| maxMemoryMbytes = maxMemory, | ||
| } = await this.getExpressionFromConfig(); | ||
| memoryExpression = defaultMemoryMbytesFromConfig; | ||
| minMemory = minMemoryMbytes; | ||
| maxMemory = maxMemoryMbytes; | ||
| } | ||
|
|
||
| if (!memoryExpression) { | ||
|
|
@@ -85,7 +100,9 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul | |
| input: inputJson, | ||
| runOptions, | ||
| }); | ||
| success({ message: `Calculated memory: ${result} MB`, stdout: true }); | ||
| const clampedResult = Math.min(Math.max(result, minMemory), maxMemory); | ||
|
|
||
| success({ message: `Calculated memory: ${clampedResult} MB`, stdout: true }); | ||
| } catch (err) { | ||
| error({ message: `Memory calculation failed: ${(err as Error).message}` }); | ||
| } | ||
|
|
@@ -94,7 +111,7 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul | |
| /** | ||
| * Helper to load the `defaultMemoryMbytes` expression from actor.json. | ||
| */ | ||
| private async getExpressionFromConfig(): Promise<string | undefined> { | ||
| private async getExpressionFromConfig(): Promise<ActorMemoryConfig> { | ||
| const cwd = process.cwd(); | ||
| const localConfigResult = await useActorConfig({ cwd }); | ||
|
|
||
|
|
@@ -103,10 +120,14 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul | |
|
|
||
| error({ message: `${message}${cause ? `\n ${cause.message}` : ''}` }); | ||
| process.exitCode = CommandExitCodes.InvalidActorJson; | ||
| return; | ||
| return {}; | ||
| } | ||
|
|
||
| const { config: localConfig } = localConfigResult.unwrap(); | ||
| return localConfig?.defaultMemoryMbytes?.toString(); | ||
| return { | ||
| defaultMemoryMbytes: localConfig?.defaultMemoryMbytes?.toString(), | ||
| minMemoryMbytes: localConfig?.minMemoryMbytes as number, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cast is a lie, its number | undefined
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we need to define actor.json type in future, that will help in such cases |
||
| maxMemoryMbytes: localConfig?.maxMemoryMbytes as number, | ||
| }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: maybe extract all this logic inside a function in the class and return it for the run method to consume instead