-
Notifications
You must be signed in to change notification settings - Fork 54
feat: Actor.charge() #346
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
feat: Actor.charge() #346
Changes from 19 commits
7529857
7f786f8
32982bb
1897d08
6461f01
0d558b6
044ae6c
5d2a678
38ff1b6
582bb0d
fe38c87
b3dafeb
c58aae6
8dc3422
02f1953
35a378f
e9482fc
9925108
973b19f
72ff36a
b5ebe79
1bb2dc3
7d4263c
6ef64a6
c312681
da8daf3
dbd8be6
99d13f2
744f4a9
1aef01d
a6ff016
c4f01a2
06b80de
c476944
ec72e6f
346cc98
31ad976
01832c8
eee72cb
c16d554
04c4790
915cb31
a3e78f2
b080ae6
e2ff873
4839a74
292ab3e
30ac39a
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 |
---|---|---|
|
@@ -27,3 +27,5 @@ apify_storage | |
crawlee_storage | ||
storage | ||
.turbo | ||
*.tgz | ||
mise.toml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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`); | ||
B4nan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
|
@@ -609,13 +617,30 @@ export class Actor<Data extends Dictionary = Dictionary> { | |
* | ||
* @param item Object or array of objects containing data to be stored in the default dataset. | ||
* The objects must be serializable to JSON and the JSON representation of each object must be smaller than 9MB. | ||
* @param eventName If provided, the method will attempt to charge for the event for each pushed item. | ||
* @ignore | ||
*/ | ||
async pushData(item: Data | Data[]): Promise<void> { | ||
async pushData(item: Data | Data[]): Promise<void>; | ||
async pushData(item: Data | Data[], eventName: string): Promise<ChargeResult>; | ||
async pushData(item: Data | Data[], eventName?: string | undefined): Promise<ChargeResult | void> { | ||
this._ensureActorInit('pushData'); | ||
|
||
const chargeResult = eventName !== undefined | ||
? await this.charge({ eventName, count: Array.isArray(item) ? item.length : 1 }) | ||
: undefined; | ||
|
||
const dataset = await this.openDataset(); | ||
return dataset.pushData(item); | ||
|
||
if (chargeResult?.eventChargeLimitReached) { | ||
const items = Array.isArray(item) ? item : [item]; | ||
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. I would maybe add 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. Well, I guess with a third occurence I'd start to consider it 😁 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. I was actually surprised we dont have it here already, I can imagine there are other places that should use it. But yeah, lets not derail here. |
||
await dataset.pushData(items.slice(0, chargeResult.chargedCount)); | ||
janbuchar marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} else { | ||
await dataset.pushData(item); | ||
} | ||
|
||
if (eventName !== undefined) { | ||
return chargeResult; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -896,6 +921,29 @@ export class Actor<Data extends Dictionary = Dictionary> { | |
return undefined; | ||
} | ||
|
||
/** | ||
* Charge for a specified number of events - sub-operations of the Actor. | ||
* | ||
* @param options The name of the event to charge for and the number of events to be charged. | ||
*/ | ||
async charge(options: ChargeOptions): Promise<ChargeResult> { | ||
return this.chargingManager.charge(options); | ||
janbuchar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Get the maximum amount of money that the Actor is allowed to charge. | ||
*/ | ||
getMaxTotalChargeUsd(): number { | ||
return this.chargingManager.getMaxTotalChargeUsd(); | ||
} | ||
|
||
/** | ||
* Get the number of events with given name that the Actor has charged for so far. | ||
*/ | ||
getChargedEventCount(eventName: string): number { | ||
return this.chargingManager.getChargedEventCount(eventName); | ||
} | ||
|
||
/** | ||
* Modifies Actor env vars so parsing respects the structure of {@apilink ApifyEnv} interface. | ||
*/ | ||
|
@@ -1304,9 +1352,15 @@ export class Actor<Data extends Dictionary = Dictionary> { | |
* | ||
* @param item Object or array of objects containing data to be stored in the default dataset. | ||
* The objects must be serializable to JSON and the JSON representation of each object must be smaller than 9MB. | ||
* @param eventName If provided, the method will attempt to charge for the event for each pushed item. | ||
*/ | ||
static async pushData<Data extends Dictionary = Dictionary>(item: Data | Data[]): Promise<void> { | ||
return Actor.getDefaultInstance().pushData(item); | ||
static async pushData<Data extends Dictionary = Dictionary>(item: Data | Data[]): Promise<void>; | ||
static async pushData<Data extends Dictionary = Dictionary>(item: Data | Data[], eventName: string): Promise<ChargeResult>; | ||
static async pushData<Data extends Dictionary = Dictionary>(item: Data | Data[], eventName?: string): Promise<ChargeResult | void> { | ||
if (eventName === undefined) { | ||
return await Actor.getDefaultInstance().pushData(item); | ||
} | ||
return await Actor.getDefaultInstance().pushData(item, eventName); | ||
janbuchar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
|
@@ -1512,6 +1566,29 @@ export class Actor<Data extends Dictionary = Dictionary> { | |
return Actor.getDefaultInstance().createProxyConfiguration(proxyConfigurationOptions); | ||
} | ||
|
||
/** | ||
* Charge for a specified number of events - sub-operations of the Actor. | ||
* | ||
* @param options The name of the event to charge for and the number of events to be charged. | ||
*/ | ||
static async charge(options: ChargeOptions): Promise<ChargeResult> { | ||
return Actor.getDefaultInstance().charge(options); | ||
} | ||
|
||
/** | ||
* Get the maximum amount of money that the Actor is allowed to charge. | ||
*/ | ||
static getMaxTotalChargeUsd(): number { | ||
return Actor.getDefaultInstance().getMaxTotalChargeUsd(); | ||
} | ||
|
||
/** | ||
* Get the number of events with given name that the Actor has charged for so far. | ||
*/ | ||
static getChargedEventCount(eventName: string): number { | ||
return Actor.getDefaultInstance().getChargedEventCount(eventName); | ||
} | ||
|
||
/** | ||
* Returns a new {@apilink ApifyEnv} object which contains information parsed from all the Apify environment variables. | ||
* | ||
|
Uh oh!
There was an error while loading. Please reload this page.