Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/commands/actor/calculate-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -63,10 +69,16 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul
const { input, defaultMemoryMbytes, ...runOptions } = this.flags;

let memoryExpression: string | undefined = defaultMemoryMbytes;
Copy link
Copy Markdown
Member

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

let minMemory = 0;
let maxMemory = Infinity;

// If not provided via flag, try to load from actor.json
if (!memoryExpression) {
memoryExpression = await this.getExpressionFromConfig();
({
defaultMemoryMbytes: memoryExpression,
minMemoryMbytes: minMemory = minMemory, // Fallback to minMemory(0) if undefined
maxMemoryMbytes: maxMemory = maxMemory, // Fallback to maxMemory(Infinity) if undefined
} = await this.getExpressionFromConfig());
}

if (!memoryExpression) {
Expand All @@ -85,7 +97,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}` });
}
Expand All @@ -94,7 +108,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 });

Expand All @@ -103,10 +117,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,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast is a lie, its number | undefined

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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,
};
}
}
26 changes: 26 additions & 0 deletions test/local/commands/actor/calculate-memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,30 @@ describe('apify actor calculate-memory', () => {

expect(lastErrorMessage()).toMatch(/Memory calculation failed: /);
});

describe('clamping to minMemoryMbytes/maxMemoryMbytes', () => {
it('should clamp memory to minMemoryMbytes from actor.json', async () => {
await createActorJson({
defaultMemoryMbytes: START_URLS_LENGTH_BASED_MEMORY_EXPRESSION,
minMemoryMbytes: 8192,
});

await testRunCommand(ActorCalculateMemoryCommand, {
flags_input: inputPath,
});
expect(lastLogMessage()).toMatch(/8192 MB/);
});

it('should clamp memory to maxMemoryMbytes from actor.json', async () => {
await createActorJson({
defaultMemoryMbytes: START_URLS_LENGTH_BASED_MEMORY_EXPRESSION,
maxMemoryMbytes: 2048,
});

await testRunCommand(ActorCalculateMemoryCommand, {
flags_input: inputPath,
});
expect(lastLogMessage()).toMatch(/2048 MB/);
});
});
});