You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -57,6 +57,79 @@ An Actor's negative net profit does not affect the positive profit of another Ac
57
57
1._Test your pricing_: Run your Actor and analyze cost-effectiveness using a special dataset.
58
58
1._Communicate value_: Ensure pricing reflects the value provided and is competitive.
59
59
60
+
## Respect user spending limits
61
+
62
+
Finish the Actor run once charging reaches user-configured maximum cost per run. Apify SDKs (JS and Python) return `ChargeResult` that helps determine when to finish.
63
+
64
+
The `eventChargeLimitReached` property checks if the current event type can be charged more. If you have multiple event types, analyze the `chargeableWithinLimit` property to see if other events can still be charged before stopping the Actor.
For pay-per-event Actors, users set a spending limit through the Apify Console. This limit is available in your Actor code as the `ACTOR_MAX_TOTAL_CHARGE_USD`[environment variable](/platform/actors/development/programming-interface/environment-variables), which contains the user's maximum cost.
When using [Crawlee](https://crawlee.dev/), use `crawler.autoscaledPool.abort()` instead of `Actor.exit()` to gracefully finish the crawler and allow the rest of your code to process normally.
130
+
131
+
:::
132
+
60
133
## Best practices for PPE Actors
61
134
62
135
Use our SDKs ([JS](/sdk/js/) and, [Python](/sdk/python/) or use [`apify actor charge`](/cli/docs/next/reference#apify-actor-charge-eventname) when using our Apify CLI) to simplify PPE implementation into your Actor. This tool can handle pricing, usage tracking, idempotency keys, API errors, and, event charging via an API.
// Charge for the work done and return error item in one call
137
205
awaitActor.pushData({
138
206
url: url,
139
207
error:"404",
140
208
errorMessage:"Page not found"
141
-
});
209
+
}, 'scraped-result');
142
210
143
211
return;
144
212
}
145
213
146
-
// Rest of the Actor logic
214
+
// Rest of the process_url function
147
215
};
148
216
149
217
awaitActor.init();
150
218
151
-
constmain=async () => {
152
-
constinput=awaitActor.getInput();
153
-
const { urls } = input;
219
+
constinput=awaitActor.getInput();
220
+
const { urls } = input;
154
221
155
-
for (consturlof urls) {
156
-
awaitprocessUrl(url);
157
-
}
158
-
159
-
// Rest of the Actor logic
160
-
};
222
+
for (consturlof urls) {
223
+
awaitprocessUrl(url);
224
+
}
161
225
162
-
awaitmain();
226
+
// Rest of the Actor logic
163
227
164
228
awaitActor.exit();
165
229
```
@@ -175,19 +239,16 @@ async def process_url(url):
175
239
response = requests.get(url)
176
240
177
241
if response.status_code ==404:
178
-
# Charge for the work done (opening the page)
179
-
await Actor.charge(event_name='scraped-result')
180
-
181
-
# Return error item instead of failing
182
-
await Actor.push_data({
183
-
'url': url,
184
-
'error': '404',
185
-
'errorMessage': 'Page not found'
186
-
})
242
+
# Charge for the work done and return error item in one call
243
+
await Actor.push_data({
244
+
'url': url,
245
+
'error': '404',
246
+
'errorMessage': 'Page not found'
247
+
}, 'scraped-result')
187
248
188
-
return
249
+
return
189
250
190
-
# Rest of the Actor logic
251
+
# Rest of the process_url function
191
252
192
253
asyncdefmain():
193
254
await Actor.init()
@@ -206,80 +267,6 @@ async def main():
206
267
</TabItem>
207
268
</Tabs>
208
269
209
-
### Respect user spending limits
210
-
211
-
Finish the Actor run once charging reaches user-configured maximum cost per run. Apify SDKs (JS and Python) return `ChargeResult` that helps determine when to finish.
212
-
213
-
The `eventChargeLimitReached` property checks if the current event type can be charged more. If you have multiple event types, analyze the `chargeableWithinLimit` property to see if other events can still be charged before stopping the Actor.
214
-
215
-
<TabsgroupId="main">
216
-
<TabItemvalue="JavaScript"label="JavaScript">
217
-
218
-
```js
219
-
import { Actor } from'apify';
220
-
221
-
constchargForApiProductDetail=async () => {
222
-
constchargeResult=awaitActor.charge({
223
-
eventName:"product-detail",
224
-
});
225
-
226
-
return chargeResult;
227
-
};
228
-
229
-
awaitActor.init();
230
-
231
-
constmain=async () => {
232
-
// API call, or any other logic that you want to charge for
When using [Crawlee](https://crawlee.dev/), use `crawler.autoscaledPool.abort()` instead of `Actor.exit()` to gracefully finish the crawler and allow the rest of your code to process normally.
280
-
281
-
:::
282
-
283
270
### Keep pricing simple with fewer events
284
271
285
272
Try to limit the number of events. Fewer events make it easier for users to understand your pricing and predict their costs.
0 commit comments