Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,9 @@ export class CrawlerSetup implements CrawlerSetupOptions {
skipLinksP,
globalStoreP,
logP,
// eslint-disable-next-line @typescript-eslint/await-thenable

requestQueueP,
// eslint-disable-next-line @typescript-eslint/await-thenable

keyValueStoreP,
]);

Expand Down
101 changes: 70 additions & 31 deletions packages/apify/src/charging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ export class ChargingManager {
this.purgeChargingLogDataset = configuration.get('purgeOnStart');
this.useChargingLogDataset = configuration.get('useChargingLogDataset');

if (
configuration.get('actorPricingInfo') &&
configuration.get('chargedEventCounts')
) {
this.loadPricingInfo(
JSON.parse(
configuration.get('actorPricingInfo'),
) as ActorRunPricingInfo,
configuration.get('maxTotalChargeUsd'),
);
this.loadChargedEventCounts(
JSON.parse(configuration.get('chargedEventCounts')) as Record<
string,
number
>,
);
}

if (this.useChargingLogDataset && this.isAtHome) {
throw new Error(
'Using the ACTOR_USE_CHARGING_LOG_DATASET environment variable is only supported in a local development environment',
Expand All @@ -117,12 +135,48 @@ export class ChargingManager {
return this.pricingModel === 'PAY_PER_EVENT';
}

private loadPricingInfo(
pricingInfo: ActorRunPricingInfo | undefined,
maxTotalChargeUsd: number | undefined,
) {
this.pricingModel = pricingInfo?.pricingModel;

// Load per-event pricing information
if (pricingInfo?.pricingModel === 'PAY_PER_EVENT') {
for (const [eventName, eventPricing] of Object.entries(
pricingInfo.pricingPerEvent.actorChargeEvents,
)) {
this.pricingInfo[eventName] = {
price: eventPricing.eventPriceUsd,
title: eventPricing.eventTitle,
};
}

this.maxTotalChargeUsd =
maxTotalChargeUsd ?? this.maxTotalChargeUsd;
}
}

private loadChargedEventCounts(
chargedEventCounts: Record<string, number> | undefined,
) {
this.chargingState = {};

for (const [eventName, chargeCount] of Object.entries(
chargedEventCounts ?? {},
)) {
this.chargingState[eventName] = {
chargeCount,
totalChargedAmount:
chargeCount * (this.pricingInfo[eventName]?.price ?? 0),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you have to call the new load* methods in this specific order, as loadChargedEventCounts reads variables initialized by loadPricingInfo (temporal coupling)

};
}
}

/**
* Initialize the ChargingManager by loading pricing information and charging state via Apify API.
*/
async init(): Promise<void> {
this.chargingState = {};

// Retrieve pricing information
if (this.isAtHome) {
if (this.actorRunId === undefined) {
Expand All @@ -131,40 +185,25 @@ export class ChargingManager {
);
}

const run = await this.apifyClient.run(this.actorRunId).get();
if (run === undefined) {
throw new Error('Actor run not found');
}

this.pricingModel = run.pricingInfo?.pricingModel;

// Load per-event pricing information
if (run.pricingInfo?.pricingModel === 'PAY_PER_EVENT') {
for (const [eventName, eventPricing] of Object.entries(
run.pricingInfo.pricingPerEvent.actorChargeEvents,
)) {
this.pricingInfo[eventName] = {
price: eventPricing.eventPriceUsd,
title: eventPricing.eventTitle,
};
if (
this.chargingState === undefined ||
Copy link
Contributor

@barjin barjin Oct 8, 2025

Choose a reason for hiding this comment

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

how about removing the load* method calls from the constructor, running them only once in the init method (at one call site) and conditionally loading the config with something like

async function getPricingInfo() {
   if (
        configuration.get('actorPricingInfo') &&
        configuration.get('chargedEventCounts')
    ) return { pricingInfo: JSON.parse(), ...

    const run = await this.apifyClient.run(this.actorRunId).get();
    ...
}

This would allow us to load the pricing info + events at one point in init like

...
const { pricingInfo ... } = await getPricingInfo();
this.loadPricingInfo( pricingInfo );
...

which is IMO cleaner than the current approach, which is conditional based on the current object state

this.pricingModel === undefined
) {
const run = await this.apifyClient.run(this.actorRunId).get();
if (run === undefined) {
throw new Error('Actor run not found');
}

this.maxTotalChargeUsd =
run.options.maxTotalChargeUsd ?? this.maxTotalChargeUsd;
}

// Load charged event counts
for (const [eventName, chargeCount] of Object.entries(
run.chargedEventCounts ?? {},
)) {
this.chargingState[eventName] = {
chargeCount,
totalChargedAmount:
chargeCount * (this.pricingInfo[eventName]?.price ?? 0),
};
this.loadPricingInfo(
run.pricingInfo,
run.options.maxTotalChargeUsd,
);
this.loadChargedEventCounts(run.chargedEventCounts);
}
}

this.chargingState ??= {};

if (!this.isPayPerEvent || !this.useChargingLogDataset) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/apify/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export interface ConfigurationOptions extends CoreConfigurationOptions {
metaOrigin?: (typeof META_ORIGINS)[keyof typeof META_ORIGINS];
testPayPerEvent?: boolean;
useChargingLogDataset?: boolean;
actorPricingInfo?: string;
chargedEventCounts?: string;
}

/**
Expand Down Expand Up @@ -179,6 +181,8 @@ export class Configuration extends CoreConfiguration {
ACTOR_MAX_TOTAL_CHARGE_USD: 'maxTotalChargeUsd',
ACTOR_TEST_PAY_PER_EVENT: 'testPayPerEvent',
ACTOR_USE_CHARGING_LOG_DATASET: 'useChargingLogDataset',
APIFY_ACTOR_PRICING_INFO: 'actorPricingInfo',
APIFY_CHARGED_ACTOR_EVENT_COUNTS: 'chargedEventCounts',
};

protected static override INTEGER_VARS = [
Expand Down
Loading