Skip to content

Commit 96ba05f

Browse files
committed
fix: pass apify client down to output job log wherever possible (#839)
1 parent 3defae0 commit 96ba05f

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
@@ -300,7 +300,7 @@ Skipping push. Use --force to override.`,
300300
});
301301

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

src/commands/builds/create.ts

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

137137
if (log) {
138138
try {
139-
await outputJobLog(build);
139+
await outputJobLog({ job: build, apifyClient: client });
140140
} catch (err) {
141141
// This should never happen...
142142
error({

src/commands/builds/log.ts

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

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

src/commands/runs/log.ts

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

3131
try {
32-
await outputJobLog(run);
32+
await outputJobLog({ job: run, apifyClient });
3333
} catch (err) {
3434
// This should never happen...
3535
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
@@ -390,25 +390,33 @@ export const purgeDefaultKeyValueStore = async () => {
390390
await Promise.all(deletePromises);
391391
};
392392

393-
export const outputJobLog = async (job: ActorRun | Build, timeout?: number) => {
393+
export const outputJobLog = async ({
394+
job,
395+
timeoutMillis,
396+
apifyClient,
397+
}: {
398+
job: ActorRun | Build;
399+
timeoutMillis?: number;
400+
apifyClient?: ApifyClient;
401+
}) => {
394402
const { id: logId, status } = job;
395-
const apifyClient = new ApifyClient({ baseUrl: process.env.APIFY_CLIENT_BASE_URL });
403+
const client = apifyClient || new ApifyClient({ baseUrl: process.env.APIFY_CLIENT_BASE_URL });
396404

397405
// In case job was already done just output log
398406
if (ACTOR_JOB_TERMINAL_STATUSES.includes(status as never)) {
399407
if (process.env.APIFY_NO_LOGS_IN_TESTS) {
400408
return;
401409
}
402410

403-
const log = await apifyClient.log(logId).get();
411+
const log = await client.log(logId).get();
404412
process.stderr.write(log!);
405413
return;
406414
}
407415

408416
// In other case stream it to stderr
409417
// eslint-disable-next-line no-async-promise-executor
410418
return new Promise<'no-logs' | 'finished' | 'timeouts'>(async (resolve) => {
411-
const stream = await apifyClient.log(logId).stream();
419+
const stream = await client.log(logId).stream();
412420

413421
if (!stream) {
414422
resolve('no-logs');
@@ -435,11 +443,11 @@ export const outputJobLog = async (job: ActorRun | Build, timeout?: number) => {
435443
}
436444
});
437445

438-
if (timeout) {
446+
if (timeoutMillis) {
439447
nodeTimeout = setTimeout(() => {
440448
stream.destroy();
441449
resolve('timeouts');
442-
}, timeout);
450+
}, timeoutMillis);
443451
}
444452
});
445453
};

0 commit comments

Comments
 (0)