Skip to content

Commit

Permalink
Use ChargingManager in Actor
Browse files Browse the repository at this point in the history
  • Loading branch information
janbuchar committed Dec 23, 2024
1 parent 0d558b6 commit 044ae6c
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions packages/apify/src/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
import ow from 'ow';

import { Configuration } from './configuration';
import { ChargingManager } from './internals/charging';
import type { ChargeOptions, ChargeResult } from './internals/charging';
import { KeyValueStore } from './key_value_store';
import { PlatformEventManager } from './platform_event_manager';
import type { ProxyConfigurationOptions } from './proxy_configuration';
Expand Down Expand Up @@ -86,11 +88,14 @@ export class Actor<Data extends Dictionary = Dictionary> {
*/
private isRebooting = false;

private chargingManager: ChargingManager;

constructor(options: ConfigurationOptions = {}) {
// use default configuration object if nothing overridden (it fallbacks to env vars)
this.config = Object.keys(options).length === 0 ? Configuration.getGlobalConfig() : new Configuration(options);
this.apifyClient = this.newClient();
this.eventManager = new PlatformEventManager(this.config);
this.chargingManager = new ChargingManager(this.config, this.apifyClient);
}

/**
Expand Down Expand Up @@ -222,6 +227,9 @@ export class Actor<Data extends Dictionary = Dictionary> {
log.debug(`Default storages purged`);

Configuration.storage.enterWith(this.config);

await this.chargingManager.init();
log.debug(`ChargingManager initialized`);
}

/**
Expand Down Expand Up @@ -907,21 +915,21 @@ export class Actor<Data extends Dictionary = Dictionary> {
* TODO
*/
async charge(options: ChargeOptions): Promise<ChargeResult> {
return { eventChargeLimitReached: false };
return this.chargingManager.charge(options);
}

/**
* TODO
*/
async getMaxTotalChargeUsd(): Promise<number> {
return 0;
return this.config.get('maxTotalChargeUsd') ?? Infinity;
}

/**
* TODO
*/
async getChargedEventCount(eventName: string): Promise<number> {
return 0;
return this.chargingManager.getChargedEventCount(eventName);
}

/**
Expand Down Expand Up @@ -1884,15 +1892,6 @@ export interface OpenStorageOptions {

export { ClientActorRun as ActorRun };

interface ChargeOptions {
eventName: string;
count?: number;
}

interface ChargeResult {
eventChargeLimitReached: boolean;
}

/**
* Exit codes for the Actor process.
* The error codes must be in the range 1-128, to avoid collision with signal exits
Expand Down

0 comments on commit 044ae6c

Please sign in to comment.