Skip to content

Commit c0719e0

Browse files
authored
feat: improve standby docs (#1096)
This PR moves development part of Actor Standby into the corresponding docs section. We also mention standby port in env vars and `usesStandbyMode` in `actor.json` section.
1 parent a13643e commit c0719e0

File tree

6 files changed

+83
-57
lines changed

6 files changed

+83
-57
lines changed

sources/platform/actors/development/actor_definition/actor_json.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import TabItem from '@theme/TabItem';
3131
"MYSQL_USER": "my_username",
3232
"MYSQL_PASSWORD": "@mySecretPassword"
3333
},
34+
"usesStandbyMode": false,
3435
"dockerfile": "./Dockerfile",
3536
"readme": "./ACTOR.md",
3637
"input": "./input_schema.json",
@@ -77,3 +78,4 @@ Actor `name`, `version`, `buildTag`, and `environmentVariables` are currently on
7778
| `storages.dataset` | Optional | You can define the schema of the items in your dataset under the `storages.dataset` field. This can be either an embedded object or a path to a JSON schema file. [Read more](./output_schema.md#specification-version-1) about Actor output schemas. |
7879
| `minMemoryMbytes` | Optional | Specifies the minimum amount of memory in megabytes required by the Actor to run. Requires an _integer_ value. If both `minMemoryMbytes` and `maxMemoryMbytes` are set, then `minMemoryMbytes` must be equal or lower than `maxMemoryMbytes`. Refer to the [Usage and resoursces](https://docs.apify.com/platform/actors/running/usage-and-resources#memory) for more details about memory allocation. |
7980
| `maxMemoryMbytes` | Optional | Specifies the maximum amount of memory in megabytes required by the Actor to run. It can be used to control the costs of run, especially when developing pay per result Actors. Requires an _integer_ value. Refer to the [Usage and resoursces](https://docs.apify.com/platform/actors/running/usage-and-resources#memory) for more details about memory allocation. |
81+
| `usesStandbyMode` | Optional | Boolean specifying whether the Actor will have [Standby mode](../programming_interface/actor_standby.md) enabled. |
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: Actor Standby
3+
description: Use the Actor as a real-time API server.
4+
slug: /actors/development/programming-interface/standby
5+
sidebar_position: 9
6+
---
7+
8+
**Use Actors in lightweight mode as a blazingly fast API server.**
9+
10+
---
11+
12+
Traditional Actors are designed to run a single task and then stop. They're mostly intended for batch jobs, such as when you need to perform a large scrape or data processing task.
13+
However, in some applications, waiting for an Actor to start is not an option. Actor Standby mode solves this problem by letting you have the Actor ready
14+
in the background, waiting for the incoming HTTP requests. In a sense, the Actor behaves like a real-time web server or standard API server.
15+
16+
#### How can I develop Actors using Standby mode
17+
18+
import Tabs from '@theme/Tabs';
19+
import TabItem from '@theme/TabItem';
20+
21+
You can head to the Settings tab of your Actor, enable Standby mode, and set the default configuration.
22+
![Standby for creators](./images/standby-creators.png)
23+
24+
Actors using Standby mode must run a HTTP server listening on a specific port. The user requests will then be proxied to the HTTP server.
25+
You can get the port using the Actor configuration available in Apify SDK.
26+
27+
If you need to override the port, you can do it via the `ACTOR_STANDBY_PORT` environment variable.
28+
See example below with a simple Actor using Standby mode.
29+
30+
<Tabs groupId="main">
31+
<TabItem value="JavaScript" label="JavaScript">
32+
33+
```js
34+
import http from 'http';
35+
import { Actor } from 'apify';
36+
37+
await Actor.init();
38+
39+
const server = http.createServer((req, res) => {
40+
res.writeHead(200, { 'Content-Type': 'text/plain' });
41+
res.end('Hello from Actor Standby!\n');
42+
});
43+
44+
server.listen(Actor.config.get('standbyPort'));
45+
```
46+
47+
</TabItem>
48+
<TabItem value="Python" label="Python">
49+
50+
```python
51+
from http.server import HTTPServer, SimpleHTTPRequestHandler
52+
from apify import Actor
53+
54+
class GetHandler(SimpleHTTPRequestHandler):
55+
def do_GET(self):
56+
self.send_response(200)
57+
self.end_headers()
58+
self.wfile.write(b'Hello from Actor Standby!')
59+
60+
async def main() -> None:
61+
async with Actor:
62+
with HTTPServer(('', Actor.config.standby_port), GetHandler) as http_server:
63+
http_server.serve_forever()
64+
```
65+
66+
</TabItem>
67+
</Tabs>
68+
69+
Please make sure to describe your Actors, their endpoints, and the schema for their
70+
inputs and ouputs in your README.
71+
72+
#### Can I monetize my Actor in the Standby mode
73+
74+
No, Standby mode is in Beta, and monetization is not supported.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The Actor's process has several environment variables set to provide it with con
3535
| `APIFY_IS_AT_HOME` | Is set to **1** if the Actor is running on Apify servers. |
3636
| `ACTOR_MEMORY_MBYTES` | Indicates the size of memory allocated for the Actor run, in megabytes. It can be used by Actors to optimize their memory usage. |
3737
| `APIFY_PROXY_PASSWORD` | The [Apify Proxy](../../../proxy/index.md) password of the user who started the Actor. |
38+
| `ACTOR_STANDBY_PORT` | TCP port on which the Actor can start an HTTP server to receive messages from [Actor Standby](./actor_standby.md). |
3839
| `ACTOR_STARTED_AT` | Date when the Actor was started. |
3940
| `ACTOR_TIMEOUT_AT` | Date when the Actor will time out. |
4041
| `APIFY_TOKEN` | The API token of the user who started the Actor. |

sources/platform/actors/development/programming_interface/index.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ This chapter will guide you through all the commands you need to build your firs
4545
desc="The metamorph operation transforms an Actor run into the run of another Actor with a new input."
4646
to="/platform/actors/development/programming-interface/metamorph"
4747
/>
48+
<Card
49+
title="Actor Standby"
50+
desc="Learn how to use Actors in lightweight mode as a blazingly fast API server."
51+
to="/platform/actors/development/programming-interface/standby"
52+
/>
4853
</CardGrid>

sources/platform/actors/running/actor_standby.md

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -62,60 +62,4 @@ No, even if you use the Actor-level hostname with the default configuration, the
6262

6363
#### How can I develop Actors using Standby mode
6464

65-
import Tabs from '@theme/Tabs';
66-
import TabItem from '@theme/TabItem';
67-
68-
You can head to the Settings tab of your Actor, enable Standby mode, and set the default configuration.
69-
![Standby for creators](./images/actor_standby/standby-creators.png)
70-
71-
Actors using Standby mode must run a HTTP server listening on a specific port. The user requests will then be proxied to the HTTP server.
72-
You can get the port using the Actor configuration available in Apify SDK.
73-
74-
If you need to override the port, you can do it via the `ACTOR_STANDBY_PORT` environment variable.
75-
See example below with a simple Actor using Standby mode.
76-
77-
<Tabs groupId="main">
78-
<TabItem value="JavaScript" label="JavaScript">
79-
80-
```js
81-
import http from 'http';
82-
import { Actor } from 'apify';
83-
84-
await Actor.init();
85-
86-
const server = http.createServer((req, res) => {
87-
res.writeHead(200, { 'Content-Type': 'text/plain' });
88-
res.end('Hello from Actor Standby!\n');
89-
});
90-
91-
server.listen(Actor.config.get('standbyPort'));
92-
```
93-
94-
</TabItem>
95-
<TabItem value="Python" label="Python">
96-
97-
```python
98-
from http.server import HTTPServer, SimpleHTTPRequestHandler
99-
from apify import Actor
100-
101-
class GetHandler(SimpleHTTPRequestHandler):
102-
def do_GET(self):
103-
self.send_response(200)
104-
self.end_headers()
105-
self.wfile.write(b'Hello from Actor Standby!')
106-
107-
async def main() -> None:
108-
async with Actor:
109-
with HTTPServer(('', Actor.config.standby_port), GetHandler) as http_server:
110-
http_server.serve_forever()
111-
```
112-
113-
</TabItem>
114-
</Tabs>
115-
116-
Please make sure to describe your Actors, their endpoints, and the schema for their
117-
inputs and ouputs in your README.
118-
119-
#### Can I monetize my Actor in the Standby mode
120-
121-
No, Standby mode is in Beta, and monetization is not supported.
65+
See the [Actor Standby development section](../development/programming_interface/actor_standby.md).

0 commit comments

Comments
 (0)