Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
61 changes: 48 additions & 13 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 @@ -60,22 +66,15 @@ export class ActorCalculateMemoryCommand extends ApifyCommand<typeof ActorCalcul
};

async run() {
const { input, defaultMemoryMbytes, ...runOptions } = this.flags;

let memoryExpression: string | undefined = defaultMemoryMbytes;

// If not provided via flag, try to load from actor.json
if (!memoryExpression) {
memoryExpression = await this.getExpressionFromConfig();
}
const { input, memoryExpression, minMemory, maxMemory, runOptions } = await this.prepareMemoryArguments();

if (!memoryExpression) {
throw new Error(
`No memory-calculation expression found. Provide it via the --defaultMemoryMbytes flag or define defaultMemoryMbytes in actor.json.`,
);
}

const inputPath = resolve(process.cwd(), this.flags.input);
const inputPath = resolve(process.cwd(), input);
const inputJson = getJsonFileContent(inputPath) ?? {};

info({ message: `Evaluating memory expression: ${memoryExpression}` });
Expand All @@ -85,16 +84,48 @@ 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}` });
}
}

/**
* Determines the memory arguments to use.
* If --defaultMemoryMbytes flag is set, use it (unlimited min/max).
* Otherwise, load from actor.json.
*/
private async prepareMemoryArguments() {
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) {
({
defaultMemoryMbytes: memoryExpression,
minMemoryMbytes: minMemory = minMemory, // Fallback to minMemory(0) if undefined
maxMemoryMbytes: maxMemory = maxMemory, // Fallback to maxMemory(Infinity) if undefined
} = await this.getExpressionFromConfig());
}

return {
memoryExpression,
minMemory,
maxMemory,
input,
runOptions,
};
}

/**
* 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 +134,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 | undefined,
maxMemoryMbytes: localConfig?.maxMemoryMbytes as number | undefined,
};
}
}
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/);
});
});
});