Skip to content

Commit 4a3f418

Browse files
committed
Review comments
1 parent fc7bb6c commit 4a3f418

File tree

3 files changed

+32
-49
lines changed

3 files changed

+32
-49
lines changed

src/resource_clients/actor.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import { Log } from '@apify/log';
77

88
import type { ApiClientSubResourceOptions } from '../base/api_client';
99
import { ResourceClient } from '../base/resource_client';
10-
import { cast, parseDateFields, pluckData, stringifyWebhooksToBase64 } from '../utils';
10+
import { cast, parseDateFields, pluckData, stringifyWebhooksToBase64 } from "../utils";
1111
import type { ActorVersion } from './actor_version';
1212
import { ActorVersionClient } from './actor_version';
1313
import { ActorVersionCollectionClient } from './actor_version_collection';
1414
import type { Build, BuildClientGetOptions } from './build';
1515
import { BuildClient } from './build';
1616
import { BuildCollectionClient } from './build_collection';
17-
import type { StreamedLog } from './log';
1817
import { RunClient } from './run';
1918
import { RunCollectionClient } from './run_collection';
2019
import type { WebhookUpdateData } from './webhook';
@@ -154,24 +153,14 @@ export class ActorClient extends ResourceClient {
154153
// Creating a new instance of RunClient here would only allow
155154
// setting it up as a nested route under actor API.
156155
const newRunClient = this.apifyClient.run(id);
157-
let streamedLog: StreamedLog;
158-
159-
// Log redirections is not compatible with browser environment
160-
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
161-
if (options.log === null || isBrowser) {
162-
return newRunClient.waitForFinish({ waitSecs });
163-
}
164-
if (options.log === undefined) {
165-
streamedLog = await newRunClient.getStreamedLog();
166-
} else {
167-
streamedLog = await newRunClient.getStreamedLog({ toLog: options.log });
168-
}
169156

157+
const streamedLog = await newRunClient.getStreamedLog({ toLog: options?.log });
170158
try {
171-
await streamedLog.start();
159+
await streamedLog?.start();
172160
return this.apifyClient.run(id).waitForFinish({ waitSecs });
173-
} finally {
174-
await streamedLog.stop();
161+
}
162+
finally {
163+
await streamedLog?.stop();
175164
}
176165
}
177166

src/resource_clients/run.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { LEVELS, Log } from '@apify/log';
77
import type { ApiClientOptionsWithOptionalResourcePath } from '../base/api_client';
88
import { ResourceClient } from '../base/resource_client';
99
import type { ApifyResponse } from '../http_client';
10-
import { cast, parseDateFields, pluckData } from '../utils';
10+
import { cast, isNode, parseDateFields, pluckData } from "../utils";
1111
import type { ActorRun } from './actor';
1212
import { DatasetClient } from './dataset';
1313
import { KeyValueStoreClient } from './key_value_store';
@@ -271,10 +271,15 @@ export class RunClient extends ResourceClient {
271271
/**
272272
* Get StreamedLog for convenient streaming of the run log and their redirection.
273273
*/
274-
async getStreamedLog(options: GetStreamedLogOptions = {}): Promise<StreamedLog> {
274+
async getStreamedLog(options: GetStreamedLogOptions = {}): Promise<StreamedLog | undefined> {
275275
const { fromStart = true } = options;
276276
let { toLog } = options;
277-
if (!toLog) {
277+
if (toLog === null || !isNode()) {
278+
// Explicitly no logging or not in Node.js
279+
return undefined
280+
}
281+
if (toLog === undefined) {
282+
// Create default StreamedLog
278283
// Get actor name and run id
279284
const runData = await this.get();
280285
const runId = runData ? `${runData.id ?? ''}` : '';
@@ -293,7 +298,7 @@ export class RunClient extends ResourceClient {
293298
}
294299

295300
export interface GetStreamedLogOptions {
296-
toLog?: Log;
301+
toLog?: Log | null;
297302
fromStart?: boolean;
298303
}
299304

test/mock_server/server.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ const mockServer = {
5858
},
5959
};
6060

61+
async function streamLogChunks(req, res) {
62+
// Asynchronously write each chunk to the response stream
63+
for (const chunk of MOCKED_ACTOR_LOGS) {
64+
res.write(chunk);
65+
res.flush(); // Flush the buffer and send the chunk immediately
66+
// Wait for a short period to simulate work being done on the server
67+
await new Promise((resolve) => {
68+
setTimeout(resolve, 10);
69+
});
70+
}
71+
72+
// End the response stream once all chunks have been sent
73+
res.end();
74+
}
75+
6176
// Debugging middleware
6277
app.use((req, res, next) => {
6378
next();
@@ -83,40 +98,14 @@ v2Router.use('/acts/redirect-actor-id', async (req, res) => {
8398
});
8499
v2Router.use('/acts', actorRouter);
85100
v2Router.use('/actor-builds', buildRouter);
86-
v2Router.use('/actor-runs/redirect-run-id/log', async (req, res) => {
87-
// Asynchronously write each chunk to the response stream
88-
for (const chunk of MOCKED_ACTOR_LOGS) {
89-
res.write(chunk);
90-
res.flush(); // Flush the buffer and send the chunk immediately
91-
// Wait for a short period to simulate work being done on the server
92-
await new Promise((resolve) => {
93-
setTimeout(resolve, 10);
94-
});
95-
}
96-
97-
// End the response stream once all chunks have been sent
98-
res.end();
99-
});
101+
v2Router.use('/actor-runs/redirect-run-id/log', streamLogChunks);
100102
v2Router.use('/actor-runs/redirect-run-id', async (req, res) => {
101103
res.json({ data: { id: 'redirect-run-id', actId: 'redirect-actor-id', status: 'SUCCEEDED' } });
102104
});
103105
v2Router.use('/actor-runs', runRouter);
104106
v2Router.use('/actor-tasks', taskRouter);
105107
v2Router.use('/users', userRouter);
106-
v2Router.use('/logs/redirect-log-id', async (req, res) => {
107-
// Asynchronously write each chunk to the response stream
108-
for (const chunk of MOCKED_ACTOR_LOGS) {
109-
res.write(chunk);
110-
res.flush(); // Flush the buffer and send the chunk immediately
111-
// Wait for a short period to simulate work being done on the server
112-
await new Promise((resolve) => {
113-
setTimeout(resolve, 10);
114-
});
115-
}
116-
117-
// End the response stream once all chunks have been sent
118-
res.end();
119-
});
108+
v2Router.use('/logs/redirect-log-id', streamLogChunks);
120109
v2Router.use('/logs', logRouter);
121110
v2Router.use('/datasets', datasetRouter);
122111
v2Router.use('/key-value-stores', keyValueStores);

0 commit comments

Comments
 (0)