| 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';
During its runtime, the Actor receives Actor events sent by the Apify platform or generated by the Apify SDK itself.
| Event | Data | Description |
|---|---|---|
SYSTEM_INFO |
{`{
"created_at": datetime,
"cpu_current_usage": float,
"mem_current_bytes": int,
"is_cpu_overloaded": bool
}`}
|
This event is emitted regularly and it indicates the current resource usage of the Actor. Theis_cpu_overloaded argument indicates whether the current CPU usage is higher than Config.max_used_cpu_ratio
|
MIGRATING |
None |
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 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 callActor.reboot
to reboot the Actor and trigger the migration immediately, to speed up the process.
|
ABORTING |
None |
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 you can use to finish all running tasks and do cleanup.
|
PERSIST_STATE |
{`{ "is_migrating": bool }`} |
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. This event is also emitted automatically when the PERSIST_STATE 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.
|
To add handlers to these events, you use the Actor.on method,
and to remove them, you use the Actor.off method.