Skip to content

Commit 5cdb06c

Browse files
authored
fix: pass apify client down to output job log wherever possible (#839)
1 parent 7272699 commit 5cdb06c

File tree

6 files changed

+19
-11
lines changed

6 files changed

+19
-11
lines changed

src/commands/actors/push.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ Skipping push. Use --force to override.`,
298298
});
299299

300300
try {
301-
await outputJobLog(build, waitForFinishMillis);
301+
await outputJobLog({ job: build, timeoutMillis: waitForFinishMillis, apifyClient });
302302
} catch (err) {
303303
warning({ message: 'Can not get log:' });
304304
console.error(err);

src/commands/builds/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class BuildsCreateCommand extends ApifyCommand<typeof BuildsCreateCommand
146146

147147
if (log) {
148148
try {
149-
await outputJobLog(build);
149+
await outputJobLog({ job: build, apifyClient: client });
150150
} catch (err) {
151151
// This should never happen...
152152
error({

src/commands/builds/log.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class BuildsLogCommand extends ApifyCommand<typeof BuildsLogCommand> {
3030
info({ message: `Log for build with ID "${buildId}":\n` });
3131

3232
try {
33-
await outputJobLog(build);
33+
await outputJobLog({ job: build, apifyClient });
3434
} catch (err) {
3535
// This should never happen...
3636
error({

src/commands/runs/log.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class RunsLogCommand extends ApifyCommand<typeof RunsLogCommand> {
3030
info({ message: `Log for run with ID "${runId}":\n`, stdout: true });
3131

3232
try {
33-
await outputJobLog(run);
33+
await outputJobLog({ job: run, apifyClient });
3434
} catch (err) {
3535
// This should never happen...
3636
error({ message: `Failed to get log for run with ID "${runId}": ${(err as Error).message}` });

src/lib/commands/run-on-cloud.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export async function* runActorOrTaskOnCloud(apifyClient: ApifyClient, options:
9595

9696
if (!silent && printRunLogs) {
9797
try {
98-
const res = await outputJobLog(run, waitForFinishMillis);
98+
const res = await outputJobLog({ job: run, timeoutMillis: waitForFinishMillis, apifyClient });
9999

100100
if (res === 'timeouts') {
101101
console.error(`\n${chalk.gray('Timeout for printing logs was hit, there may be future logs.')}\n`);

src/lib/utils.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,25 +365,33 @@ export const purgeDefaultKeyValueStore = async () => {
365365
await Promise.all(deletePromises);
366366
};
367367

368-
export const outputJobLog = async (job: ActorRun | Build, timeout?: number) => {
368+
export const outputJobLog = async ({
369+
job,
370+
timeoutMillis,
371+
apifyClient,
372+
}: {
373+
job: ActorRun | Build;
374+
timeoutMillis?: number;
375+
apifyClient?: ApifyClient;
376+
}) => {
369377
const { id: logId, status } = job;
370-
const apifyClient = new ApifyClient({ baseUrl: process.env.APIFY_CLIENT_BASE_URL });
378+
const client = apifyClient || new ApifyClient({ baseUrl: process.env.APIFY_CLIENT_BASE_URL });
371379

372380
// In case job was already done just output log
373381
if (ACTOR_JOB_TERMINAL_STATUSES.includes(status as never)) {
374382
if (process.env.APIFY_NO_LOGS_IN_TESTS) {
375383
return;
376384
}
377385

378-
const log = await apifyClient.log(logId).get();
386+
const log = await client.log(logId).get();
379387
process.stderr.write(log!);
380388
return;
381389
}
382390

383391
// In other case stream it to stderr
384392
// eslint-disable-next-line no-async-promise-executor
385393
return new Promise<'no-logs' | 'finished' | 'timeouts'>(async (resolve) => {
386-
const stream = await apifyClient.log(logId).stream();
394+
const stream = await client.log(logId).stream();
387395

388396
if (!stream) {
389397
resolve('no-logs');
@@ -410,11 +418,11 @@ export const outputJobLog = async (job: ActorRun | Build, timeout?: number) => {
410418
}
411419
});
412420

413-
if (timeout) {
421+
if (timeoutMillis) {
414422
nodeTimeout = setTimeout(() => {
415423
stream.destroy();
416424
resolve('timeouts');
417-
}, timeout);
425+
}, timeoutMillis);
418426
}
419427
});
420428
};

0 commit comments

Comments
 (0)