Skip to content

Commit 0c39772

Browse files
authored
docs: Explain how to correctly terminate PPE Actors (#568)
1 parent d33a21e commit 0c39772

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

docs/02_concepts/08_pay_per_event.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ description: Monetize your Actors using the pay-per-event pricing model
66

77
import ActorChargeSource from '!!raw-loader!./code/actor_charge.ts';
88
import ConditionalActorChargeSource from '!!raw-loader!./code/conditional_actor_charge.ts';
9+
import ChargeLimitCheckSource from '!!raw-loader!./code/charge_limit_check.ts';
910
import ApiLink from '@site/src/components/ApiLink';
1011
import CodeBlock from '@theme/CodeBlock';
1112

@@ -29,6 +30,20 @@ Then you just push your code to Apify and that's it! The SDK will even keep trac
2930

3031
If you need finer control over charging, you can access call <ApiLink to="class/Actor#getChargingManager">`Actor.getChargingManager()`</ApiLink> to access the <ApiLink to="class/ChargingManager">`ChargingManager`</ApiLink>, which can provide more detailed information - for example how many events of each type can be charged before reaching the configured limit.
3132

33+
### Handling the charge limit
34+
35+
While the SDK automatically prevents overcharging by limiting how many events are charged and how many items are pushed, **it does not stop your Actor from running**. When the charge limit is reached, `Actor.charge` and `Actor.pushData` will silently stop charging and pushing data, but your Actor will keep running — potentially doing expensive work (scraping pages, calling APIs) for no purpose. This means your Actor may never terminate on its own if you don't check the charge limit yourself.
36+
37+
To avoid this, you should check the `eventChargeLimitReached` field in the result returned by <ApiLink to="class/Actor#charge">`Actor.charge`</ApiLink> or `Actor.pushData` and stop your Actor when the limit is reached. You can also use the `chargeableWithinLimit` field from the result to plan ahead — it tells you how many events of each type can still be charged within the remaining budget.
38+
39+
<CodeBlock language="typescript">{ChargeLimitCheckSource}</CodeBlock>
40+
41+
Alternatively, you can periodically check the remaining budget via <ApiLink to="class/Actor#getChargingManager">`Actor.getChargingManager()`</ApiLink> instead of inspecting every `ChargeResult`. This can be useful when charging happens in multiple places across your code, or when using a crawler where you don't directly control the main loop.
42+
43+
:::caution
44+
Always check the charge limit in your Actor, whether through `ChargeResult` return values or the `ChargingManager`. Without this check, your Actor will continue running and consuming platform resources after the budget is exhausted, producing no output.
45+
:::
46+
3247
## Transitioning from a different pricing model
3348

3449
When you plan to start using the pay-per-event pricing model for an Actor that is already monetized with a different pricing model, your source code will need support both pricing models during the transition period enforced by the Apify platform. Arguably the most frequent case is the transition from the pay-per-result model which utilizes the `ACTOR_MAX_PAID_DATASET_ITEMS` environment variable to prevent returning unpaid dataset items. The following is an example how to handle such scenarios. The key part is the <ApiLink to="class/ChargingManager#getPricingInfo">`ChargingManager.getPricingInfo`</ApiLink> method which returns information about the current pricing model.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Actor } from 'apify';
2+
3+
await Actor.init();
4+
5+
const urls = [
6+
'https://example.com/1',
7+
'https://example.com/2',
8+
'https://example.com/3',
9+
];
10+
11+
for (const url of urls) {
12+
// Do some expensive work (e.g. scraping, API calls)
13+
const result = { url, data: `Scraped data from ${url}` };
14+
15+
// highlight-start
16+
// pushData returns a ChargeResult - check it to know if the budget ran out
17+
const { eventChargeLimitReached } = await Actor.pushData(
18+
result,
19+
'result-item',
20+
);
21+
22+
if (eventChargeLimitReached) {
23+
console.log('Charge limit reached, stopping the Actor');
24+
break;
25+
}
26+
// highlight-end
27+
}
28+
29+
await Actor.exit();

0 commit comments

Comments
 (0)