Skip to main content

System events in Apify Actors

Learn about system events sent to your Actor and how to benefit from them.


Understand system events

Apify's system notifies Actors about various events, such as:

  • Migration to another server
  • Abort operations triggered by another Actor
  • CPU overload

These events help you manage your Actor's behavior and resources effecetively.

System events

The following table outlines the system events available:

Event namePayloadDescription
cpuInfo{ isCpuOverloaded: Boolean }Emitted approximately every second, indicating whether the Actor is using maximum available CPU resources.
migratingN/ASignals that the Actor will soon migrate to another worker server on the Apify platform.
abortingN/ATriggered when a user initiates a graceful abort of an Actor run, allowing time for cleanup.
persistState{ isMigrating: Boolean }Emitted at regular intervals (default: 60 seconds) to notify Apify SDK components to persist their state.

How system events work

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:

{
// Event name
name: String,

// Time when the event was created, in ISO format
createdAt: String,

// Optional object with payload
data: Object,
}
Virtual events

Some events like persistState, are generated virtually at the Actor SDK level, not sent via WebSocket.

Handle system events

To work with system events in your Actor, use the following methods:

import { Actor } from 'apify';

await Actor.init();

// Add event handler
Actor.on('cpuInfo', (data) => {
if (data.isCpuOverloaded) console.log('Oh no, we need to slow down!');
});

// Remove all handlers for a specific event
Actor.off('systemInfo');

// Remove a specific event handler
Actor.off('systemInfo', handler);

await Actor.exit();

By utilizing these system events, you can create more robust and efficient Actors that respond dynamically to changes in their environment.