---
id: actor-events
title: Actor events & state persistence
description: Handle platform events like state persistence and graceful shutdown in your Actors.
---

import RunnableCodeBlock from '@site/src/components/RunnableCodeBlock';

import ActorEventsExample from '!!raw-loader!roa-loader!./code/04_actor_events.py';
import UseStateExample from '!!raw-loader!roa-loader!./code/04_use_state.py';
import ApiLink from '@theme/ApiLink';

During its runtime, the Actor receives Actor events sent by the Apify platform or generated by the Apify SDK itself.

## Event types

<table>
    <thead>
        <tr>
            <th>Event</th>
            <th>Data</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><code>SYSTEM_INFO</code></td>
            <td><pre>{`{
  "created_at": datetime,
  "cpu_current_usage": float,
  "mem_current_bytes": int,
  "is_cpu_overloaded": bool
}`}
            </pre></td>
            <td>
                <p>This event is emitted regularly and it indicates the current resource usage of the Actor.</p>
                The <code>is_cpu_overloaded</code> argument indicates whether the current CPU usage is higher than <code>Config.max_used_cpu_ratio</code>
            </td>
        </tr>
        <tr>
            <td><code>MIGRATING</code></td>
            <td><code>None</code></td>
            <td>
                <p>Emitted when the Actor running on the Apify platform
                is going to be <a href="https://docs.apify.com/platform/actors/development/state-persistence#what-is-a-migration">migrated</a>
                {' '}to another worker server soon.</p>
                You can use it to persist the state of the Actor so that once it is executed again on the new server,
                it doesn't have to start over from the beginning.
                Once you have persisted the state of your Actor, you can call <ApiLink to="class/Actor#reboot">`Actor.reboot`</ApiLink>
                to reboot the Actor and trigger the migration immediately, to speed up the process.
            </td>
        </tr>
        <tr>
            <td><code>ABORTING</code></td>
            <td><code>None</code></td>
            <td>
                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 <code>ABORTING</code> event which you can use to finish all running tasks and do cleanup.
            </td>
        </tr>
        <tr>
            <td><code>PERSIST_STATE</code></td>
            <td><pre>{`{ "is_migrating": bool }`}</pre></td>
            <td>
                <p>Emitted in regular intervals (by default 60 seconds) to notify the Actor that it should persist its state,
                in order to avoid repeating all work when the Actor restarts.</p>
                <p>This event is also emitted automatically when the <code>MIGRATING</code> event happens,
                in which case the <code>is_migrating</code> flag is set to <code>True</code>.</p>
                Note that the <code>PERSIST_STATE</code> event is provided merely for user convenience,
                you can achieve the same effect by persisting the state regularly in an interval and listening for the migrating event.
            </td>
        </tr>
        <tr>
            <td><code>EXIT</code></td>
            <td><code>None</code></td>
            <td>
                Emitted by the SDK (not the platform) when the Actor is about to exit. You can use this event to perform final cleanup tasks,
                such as closing external connections or sending notifications, before the Actor shuts down.
            </td>
        </tr>
    </tbody>
</table>

## Adding handlers to events

To add handlers to these events, you use the <ApiLink to="class/Actor#on">`Actor.on`</ApiLink> method,
and to remove them, you use the <ApiLink to="class/Actor#off">`Actor.off`</ApiLink> method.

<RunnableCodeBlock className="language-python" language="python">
    {ActorEventsExample}
</RunnableCodeBlock>

## Automatic state persistence

The example above shows how to manually persist state using the `PERSIST_STATE` event. For most use cases, you can use the <ApiLink to="class/Actor#use_state">`Actor.use_state`</ApiLink> method instead, which handles state persistence automatically.

`Actor.use_state` returns a dictionary that is automatically saved to the default key-value store at regular intervals and whenever a migration or shutdown occurs. You can modify the dictionary in place, and changes are persisted without any manual `set_value` calls.

You can optionally specify a `key` (the key-value store key under which the state is stored) and a `kvs_name` (the name of the key-value store to use). By default, the state is stored in the default key-value store under a default key.

<RunnableCodeBlock className="language-python" language="python">
    {UseStateExample}
</RunnableCodeBlock>

For more details on platform events and state persistence, see the [system events](https://docs.apify.com/platform/actors/development/programming-interface/system-events) and [state persistence](https://docs.apify.com/platform/actors/development/state-persistence) documentation on the Apify platform.
