Skip to content

Commit 430fc0a

Browse files
authored
docs: improve basic commands document (#1118)
streamline the prose restructure the document use tabs component to render different set of prose for each language
1 parent 4217070 commit 430fc0a

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

sources/platform/actors/development/programming_interface/basic_commands.md

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@ import TabItem from '@theme/TabItem';
1212

1313
---
1414

15-
The commands described in this section are expected to be called from within a context of a running Actor, both in a local environment or on the Apify platform.
15+
This page covers essential commands for the Apify SDK in JavaScript & Python. These commands are designed to be used within a running Actor, either in a local environment or on the Apify platform.
1616

17-
## Actor initialization
17+
## Initialize your Actor
1818

19-
First, the Actor should be initialized. During initialization, it prepares to receive events from the Apify platform. The Actor determines the machine and storage configuration and optionally purges the previous state from local storage. It will also create a default instance of the Actor class.
19+
Before using any Apify SDK methods, initialize your Actor. This step prepares the Actor to receive events from the Apify platform, sets up machine and storage configurations, and clears previous local storage states.
2020

21-
It is not required to perform the initialization explicitly because the Actor will initialize on the execution of any Actor method, but we strongly recommend it to prevent race conditions.
22-
23-
### Using the JavaScript SDK
21+
<Tabs groupId="main">
22+
<TabItem value="JavaScript" label="JavaScript">
2423

25-
The Actor is initialized by calling the `init()` method. It should be paired with an `exit()` method, which terminates the Actor. The use of `exit()` is not required but recommended. For more information, go to [Exit Actor](#exit-actor).
24+
Use the `init()` method to initialize your Actor. Pair it with `exit()` to properly terminate the Actor. For more information on `exit()`, go to [Exit Actor](#exit-actor).
2625

2726
```js
2827
import { Actor } from 'apify';
@@ -33,7 +32,8 @@ console.log('Actor starting...');
3332
await Actor.exit();
3433
```
3534

36-
An alternative way of initializing the Actor is with a `main()` function. This is useful in environments where the latest JavaScript syntax and top-level awaits are not supported. The main function is only syntax-sugar for `init()` and `exit()`. It will call `init()` before it executes its callback and `exit()` after the callback resolves.
35+
Alternatively, use the `main()` function for environments that don't support top-level awaits. The `main()` function is syntax-sugar for `init()` and `exit()`. It will call `init()` before it executes its callback and `exit()` after the callback resolves.
36+
3737

3838
```js
3939
import { Actor } from 'apify';
@@ -44,9 +44,10 @@ Actor.main(async () => {
4444
});
4545
```
4646

47-
### Using the Python SDK
47+
</TabItem>
48+
<TabItem value="Python" label="Python">
4849

49-
In the Python SDK, an Actor is written as an asynchronous context manager, which means that we can use the `with` keyword to write our Actor code into a block. The `init()` method will be called before the code block is executed, and the `exit()` method will be called after the code block is finished.
50+
In Python, use an asynchronous context manager with the `with` keyword. The `init()` method will be called before the code block is executed, and the `exit()` method will be called after the code block is finished.
5051

5152
```python
5253
from apify import Actor
@@ -57,11 +58,12 @@ async def main():
5758
# ...
5859
```
5960

60-
## Get input
61+
</TabItem>
62+
</Tabs>
6163

62-
Get access to the Actor input object passed by the user. It is parsed from a JSON file, which is stored by the system in the Actor's default key-value store. Usually, the file is called `INPUT`, but the exact key is defined in the `ACTOR_INPUT_KEY` environment variable.
64+
## Get input
6365

64-
The input is an object with properties. If the Actor defines the input schema, the input object is guaranteed to conform to it. For details, see [Input and output](#input-and-output).
66+
Access the Actor's input object, which is stored as a JSON file in the Actor's default key-value store. The input is an object with properties. If the Actor defines the input schema, the input object is guaranteed to conform to it. For details, check out [Input and output](#input-and-output).
6567

6668
<Tabs groupId="main">
6769
<TabItem value="JavaScript" label="JavaScript">
@@ -94,11 +96,11 @@ async def main():
9496
</TabItem>
9597
</Tabs>
9698

97-
## Key-value store access
99+
Usually, the file is called `INPUT`, but the exact key is defined in the `ACTOR_INPUT_KEY` environment variable.
98100

99-
Write and read arbitrary files using a storage called [Key-value store](../../../storage/key_value_store.md). When an Actor starts, by default, it is associated with a newly-created key-value store, which only contains one file with the input of the Actor (see [Get input](#get-input)).
101+
## Key-value store access
100102

101-
The user can override this behavior and specify another key-value store or input key when running the Actor.
103+
Use the [Key-value store](../../../storage/key_value_store.md) to read and write arbitrary files
102104

103105
<Tabs groupId="main">
104106
<TabItem value="JavaScript" label="JavaScript">
@@ -148,9 +150,9 @@ async def main():
148150

149151
## Push results to the dataset
150152

151-
Larger results can be saved to append-only object storage called [Dataset](../../../storage/dataset.md). When an Actor starts, by default, it is associated with a newly-created empty default dataset. The Actor can create additional datasets or access existing datasets created by other Actors and use those as needed.
153+
Store larger results in a [Dataset](../../../storage/dataset.md), an append-only object storage
152154

153-
Note that Datasets can optionally be equipped with the schema that ensures only certain kinds of objects are stored in them. See [Dataset schema file](../../../storage/dataset.md) for more details.
155+
Note that Datasets can optionally be equipped with the schema that ensures only certain kinds of objects are stored in them.
154156

155157
<Tabs groupId="main">
156158
<TabItem value="JavaScript" label="JavaScript">
@@ -183,7 +185,16 @@ async def main():
183185

184186
## Exit Actor
185187

186-
When the main Actor process exits (i.e. the Docker container stops running), the Actor run is considered finished, and the process exit code is used to determine whether the Actor has succeeded (exit code `0` leads to status `SUCCEEDED`) or failed (exit code not equal to `0` leads to status `SUCCEEDED`). In this case, the platforms set a status message to a default value like `Actor exit with exit code 0`, which is not very descriptive for the users.
188+
When an Actor's main process terminates, the Actor run is considered finished. The process exit code determines Actor's final status:
189+
190+
- Exit code `0`: Status `SUCCEEDED`
191+
- Exit code not equal to `0`: Status `FAILED`
192+
193+
By default, the platform sets a generic status message like _Actor exit with exit code 0_. However, you can provide more informative message using the SDK's exit methods.
194+
195+
### Basic exit
196+
197+
Use the `exit()` method to terminate the Actor with a custom status message:
187198

188199
<Tabs groupId="main">
189200
<TabItem value="JavaScript" label="JavaScript">
@@ -215,6 +226,10 @@ async def main():
215226
</TabItem>
216227
</Tabs>
217228

229+
### Immediate exit
230+
231+
To exit immediately without calling exit handlers:
232+
218233

219234
<Tabs groupId="main">
220235
<TabItem value="JavaScript" label="JavaScript">
@@ -245,6 +260,9 @@ async def main():
245260
</TabItem>
246261
</Tabs>
247262

263+
### Failed exit
264+
265+
To indicate a failed run:
248266

249267
<Tabs groupId="main">
250268
<TabItem value="JavaScript" label="JavaScript">
@@ -275,10 +293,15 @@ async def main():
275293
</TabItem>
276294
</Tabs>
277295

278-
An alternative and preferred way to exit an Actor is using the `exit` function in the SDK, as shown below. This has two advantages:
296+
### Preferred exit methods
297+
298+
The SDK provides convenient methods for exiting Actors:
299+
300+
1. Use `exit()` with custom messages to inform users about the Actor's achievements or issues.
279301

280-
- You can provide a custom status message for users to tell them what the Actor achieved. On error, try to explain to users what happened, and most importantly, how they can fix the error. This greatly improves user experience.
281-
- The system emits the `exit` event, which can be listened to and used by various components of the Actor to perform a cleanup, persist state, etc. Note that the caller of exit can specify how long the system should wait for all `exit` event handlers to complete before closing the process, using the `timeoutSecs` option. For details, see [System Events](#system-events).
302+
2. The `exit()` method emits `exit` event allowing components to perform cleanup or state persistence.
303+
304+
Example of a failed exit using a shorthand method:
282305

283306
<Tabs groupId="main">
284307
<TabItem value="JavaScript" label="JavaScript">
@@ -309,6 +332,9 @@ async def main():
309332
</TabItem>
310333
</Tabs>
311334

335+
### Exit event handlers (JavaScript only)
336+
337+
In JavaScript, you can register handlers for the `exit` event:
312338

313339
<Tabs groupId="main">
314340
<TabItem value="JavaScript" label="JavaScript">
@@ -332,7 +358,6 @@ await Actor.exit();
332358

333359
```python
334360
# 😔 Custom handlers are not supported in the Python SDK yet.
335-
# 👉 Stay tuned and follow the news at https://apify.com/change-log
336361
```
337362

338363
</TabItem>

0 commit comments

Comments
 (0)