Skip to content

Commit 3ceb066

Browse files
authored
docs: improve system events doc (#1121)
add admonitions rework logic flow rework structure & add headings streamline descriptions within table
1 parent fd96dfb commit 3ceb066

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

sources/platform/actors/development/programming_interface/system_events.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,40 @@ slug: /actors/development/programming-interface/system-events
55
sidebar_position: 4
66
---
77

8+
# System events in Apify Actors
9+
810
**Learn about system events sent to your Actor and how to benefit from them.**
911

1012
import Tabs from '@theme/Tabs';
1113
import TabItem from '@theme/TabItem';
1214

1315
---
1416

15-
The system notifies Actors about various events such as a migration to another server, [abort operation](#abort-another-actor) triggered by another Actor, or the CPU being overloaded.
17+
## Understand system events
18+
19+
Apify's system notifies Actors about various events, such as:
20+
21+
- Migration to another server
22+
- Abort operations triggered by another Actor
23+
- CPU overload
24+
25+
These events help you manage your Actor's behavior and resources effecetively.
26+
27+
## System events
28+
29+
The following table outlines the system events available:
1630

17-
Currently, the system sends the following events:
1831

1932
| Event name | Payload | Description |
2033
| -------------- | ------- | ----------- |
21-
| `cpuInfo` | `{ isCpuOverloaded: Boolean }` | The event is emitted approximately every second and it indicates whether the Actor is using the maximum of available CPU resources. If that's the case, the Actor should not add more workload. For example, this event is used by the AutoscaledPool class. |
22-
| `migrating` | N/A | Emitted when the Actor running on the Apify platform is going to be migrated to another worker server soon. You can use it to persist the state of the Actor and abort the run, to speed up migration. For example, this is used by the RequestList class. |
23-
| `aborting` | N/A | When a user aborts an Actor run on the Apify platform, they can choose to abort gracefully to allow the Actor some time before getting killed. This graceful abort emits the `aborting` event which the SDK uses to gracefully stop running crawls and you can use it to do your own cleanup as well.|
24-
| `persistState` | `{ isMigrating: Boolean }` | Emitted in regular intervals (by default 60 seconds) to notify all components of the Apify SDK that it is time to persist their state in order to avoid repeating all work when the Actor restarts. This event is automatically emitted together with the migrating event, in which case the `isMigrating` flag is set to `true`. Otherwise, the flag is `false`. Note that the `persistState` event is provided merely for user convenience. You can achieve the same effect using `setInterval()` and listening for the `migrating` event. |
34+
| `cpuInfo` | `{ isCpuOverloaded: Boolean }` | Emitted approximately every second, indicating whether the Actor is using maximum available CPU resources. |
35+
| `migrating` | N/A | Signals that the Actor will soon migrate to another worker server on the Apify platform. |
36+
| `aborting` | N/A | Triggered when a user initiates a graceful abort of an Actor run, allowing time for cleanup. |
37+
| `persistState` | `{ isMigrating: Boolean }` | Emitted at regular intervals (default: _60 seconds_) to notify Apify SDK components to persist their state. |
2538

26-
Under the hood, Actors receive system events by connecting to a web socket address specified by the `ACTOR_EVENTS_WEBSOCKET_URL` environment variable. The system sends messages in JSON format in the following structure:
39+
## How system events work
40+
41+
Actors receive system events through a WebSocket connection. The address is specified by the `ACTOR_EVENTS_WEBSOCKET_URL` environment variable. Messages are sent in JSON format with the following structure:
2742

2843
```json5
2944
{
@@ -38,7 +53,15 @@ Under the hood, Actors receive system events by connecting to a web socket addre
3853
}
3954
```
4055

41-
Note that some events (e.g. `persistState`) are not sent by the system via the web socket, but generated virtually on the Actor SDK level.
56+
:::note Virtual events
57+
58+
Some events like `persistState`, are generated virtually at the Actor SDK level, not sent via WebSocket.
59+
60+
:::
61+
62+
## Handle system events
63+
64+
To work with system events in your Actor, use the following methods:
4265

4366
<Tabs groupId="main">
4467
<TabItem value="JavaScript" label="JavaScript">
@@ -90,3 +113,5 @@ async def main():
90113

91114
</TabItem>
92115
</Tabs>
116+
117+
By utilizing these system events, you can create more robust and efficient Actors that respond dynamically to changes in their environment.

0 commit comments

Comments
 (0)