@@ -86,44 +86,45 @@ export class ChargingManager {
86
86
87
87
private apifyClient : ApifyClient ;
88
88
89
- constructor ( configuration : Configuration , apifyClient : ApifyClient ) {
89
+ constructor (
90
+ private configuration : Configuration ,
91
+ apifyClient : ApifyClient ,
92
+ ) {
90
93
this . maxTotalChargeUsd =
91
94
configuration . get ( 'maxTotalChargeUsd' ) || Infinity ; // convert `0` to `Infinity` in case the value is an empty string
92
95
this . isAtHome = configuration . get ( 'isAtHome' ) ;
93
96
this . actorRunId = configuration . get ( 'actorRunId' ) ;
94
97
this . purgeChargingLogDataset = configuration . get ( 'purgeOnStart' ) ;
95
98
this . useChargingLogDataset = configuration . get ( 'useChargingLogDataset' ) ;
96
99
97
- if ( this . useChargingLogDataset && this . isAtHome ) {
98
- throw new Error (
99
- 'Using the ACTOR_USE_CHARGING_LOG_DATASET environment variable is only supported in a local development environment' ,
100
- ) ;
101
- }
102
-
103
- if ( configuration . get ( 'testPayPerEvent' ) ) {
104
- if ( this . isAtHome ) {
105
- throw new Error (
106
- 'Using the ACTOR_TEST_PAY_PER_EVENT environment variable is only supported in a local development environment' ,
107
- ) ;
108
- }
109
-
110
- this . pricingModel = 'PAY_PER_EVENT' ;
111
- }
112
-
113
100
this . apifyClient = apifyClient ;
114
101
}
115
102
116
103
private get isPayPerEvent ( ) {
117
104
return this . pricingModel === 'PAY_PER_EVENT' ;
118
105
}
119
106
120
- /**
121
- * Initialize the ChargingManager by loading pricing information and charging state via Apify API.
122
- */
123
- async init ( ) : Promise < void > {
124
- this . chargingState = { } ;
107
+ private async fetchPricingInfo ( ) : Promise < {
108
+ pricingInfo ?: ActorRunPricingInfo ;
109
+ chargedEventCounts ?: Record < string , number > ;
110
+ maxTotalChargeUsd : number ;
111
+ } > {
112
+ if (
113
+ this . configuration . get ( 'actorPricingInfo' ) &&
114
+ this . configuration . get ( 'chargedEventCounts' )
115
+ ) {
116
+ return {
117
+ pricingInfo : JSON . parse (
118
+ this . configuration . get ( 'actorPricingInfo' ) ,
119
+ ) as ActorRunPricingInfo ,
120
+ chargedEventCounts : JSON . parse (
121
+ this . configuration . get ( 'chargedEventCounts' ) ,
122
+ ) as Record < string , number > ,
123
+ maxTotalChargeUsd :
124
+ this . configuration . get ( 'maxTotalChargeUsd' ) || Infinity ,
125
+ } ;
126
+ }
125
127
126
- // Retrieve pricing information
127
128
if ( this . isAtHome ) {
128
129
if ( this . actorRunId === undefined ) {
129
130
throw new Error (
@@ -136,33 +137,74 @@ export class ChargingManager {
136
137
throw new Error ( 'Actor run not found' ) ;
137
138
}
138
139
139
- this . pricingModel = run . pricingInfo ?. pricingModel ;
140
-
141
- // Load per-event pricing information
142
- if ( run . pricingInfo ?. pricingModel === 'PAY_PER_EVENT' ) {
143
- for ( const [ eventName , eventPricing ] of Object . entries (
144
- run . pricingInfo . pricingPerEvent . actorChargeEvents ,
145
- ) ) {
146
- this . pricingInfo [ eventName ] = {
147
- price : eventPricing . eventPriceUsd ,
148
- title : eventPricing . eventTitle ,
149
- } ;
150
- }
151
-
152
- this . maxTotalChargeUsd =
153
- run . options . maxTotalChargeUsd ?? this . maxTotalChargeUsd ;
140
+ return {
141
+ pricingInfo : run . pricingInfo ,
142
+ chargedEventCounts : run . chargedEventCounts ,
143
+ maxTotalChargeUsd : run . options . maxTotalChargeUsd || Infinity ,
144
+ } ;
145
+ }
146
+
147
+ return {
148
+ pricingInfo : undefined ,
149
+ chargedEventCounts : { } ,
150
+ maxTotalChargeUsd :
151
+ this . configuration . get ( 'maxTotalChargeUsd' ) || Infinity ,
152
+ } ;
153
+ }
154
+
155
+ /**
156
+ * Initialize the ChargingManager by loading pricing information and charging state via Apify API.
157
+ */
158
+ async init ( ) : Promise < void > {
159
+ // Validate config - it may have changed since the instantiation
160
+ if ( this . useChargingLogDataset && this . isAtHome ) {
161
+ throw new Error (
162
+ 'Using the ACTOR_USE_CHARGING_LOG_DATASET environment variable is only supported in a local development environment' ,
163
+ ) ;
164
+ }
165
+
166
+ if ( this . configuration . get ( 'testPayPerEvent' ) ) {
167
+ if ( this . isAtHome ) {
168
+ throw new Error (
169
+ 'Using the ACTOR_TEST_PAY_PER_EVENT environment variable is only supported in a local development environment' ,
170
+ ) ;
154
171
}
172
+ }
173
+
174
+ // Retrieve pricing information
175
+ const { pricingInfo, chargedEventCounts, maxTotalChargeUsd } =
176
+ await this . fetchPricingInfo ( ) ;
177
+
178
+ if ( this . configuration . get ( 'testPayPerEvent' ) ) {
179
+ this . pricingModel = 'PAY_PER_EVENT' ;
180
+ } else {
181
+ this . pricingModel ??= pricingInfo ?. pricingModel ;
182
+ }
155
183
156
- // Load charged event counts
157
- for ( const [ eventName , chargeCount ] of Object . entries (
158
- run . chargedEventCounts ?? { } ,
184
+ // Load per-event pricing information
185
+ if ( pricingInfo ?. pricingModel === 'PAY_PER_EVENT' ) {
186
+ for ( const [ eventName , eventPricing ] of Object . entries (
187
+ pricingInfo . pricingPerEvent . actorChargeEvents ,
159
188
) ) {
160
- this . chargingState [ eventName ] = {
161
- chargeCount,
162
- totalChargedAmount :
163
- chargeCount * ( this . pricingInfo [ eventName ] ?. price ?? 0 ) ,
189
+ this . pricingInfo [ eventName ] = {
190
+ price : eventPricing . eventPriceUsd ,
191
+ title : eventPricing . eventTitle ,
164
192
} ;
165
193
}
194
+
195
+ this . maxTotalChargeUsd = maxTotalChargeUsd ;
196
+ }
197
+
198
+ this . chargingState = { } ;
199
+
200
+ for ( const [ eventName , chargeCount ] of Object . entries (
201
+ chargedEventCounts ?? { } ,
202
+ ) ) {
203
+ this . chargingState [ eventName ] = {
204
+ chargeCount,
205
+ totalChargedAmount :
206
+ chargeCount * ( this . pricingInfo [ eventName ] ?. price ?? 0 ) ,
207
+ } ;
166
208
}
167
209
168
210
if ( ! this . isPayPerEvent || ! this . useChargingLogDataset ) {
0 commit comments