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
**Learn about system events sent to your Actor and how to benefit from them.**
9
11
10
12
import Tabs from '@theme/Tabs';
11
13
import TabItem from '@theme/TabItem';
12
14
13
15
---
14
16
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:
16
30
17
-
Currently, the system sends the following events:
18
31
19
32
| Event name | Payload | Description |
20
33
| -------------- | ------- | ----------- |
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. |
25
38
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:
27
42
28
43
```json5
29
44
{
@@ -38,7 +53,15 @@ Under the hood, Actors receive system events by connecting to a web socket addre
38
53
}
39
54
```
40
55
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:
42
65
43
66
<TabsgroupId="main">
44
67
<TabItemvalue="JavaScript"label="JavaScript">
@@ -90,3 +113,5 @@ async def main():
90
113
91
114
</TabItem>
92
115
</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