Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/resource_clients/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Readable } from 'node:stream';
import c from 'ansi-colors';

import type { Log } from '@apify/log';
import { Logger, LogLevel } from '@apify/log';
import log, { Logger, LogLevel } from '@apify/log';

import type { ApifyApiError } from '../apify_api_error';
import type { ApiClientSubResourceOptions } from '../base/api_client';
Expand Down Expand Up @@ -197,11 +197,15 @@ export class StreamedLog {
if (!logStream) {
return;
}
const lastChunkRemainder = await this.logStreamChunks(logStream);
// Process whatever is left when exiting. Maybe it is incomplete, maybe it is last log without EOL.
const lastMessage = Buffer.from(lastChunkRemainder).toString().trim();
if (lastMessage.length) {
this.destinationLog.info(lastMessage);
try {
const lastChunkRemainder = await this.logStreamChunks(logStream);
// Process whatever is left when exiting. Maybe it is incomplete, maybe it is last log without EOL.
const lastMessage = Buffer.from(lastChunkRemainder).toString().trim();
if (lastMessage.length) {
this.destinationLog.info(lastMessage);
}
} catch (err) {
log.warning(`Log redirection stopped due to error`, err as Error);
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/mock_server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ export function createDefaultApp(v2Router = express.Router()) {
res.json({ data: { id: 'redirect-run-id', actId: 'redirect-actor-id', status: 'SUCCEEDED' } });
});

v2Router.use('/actor-runs/econnreset-run-id/log', async (req: express.Request, res: express.Response) => {
res.write(MOCKED_ACTOR_LOGS[0]);
(res as any).flush();
await new Promise<void>((resolve) => {
setTimeout(resolve, 10);
});
req.socket.destroy();
});
v2Router.use('/actor-runs/econnreset-run-id', async (_, res) => {
res.json({ data: { id: 'econnreset-run-id', actId: 'redirect-actor-id', status: 'SUCCEEDED' } });
});

v2Router.use('/actor-runs', runs);
v2Router.use('/actor-tasks', tasks);
v2Router.use('/users', users);
Expand Down
14 changes: 14 additions & 0 deletions test/runs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,18 @@ describe('Redirect run logs', () => {
logSpy.mockRestore();
});
});

describe('run.getStreamedLog ECONNRESET', () => {
test('logs warning instead of throwing on error', async () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const streamedLog = await client.run('econnreset-run-id').getStreamedLog({ fromStart: true });
streamedLog?.start();
await setTimeoutNode(500);
await expect(streamedLog?.stop()).resolves.not.toThrow();
expect(
warnSpy.mock.calls.some(([msg]: [string]) => msg?.includes('Log redirection stopped due to error')),
).toBe(true);
warnSpy.mockRestore();
});
});
});