Skip to content

Commit 982f4b6

Browse files
fnesvedaTC-MO
andauthored
feat: Add "Speeding up migrations" section to "State persistence" (#1365)
We've recently added support to speed up Actor migrations by rebooting the run when it's migrating. This adds docs about that, with code examples. The support for this in the Apify SDK is not released yet, it'll go out in the next SDK release, I'll only merge the docs then. --------- Co-authored-by: Michał Olender <[email protected]>
1 parent 20d6a1e commit 982f4b6

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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, the Apify platform will shut it down and restart it on a new server within one minute.
135+
To speed this process up, once you have persisted the Actor state,
136+
you can manually reboot the Actor 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)