Skip to content

Commit 1cffc5e

Browse files
committed
Update types to match the latest API and add docs
1 parent 6bb337b commit 1cffc5e

File tree

5 files changed

+105
-63
lines changed

5 files changed

+105
-63
lines changed

docs/guides/log_redirection.mdx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
id: log-redirection
3+
title: Log redirection
4+
description: Redirect logs from another Actor run
5+
---
6+
7+
import ApiLink from '@site/src/components/ApiLink';
8+
9+
In some situations, one Actor is going to start one or more other Actors and wait for them to finish and produce some results. In such cases, you might want to redirect the logs of the started Actors' runs back to the parent Actor run, so that you can see the progress of the started Actors' runs in the parent Actor's logs. This guide will show possibilities on how to do it.
10+
11+
### Redirecting logs from Actor.call
12+
13+
Typical use case for log redirection is to call another Actor using the [`Actor.call()`](/reference/class/Actor#call) method. This method has an optional logger argument, which is by default set to the `default` literal. This means that the logs of the called Actor will be automatically redirected to the parent Actor's logs with default formatting and filtering. If you set the logger argument to `null`, then no log redirection happens. The third option is to pass your own `Log` instance with the possibility to define your specific logging. Below you can see those three possible ways of log redirection when starting another Actor run through Actor.call.
14+
15+
```javascript
16+
import { Actor } from 'apify';
17+
import {LoggerActorRedirect} from 'apify-client';
18+
import { LEVELS, Log } from '@apify/log';
19+
20+
const input = {}; // Some Actor input
21+
const actorId= 'someActorId'; // ID of actor you want to call
22+
23+
await Actor.init();
24+
25+
// Default log redirection - implicitly
26+
await Actor.call(actorId, input);
27+
28+
// Default log redirection - explicitly
29+
await Actor.call(actorId, input, {log:'default'});
30+
31+
// No log redirection
32+
await Actor.call(actorId, input, {log:null});
33+
34+
// Custom log redirection
35+
await Actor.call(actorId, input, {log: new Log({ level: LEVELS.DEBUG, prefix: 'customPrefix', logger: new LoggerActorRedirect()});
36+
37+
await Actor.exit();
38+
```
39+
40+
Each default redirect log entry will have a specific format. After the timestamp, it will contain cyan colored text that will contain the redirect information - the other Actor's name and the run ID. The rest of the log message will be printed in the same manner as the parent Actor's log is configured.
41+
42+
The log redirection can be deep, meaning that if the other Actor also starts another actor and is redirecting logs from it, then in the top-level Actor, you can see it as well. See the following example screenshot of the Apify log console when one Actor recursively starts itself (there are 2 levels of recursion in the example).
43+
44+
### Redirecting logs from already running Actor run
45+
46+
In some cases, you might want to connect to an already running Actor run and redirect its logs to your current Actor run. This can be done using the <ApiLink to="class/ApifyClient">`ApifyClient`</ApiLink> and getting the streamed log from a specific Actor run. You can then control the log redirection manually by explicitly calling start and stop methods.
47+
48+
You can further decide whether you want to redirect just new logs of the ongoing Actor run, or if you also want to redirect historical logs from that Actor's run, so all logs it has produced since it was started. This can be controlled by the `fromStart` option of the `getStreamedLog()` method. If you set it to `true`, all historical logs will be redirected first, and then new logs will be redirected as they are produced. If you set it to `false`, only new logs will be redirected.
49+
50+
```javascript
51+
import { Actor } from 'apify';
52+
53+
const actorRunId = 'someRunId'; // ID of actor you want to call
54+
55+
await Actor.init();
56+
57+
// Redirect only new logs from the other Actor run, due to `fromStart: false`
58+
await Actor.newClient().run(actorRunId).getStreamedLog({ fromStart: false });
59+
streamedLog.start();
60+
// Do some stuff while also redirecting logs from another Actor
61+
await streamedLog.stop();
62+
63+
await Actor.exit();
64+
```

package-lock.json

Lines changed: 18 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"@types/semver": "^7.5.8",
7878
"@types/tough-cookie": "^4.0.5",
7979
"@types/ws": "^8.5.12",
80-
"apify-client": "^2.17.0",
80+
"apify-client": "^2.20.0",
8181
"commitlint": "^20.0.0",
8282
"crawlee": "^3.13.5",
8383
"eslint": "^9.23.0",

packages/apify/src/actor.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
import { sleep, snakeCaseToCamelCase } from '@crawlee/utils';
2727
import type {
2828
ActorCallOptions,
29+
ActorStartOptions,
2930
ApifyClientOptions,
3031
RunAbortOptions,
3132
TaskCallOptions,
@@ -227,40 +228,37 @@ export interface ApifyEnv {
227228

228229
export type UserFunc<T = unknown> = () => Awaitable<T>;
229230

230-
export interface CallOptions extends Omit<ActorCallOptions, 'timeout'> {
231+
export interface Token {
231232
/**
232-
* User API token that is used to run the Actor. By default, it is taken from the `APIFY_TOKEN` environment variable.
233+
* User Apify API token that is used for API calls. By default, it is taken from the `APIFY_TOKEN` environment variable.
233234
*/
234235
token?: string;
235-
/**
236-
* Timeout for the Actor run in seconds, or `'inherit'`.
237-
*
238-
* Using `inherit` will set timeout of the newly started Actor run to the time
239-
* remaining until this Actor run times out so that the new run does not outlive this one.
240-
*/
241-
timeout?: number | 'inherit';
242236
}
243237

244-
export interface CallTaskOptions extends Omit<TaskCallOptions, 'timeout'> {
238+
export interface Timeout {
245239
/**
246-
* User API token that is used to run the Actor. By default, it is taken from the `APIFY_TOKEN` environment variable.
247-
*/
248-
token?: string;
249-
/**
250-
* Timeout for the Actor task in seconds, or `'inherit'`.
240+
* Timeout for the Actor run in seconds, or `'inherit'`.
251241
*
252-
* Using `inherit` will set timeout of the newly started Actor task to the time
242+
* Using `inherit` will set timeout of the newly started Actor run to the time
253243
* remaining until this Actor run times out so that the new run does not outlive this one.
254244
*/
255245
timeout?: number | 'inherit';
256246
}
257247

258-
export interface AbortOptions extends RunAbortOptions {
259-
/**
260-
* User API token that is used to run the Actor. By default, it is taken from the `APIFY_TOKEN` environment variable.
261-
*/
262-
token?: string;
263-
248+
export interface CallOptions
249+
extends Omit<ActorCallOptions, 'timeout'>,
250+
Token,
251+
Timeout {}
252+
export interface StartOptions
253+
extends Omit<ActorStartOptions, 'waitForFinish' | 'timeout'>,
254+
Token,
255+
Timeout {}
256+
export interface CallTaskOptions
257+
extends Omit<TaskCallOptions, 'timeout'>,
258+
Token,
259+
Timeout {}
260+
261+
export interface AbortOptions extends RunAbortOptions, Token {
264262
/** Exit with given status message */
265263
statusMessage?: string;
266264
}
@@ -718,7 +716,7 @@ export class Actor<Data extends Dictionary = Dictionary> {
718716
async start(
719717
actorId: string,
720718
input?: unknown,
721-
options: CallOptions = {},
719+
options: StartOptions = {},
722720
): Promise<ClientActorRun> {
723721
const timeout =
724722
options.timeout === 'inherit'

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = {
2424
'guides/pay-per-event',
2525
'guides/type-script-actor',
2626
'guides/docker-images',
27+
'guides/log-redirection',
2728
],
2829
},
2930
{

0 commit comments

Comments
 (0)