Skip to content

Commit 5743522

Browse files
committed
feat: Add "Speeding up migrations" section to "State persistence"
1 parent 12f27b8 commit 5743522

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

sources/platform/actors/development/builds_and_runs/state_persistence.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Long-running [Actor](../../index.mdx) jobs may need to migrate between servers.
1818
To prevent data loss, long-running Actors should:
1919

2020
- Periodically save (persist) their state.
21-
- Listem for [migration events](/sdk/js/api/apify/class/PlatformEventManager)
21+
- Listen for [migration events](/sdk/js/api/apify/class/PlatformEventManager)
2222
- Check for persisted state when starting, allowing them to resume from where they left off.
2323

2424
For short-running Actors, the risk of restarts and the cost of repeated runs are low, so you can typically ignore state persistence.
@@ -51,7 +51,7 @@ By default, an Actor keeps its state in the server's memory. During a server swi
5151

5252
The [Apify SDKs](/sdk) handle state persistence automatically.
5353

54-
This is done using the `Actor.on()` method and the `migrating` event.
54+
This is done using the `Actor.on()` method and the `migrating` event.
5555

5656
- The `migrating` event is triggered just before a migration occurs, allowing you to save your state.
5757
- To retrieve previously saved state, you can use the [`Actor.getValue`](/sdk/js/reference/class/Actor#getValue)/[`Actor.get_value`](/sdk/python/reference/class/Actor#get_value) methods.
@@ -81,15 +81,15 @@ await Actor.exit();
8181
<TabItem value="Python" label="Python">
8282

8383
```python
84-
from apify import Actor
84+
from apify import Actor, Event
8585

86-
async def actor_migrate():
86+
async def actor_migrate(_event_data):
8787
await Actor.set_value('my-crawling-state', {'foo': 'bar'})
8888

8989
async def main():
9090
async with Actor:
9191
# ...
92-
Actor.on('migrating', actor_migrate)
92+
Actor.on(Event.MIGRATING, actor_migrate)
9393
# ...
9494
```
9595

@@ -128,3 +128,50 @@ async def main():
128128
</Tabs>
129129

130130
For improved Actor performance consider [caching repeated page data](/academy/expert-scraping-with-apify/saving-useful-stats).
131+
132+
## Speeding up migrations
133+
134+
Once your Actor receives the `migrating` event, in under a minute, the Apify platform will shut it down and restart it on a new server.
135+
If you want to speed this process up, once you have persisted the Actor state,
136+
you can reboot the Actor manually in the `migrating` event handler using the `Actor.reboot()` method
137+
available in the [Apify SDK for JavaScript](/sdk/js/reference/class/Actor#reboot) or [Apify SDK for Python](/sdk/python/reference/class/Actor#reboot).
138+
139+
<Tabs groupId="main">
140+
<TabItem value="JavaScript" label="JavaScript">
141+
142+
```js
143+
import { Actor } from 'apify';
144+
145+
await Actor.init();
146+
// ...
147+
Actor.on('migrating', async () => {
148+
// ...
149+
// save state
150+
// ...
151+
await Actor.reboot();
152+
});
153+
// ...
154+
await Actor.exit();
155+
```
156+
157+
</TabItem>
158+
<TabItem value="Python" label="Python">
159+
160+
```python
161+
from apify import Actor, Event
162+
163+
async def actor_migrate(_event_data):
164+
# ...
165+
# save state
166+
# ...
167+
await Actor.reboot()
168+
169+
async def main():
170+
async with Actor:
171+
# ...
172+
Actor.on(Event.MIGRATING, actor_migrate)
173+
# ...
174+
```
175+
176+
</TabItem>
177+
</Tabs>

0 commit comments

Comments
 (0)